From 873dd7c5e59d59392bfcb7a7d10dbc217b5f5b65 Mon Sep 17 00:00:00 2001 From: Mike Hall Date: Mon, 11 Aug 2014 15:43:21 -0500 Subject: [PATCH 1/2] Fixed a bug where the job would be enqueued ~58 hours in the future --- app/workers/activate_pending_users_worker.rb | 4 +--- 1 file changed, 1 insertion(+), 3 deletions(-) diff --git a/app/workers/activate_pending_users_worker.rb b/app/workers/activate_pending_users_worker.rb index 0f0ef3c1..75c7cc55 100644 --- a/app/workers/activate_pending_users_worker.rb +++ b/app/workers/activate_pending_users_worker.rb @@ -9,10 +9,8 @@ def perform # but would like to spread their execution # over the next hour to avoid overloading # the database. - delay = 0 User.pending.find_each(batch_size: 100) do |user| - UserActivateWorker.perform_in(delay.minutes, user.id) - delay = delay + Random.rand(0..59) + UserActivateWorker.perform_in(Random.rand(0..59).minutes, user.id) end end end From 3ef8fc820b978f6fdbe15943c29c6a57eae3b937 Mon Sep 17 00:00:00 2001 From: Mike Hall Date: Mon, 11 Aug 2014 15:46:32 -0500 Subject: [PATCH 2/2] Fixed a bug where the job would be enqueued ~58 hours in the future --- app/workers/activate_pending_users_worker.rb | 1 - spec/workers/activate_pending_users_worker_spec.rb | 5 +++++ spec/workers/user_activate_worker_spec.rb | 4 ++-- 3 files changed, 7 insertions(+), 3 deletions(-) create mode 100644 spec/workers/activate_pending_users_worker_spec.rb diff --git a/app/workers/activate_pending_users_worker.rb b/app/workers/activate_pending_users_worker.rb index 75c7cc55..5010ffaa 100644 --- a/app/workers/activate_pending_users_worker.rb +++ b/app/workers/activate_pending_users_worker.rb @@ -2,7 +2,6 @@ class ActivatePendingUsersWorker include Sidekiq::Worker sidekiq_options queue: :critical - def perform # Spawning possibly many thousands # of workers the order doesn't really matter diff --git a/spec/workers/activate_pending_users_worker_spec.rb b/spec/workers/activate_pending_users_worker_spec.rb new file mode 100644 index 00000000..c009fd5f --- /dev/null +++ b/spec/workers/activate_pending_users_worker_spec.rb @@ -0,0 +1,5 @@ +require 'sidekiq/testing' +Sidekiq::Testing.inline! + +RSpec.describe ActivatePendingUsersWorker do +end diff --git a/spec/workers/user_activate_worker_spec.rb b/spec/workers/user_activate_worker_spec.rb index 525c9a08..6f17dcf3 100644 --- a/spec/workers/user_activate_worker_spec.rb +++ b/spec/workers/user_activate_worker_spec.rb @@ -18,7 +18,7 @@ it 'should activate user' do worker.perform(user.id) user.reload - + expect(user.active?).to eq(true) expect(user.activated_on).not_to eq(nil) end @@ -30,7 +30,7 @@ it 'should do nothing' do worker.perform(user.id) user.reload - + expect(user.updated_at).to eq(user.created_at) end end