Skip to content

Instantly share code, notes, and snippets.

@mperham
Created October 19, 2012 22:15
Show Gist options
  • Save mperham/3921028 to your computer and use it in GitHub Desktop.
Save mperham/3921028 to your computer and use it in GitHub Desktop.
Sidekiq retry management API
require 'sidekiq'
module Sidekiq
# Encapsulates a single job awaiting retry
class Retry
attr_reader :score, :item
def initialize(score, item)
@score = score
@item = Sidekiq.load_json(item)
end
def delete
count = Sidekiq.redis do |conn|
conn.zremrangebyscore('retry', @score, @score)
end
count != 0
end
def method_missing(name, *args)
@item.send(name, *args)
end
end
##
# Allows enumeration of all retries pending in Sidekiq.
# Based on this, you can search/filter for jobs. Here's an
# example where I'm selecting all jobs of a certain type
# and deleting them from the retry queue.
#
# r = Sidekiq::Retries.new
# r.select do |retri|
# retri['class'] == 'Sidekiq::Extensions::DelayedClass' &&
# retri['args'][0] == 'User' &&
# retri['args'][1] == 'setup_new_subscriber'
# end.map(&:delete)
class Retries
include Enumerable
def each(&block)
# page thru the sorted set backwards so deleting entries doesn't screw up indexing
page = -1
page_size = 50
loop do
retries = Sidekiq.redis do |conn|
conn.zrange 'retry', page * page_size, (page * page_size) + (page_size - 1), :with_scores => true
end
break if retries.empty?
page -= 1
retries.each do |retri, score|
block.call Retry.new(score, retri)
end
end
end
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment