Skip to content

Instantly share code, notes, and snippets.

@AlxGolubev
Last active June 5, 2017 14:09
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save AlxGolubev/8d8c7f0db64ed6e4232fde79c833fe19 to your computer and use it in GitHub Desktop.
Save AlxGolubev/8d8c7f0db64ed6e4232fde79c833fe19 to your computer and use it in GitHub Desktop.
Class for case when queue for worker was changed but there are a lot of scheduled jobs in queue previously used
class JobsMover
BATCH_SIZE = 100
def initialize(original_queue, klass)
@original_queue = Sidekiq::Queue.new(original_queue)
@klass = klass
end
def move
jobs = @original_queue.select { |job| job.item['class'] == @klass }
total = jobs.count
progress = 0
jobs.each_slice(BATCH_SIZE) do |jobs_set|
jobs_set.each do |job|
job.delete
@klass.constantize.perform_async(*job.item['args'])
end
print "#{progress += BATCH_SIZE} / #{total} \r"
end
end
end
class JobsRemover
BATCH_SIZE = 100
def initialize(original_queue, klass)
@original_queue = Sidekiq::Queue.new(original_queue)
@klass = klass
end
def remove
jobs = @original_queue.select { |job| job.item['class'] == @klass }
total = jobs.count
progress = 0
jobs.each_slice(BATCH_SIZE) do |jobs_set|
jobs_set.each do |job|
job.delete
end
print "#{progress += BATCH_SIZE} / #{total} \r"
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment