Skip to content

Skip search indexation if another job is enqueued. #224

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Oct 15, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions app/jobs/search_sync_job.rb
Original file line number Diff line number Diff line change
@@ -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

Expand All @@ -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)
Expand All @@ -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
1 change: 1 addition & 0 deletions config/sidekiq.yml
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ production:
:queues:
- [low, 1]
- [default, 2]
- [search_sync, 2]
- [medium, 3]
- [high, 4]
- [urgent, 5]
Expand Down