diff --git a/app/jobs/search_sync_job.rb b/app/jobs/search_sync_job.rb index 40b9953c..351d5d37 100644 --- a/app/jobs/search_sync_job.rb +++ b/app/jobs/search_sync_job.rb @@ -1,9 +1,11 @@ class SearchSyncJob include Sidekiq::Worker + sidekiq_options queue: :search_sync - sidekiq_options queue: :medium - + # TODO refactor this, when we drop Tire. def perform + return if duplicate_job? # Skip if there is more enqueued jobs + number_of_protips_in_index = Protip.tire.search { query { all } }.total number_of_protips_in_database = Protip.count @@ -13,7 +15,7 @@ def perform query { all } end.map { |protip| protip.id.to_i } - protips_in_database = Protip.select(:id).map(&:id) + protips_in_database = Protip.pluck(:id) #now that we know the sets in db and index, calculate the missing records nonexistent_protips = (protips_in_index - protips_in_database) @@ -26,8 +28,10 @@ def perform unindexed_protips.each do |unindexed_protip_id| IndexProtipJob.perform_async(unindexed_protip_id) end - - puts "removed #{nonexistent_protips.count} protips and added #{unindexed_protips.count} protips" end end + + def duplicate_job? + Sidekiq::Queue.new('search_sync').size > 2 + end end diff --git a/config/sidekiq.yml b/config/sidekiq.yml index 2d33e2bd..588a75bb 100644 --- a/config/sidekiq.yml +++ b/config/sidekiq.yml @@ -7,6 +7,7 @@ production: :queues: - [low, 1] - [default, 2] + - [search_sync, 2] - [medium, 3] - [high, 4] - [urgent, 5]