-
Notifications
You must be signed in to change notification settings - Fork 2.5k
API
Sidekiq has a public API allowing access to real-time information about workers, queues and jobs. See sidekiq/api for RDoc.
stats = Sidekiq::Stats.newGets the number of jobs that have been processed.
stats.processed # => 100Gets the number of jobs that have failed.
stats.failed # => 3Get the queues with name and number enqueued.
stats.queues # => { "default" => 1001, "email" => 50 }Gets the number of jobs enqueued in all queues (does NOT include retries and scheduled jobs).
stats.enqueued # => 5 All dates are UTC and history stats are cleared after 5 years.
Get history of failed/processed stats:
s = Sidekiq::Stats::History.new(2) # Indicates how many days of data you want starting from today (UTC)
s.failed # => { "2012-12-05" => 120, "2012-12-04" => 234 }
s.processed # => { "2012-12-05" => 1010, "2012-12-04" => 1500 }Start from a different date:
s = Sidekiq::Stats::History.new( 3, Date.parse("2012-12-3") )
s.failed # => { "2012-12-03" => 10, "2012-12-02" => 24, "2012-12-01" => 4 }
s.processed # => { "2012-12-03" => 124, "2012-12-02" => 345, "2012-12-01" => 355 } Sidekiq::Queue.new # the default queue
Sidekiq::Queue.new("mailer")Gets the number of jobs within a queue.
Sidekiq::Queue.new.size # => 4Deletes all Jobs in a Queue, by removing the queue.
Sidekiq::Queue.new.clearDeletes jobs within the queue mailer with a jid of 'abcdef1234567890'
queue = Sidekiq::Queue.new("mailer")
queue.each do |job|
job.klass # => 'MyWorker'
job.args # => [1, 2, 3]
job.delete if job.jid == 'abcdef1234567890'
endCalculate the latency (in seconds) of a queue (now - when the oldest job was enqueued):
> Sidekiq::Queue.new.latency
14.5The scheduled sorted set holds all scheduled jobs in chronologically-sorted order.
Sidekiq::ScheduledSet.newClear the queue
Sidekiq::ScheduledSet.new.clearAllows enumeration of scheduled jobs within 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 scheduled queue.
r = Sidekiq::ScheduledSet.new
r.select do |retri|
retri.klass == 'Sidekiq::Extensions::DelayedClass' &&
# For Sidekiq::Extensions (e.g., Foo.delay.bar(*args)),
# the context is serialized to YAML, and must
# be deserialized to get to the original args
((klass, method, args) = YAML.load(retri.args[0])) &&
klass == User &&
method == :setup_new_subscriber
end.map(&:delete)The retry sorted set holds all jobs to be retried in chronologically sorted order.
Sidekiq::RetrySet.newClear the queue
Sidekiq::RetrySet.new.clearAllows enumeration of retries within 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.
query = Sidekiq::RetrySet.new
query.select do |job|
job.klass == 'Sidekiq::Extensions::DelayedClass' &&
# For Sidekiq::Extensions (e.g., Foo.delay.bar(*args)),
# the context is serialized to YAML, and must
# be deserialized to get to the original args
((klass, method, args) = YAML.load(job.args[0])) &&
klass == User &&
method == :setup_new_subscriber
end.map(&:delete)Programmatic access to the current active worker set.
WARNING:
This is live data that can change every millisecond. If you call #size => 5 and then expect #each to be called 5 times, you're going to have a bad time.
workers = Sidekiq::Workers.new
workers.size => 2
workers.each do |name, work, started_at|
# name is a unique identifier per worker
# work is a Hash which looks like:
# { 'queue' => name, 'run_at' => timestamp, 'payload' => msg }
# started_at is a String rep of the time when the worker started working on the job
end