Skip to content

Commit 2283cb0

Browse files
committed
Restored the search sync task as a sidekiq job
1 parent 310232d commit 2283cb0

File tree

5 files changed

+60
-186
lines changed

5 files changed

+60
-186
lines changed

app/clock.rb

Lines changed: 16 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,6 @@
55

66
include Clockwork
77

8-
98
# Runs as 1:01 AM Pacific
109
every(1.day, 'award:activate:active', at: '01:01') do
1110
ActivatePendingUsersWorker.perform_async
@@ -25,12 +24,23 @@
2524
end
2625
end
2726

28-
every(1.day, 'cleanup:protips:associate_zombie_upvotes', at: '03:30') { CleanupProtipsAssociateZombieUpvotesJob.perform_async }
29-
every(1.day, 'clear_expired_sessions', at: '06:00') { ClearExpiredSessionsJob.perform_async }
30-
every(1.day, 'protips:recalculate_scores', at: '00:00') {}
31-
every(1.day, 'search:sync', at: '00:00') {}
32-
every(1.day, 'teams:refresh', at: '00:00') {}
27+
every(1.day, 'cleanup:protips:associate_zombie_upvotes', at: '03:30') do
28+
CleanupProtipsAssociateZombieUpvotesJob.perform_async
29+
end
30+
31+
every(1.day, 'clear_expired_sessions', at: '06:00') do
32+
ClearExpiredSessionsJob.perform_async
33+
end
3334

35+
every(1.day, 'protips:recalculate_scores', at: '03:00') do
36+
ProtipsRecalculateScoresJob.perform_async
37+
end
38+
39+
every(1.day, 'search:sync', at: '04:00') do
40+
SearchSyncJob.perform_async
41+
end
42+
43+
every(1.day, 'teams:refresh', at: '00:00') {}
3444

3545
# This is tied with broken code. Probably should delete
3646
every(1.day, 'facts:system', at: '00:00') {}
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
class ProtipsRecalculateScoresJob
2+
include Sidekiq::Worker
3+
4+
sidekiq_options queue: :low
5+
6+
def perform
7+
Protip.where('created_at > ?', 25.hours.ago).where(upvotes_value_cache: nil).each do |protip|
8+
ProcessProtipJob.perform_async(:recalculate_score, protip.id)
9+
end
10+
end
11+
end

app/jobs/search_sync_job.rb

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
class SearchSyncJob
2+
include Sidekiq::Worker
3+
4+
sidekiq_options queue: :medium
5+
6+
def perform
7+
number_of_protips_in_index = Protip.tire.search { query { all } }.total
8+
number_of_protips_in_database = Protip.count
9+
10+
if number_of_protips_in_index != number_of_protips_in_database
11+
protips_in_index = Protip.tire.search do
12+
size number_of_protips_in_index
13+
query { all }
14+
end.map { |protip| protip.id.to_i }
15+
16+
protips_in_database = Protip.select(:id).map(&:id)
17+
18+
#now that we know the sets in db and index, calculate the missing records
19+
nonexistent_protips = (protips_in_index - protips_in_database)
20+
unindexed_protips = (protips_in_database - protips_in_index)
21+
22+
nonexistent_protips.each do |nonexistent_protip_id|
23+
Protip.index.remove({'_id' => nonexistent_protip_id, '_type' => 'protip'})
24+
end
25+
26+
unindexed_protips.each do |unindexed_protip_id|
27+
IndexProtip.perform_async(unindexed_protip_id)
28+
end
29+
30+
puts "removed #{nonexistent_protips.count} protips and added #{unindexed_protips.count} protips"
31+
end
32+
end
33+
end

lib/tasks/protips.rake

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

lib/tasks/search.rake

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

0 commit comments

Comments
 (0)