Skip to content
Anthony Panozzo edited this page Apr 11, 2014 · 65 revisions

Sidekiq has a public API allowing access to real-time information about workers, queues and jobs. See sidekiq/api for RDoc. Require the API to get access to all of the functionality described below.

require 'sidekiq/api'

Stats

stats = Sidekiq::Stats.new

Gets the number of jobs that have been processed.

stats.processed # => 100

Gets the number of jobs that have failed.

stats.failed # => 3

Get 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 

Stats History

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 }

Queue

  Sidekiq::Queue.new # the default queue
  Sidekiq::Queue.new("mailer")

Gets the number of jobs within a queue.

  Sidekiq::Queue.new.size # => 4

Deletes all Jobs in a Queue, by removing the queue.

  Sidekiq::Queue.new.clear

Deletes 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'
end

Calculate the latency (in seconds) of a queue (now - when the oldest job was enqueued):

> Sidekiq::Queue.new.latency
14.5

Named Queues

Scheduled

The scheduled sorted set holds all scheduled jobs in chronologically-sorted order.

Sidekiq::ScheduledSet.new

Clear the queue

Sidekiq::ScheduledSet.new.clear

Allows 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)

Retries

The retry sorted set holds all jobs to be retried in chronologically sorted order.

Sidekiq::RetrySet.new

Clear the queue

Sidekiq::RetrySet.new.clear

Allows 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)

Workers

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

Clone this wiki locally