Skip to content

Commit f003870

Browse files
seurosjust3ws
authored andcommitted
Skip search indexation if another job is enqueued.
1 parent e4b768e commit f003870

File tree

2 files changed

+10
-5
lines changed

2 files changed

+10
-5
lines changed

app/jobs/search_sync_job.rb

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,11 @@
11
class SearchSyncJob
22
include Sidekiq::Worker
3+
sidekiq_options queue: :search_sync
34

4-
sidekiq_options queue: :medium
5-
5+
# TODO refactor this, when we drop Tire.
66
def perform
7+
return if duplicate_job? # Skip if there is more enqueued jobs
8+
79
number_of_protips_in_index = Protip.tire.search { query { all } }.total
810
number_of_protips_in_database = Protip.count
911

@@ -13,7 +15,7 @@ def perform
1315
query { all }
1416
end.map { |protip| protip.id.to_i }
1517

16-
protips_in_database = Protip.select(:id).map(&:id)
18+
protips_in_database = Protip.pluck(:id)
1719

1820
#now that we know the sets in db and index, calculate the missing records
1921
nonexistent_protips = (protips_in_index - protips_in_database)
@@ -26,8 +28,10 @@ def perform
2628
unindexed_protips.each do |unindexed_protip_id|
2729
IndexProtipJob.perform_async(unindexed_protip_id)
2830
end
29-
30-
puts "removed #{nonexistent_protips.count} protips and added #{unindexed_protips.count} protips"
3131
end
3232
end
33+
34+
def duplicate_job?
35+
Sidekiq::Queue.new('search_sync').size > 2
36+
end
3337
end

config/sidekiq.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ production:
77
:queues:
88
- [low, 1]
99
- [default, 2]
10+
- [search_sync, 2]
1011
- [medium, 3]
1112
- [high, 4]
1213
- [urgent, 5]

0 commit comments

Comments
 (0)