Created
October 19, 2012 22:15
-
-
Save mperham/3921028 to your computer and use it in GitHub Desktop.
Sidekiq retry management API
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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