Skip to content

Commit 75e13ed

Browse files
committed
Added recurring scheduled worker that will activate pending users
1 parent 41438c4 commit 75e13ed

File tree

5 files changed

+24
-104
lines changed

5 files changed

+24
-104
lines changed

app/clock.rb

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,11 @@
66
include Clockwork
77

88

9-
every(1.day, 'award:activate:active', at: '00:00') { }
9+
# Runs as 1:01 AM Pacific
10+
every(1.day, 'award:activate:active', at: '01:01') do
11+
ActivatePendingUsersWorker.perform_async
12+
end
13+
1014
every(1.day, 'award:fresh:stale', at: '00:00') {}
1115
every(1.day, 'cleanup:protips:associate_zombie_upvotes', at: '00:00') {}
1216
every(1.day, 'clear_expired_sessions', at: '00:00') {}

app/models/user.rb

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -139,7 +139,7 @@ def banned?
139139
def activate
140140
UserActivateWorker.perform_async(id)
141141
end
142-
142+
143143
def activate!
144144
# TODO: Switch to update, failing validations?
145145
update_attributes!(state: ACTIVE, activated_on: DateTime.now)
@@ -166,8 +166,6 @@ def oldest_achievement_since_last_visit
166166
badges.where("badges.created_at > ?", last_request_at).order('badges.created_at ASC').last
167167
end
168168

169-
170-
171169
def company_name
172170
team.try(:name) || company
173171
end
@@ -177,7 +175,6 @@ def profile_url
177175
avatar_url
178176
end
179177

180-
181178
def can_be_refreshed?
182179
(achievements_checked_at.nil? || achievements_checked_at < 1.hour.ago)
183180
end
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
class ActivatePendingUsersWorker
2+
include Sidekiq::Worker
3+
sidekiq_options queue: :critical
4+
5+
6+
def perform
7+
# Spawning possibly many thousands
8+
# of workers the order doesn't really matter
9+
# but would like to spread their execution
10+
# over the next hour to avoid overloading
11+
# the database.
12+
delay = 0
13+
User.pending.find_each(batch_size: 100) do |user|
14+
UserActivateWorker.perform_in(delay.minutes, user.id)
15+
delay = delay + Random.rand(0..59)
16+
end
17+
end
18+
end

app/workers/user_activate_worker.rb

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,3 @@
1-
# UserActivateWorker
21
class UserActivateWorker
32
include Sidekiq::Worker
43
sidekiq_options queue: :high

lib/tasks/award.rake

Lines changed: 0 additions & 98 deletions
This file was deleted.

0 commit comments

Comments
 (0)