Skip to content
mperham edited this page Feb 3, 2013 · 65 revisions

Sidekiq has an internal API allowing one to access real-time information about workers, queues and jobs.

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

Note: All dates are UTC and history stats are cleared after 6 months

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("default")
  Sidekiq::Queue.new("mailer")

Gets the number of jobs within a queue.

  Sidekiq::Queue.new("default").size # => 4

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

  Sidekiq::Queue.new("default").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

Named Queues

Scheduled

Selects the schedule named queue

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 retry queue.

r = Sidekiq::ScheduledSet.new
r.select do |retri|
  retri.klass == 'Sidekiq::Extensions::DelayedClass' &&
  retri.args[0] == 'User' &&
  retri.args[1] == 'setup_new_subscriber'
end.map(&:delete)

Retries

Selects the retry named queue

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.

  r = Sidekiq::RetrySet.new
  r.select do |retri|
    retri.klass == 'Sidekiq::Extensions::DelayedClass' &&
    retri.args[0] == 'User' &&
    retri.args[1] == 'setup_new_subscriber'
  end.map(&:delete)

Workers

Retrieve data about the current set of busy workers.

workers = Sidekiq::Workers.new
workers.size #  => Integer
workers.each do |name, work|
  # name is a unique identifier per Processor instance
  # work is a Hash which looks like:
  # { 'queue' => name, 'run_at' => timestamp, 'payload' => msg }
end

Clone this wiki locally