110

I'm used to using delayed_jobs method of going into the console to see whats in the queue, and the ease of clearing the queue when needed. Are there similar commands in Sidekiq for this? Thanks!

perseverance
  • 6,372
  • 12
  • 49
  • 68

10 Answers10

159

There is an ergonomic API for viewing and managing queues.

It is not required by default.

require 'sidekiq/api'

Here's the excerpt:

# get a handle to the default queue
default_queue = Sidekiq::Queue.new 

# get a handle to the mailer queue
mailer_queue = Sidekiq::Queue.new("mailer") 

# How many jobs are in the default queue?
default_queue.size # => 1001

# How many jobs are in the mailer queue?
mailer_queue.size # => 50

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

You can also get some summary statistics.

stats = Sidekiq::Stats.new

# Get the number of jobs that have been processed.
stats.processed # => 100

# Get 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 # => 1051 
mkirk
  • 3,965
  • 1
  • 26
  • 37
  • 11
    This is the approach that works right now (August 2016). The accepted answer is outdated as of ~2013. – Jan Klimo Aug 05 '16 at 05:37
93

I haven't ever used Sidekiq, so it's possible that there are methods just for viewing the queued jobs, but they would really just be wrappers around Redis commands, since that's basically all Sidekiq (and Resque) is:

# See workers
Sidekiq::Client.registered_workers

# See queues
Sidekiq::Client.registered_queues

# See all jobs for one queue
Sidekiq.redis { |r| r.lrange "queue:app_queue", 0, -1 }

# See all jobs in all queues
Sidekiq::Client.registered_queues.each do |q|
  Sidekiq.redis { |r| r.lrange "queue:#{q}", 0, -1 }
end

# Remove a queue and all of its jobs
Sidekiq.redis do |r| 
  r.srem "queues", "app_queue"
  r.del  "queue:app_queue"
end

Unfortunately, removing a specific job is a little more difficult as you'd have to copy its exact value:

# Remove a specific job from a queue
Sidekiq.redis { |r| r.lrem "queue:app_queue", -1, "the payload string stored in Redis" }

You could do all of this even more easily via redis-cli :

$ redis-cli
> select 0 # (or whichever namespace Sidekiq is using)
> keys * # (just to get an idea of what you're working with)
> smembers queues
> lrange queues:app_queue 0 -1
> lrem queues:app_queue -1 "payload"
bricker
  • 8,911
  • 2
  • 44
  • 54
  • Have you seen a good way to import/migrate resque scheduled jobs to sidekiq's format? Setting sidekiq's namespace to 'resque' doesn't seem to pick up scheduled jobs from what I can tell. Thanks! – Brian Armstrong Aug 28 '13 at 23:58
  • 35
    Some of the solutions provided here are deprecated. – Peter Wagenet Feb 19 '14 at 18:44
  • @BrianArmstrong Sidekiq.redis { |r| r.zrange("schedule", 0, -1, {withscores: true}) } worked for me referencing http://stackoverflow.com/questions/16009639/sidekiq-to-cancel-list-to-scheduled-jobs – Paul Mar 04 '14 at 18:31
  • 2
    As Wagenet pointed out above, these examples are outdated; as mkirk pointed out below, the latest docs w/examples are on the wiki: https://github.com/mperham/sidekiq/wiki/API – odigity Aug 18 '14 at 14:59
  • 3
    `Sidekiq::Client.registered_queues ` has been replaced with `Sidekiq::Queue.all` and `Sidekiq::Client.registered_workers` with `Sidekiq::Workers.new`, see: https://github.com/mperham/sidekiq/blob/52562b715174e447a0f7666136838521fda69214/3.0-Upgrade.md – Martin Svoboda Nov 15 '19 at 15:26
11

if there is any scheduled job. You may delete all the jobs using the following command:

Sidekiq::ScheduledSet.new.clear

if there any queues you wanted to delete all jobs you may use the following command:

  Sidekiq::Queue.new.clear

Retries Jobs can be removed by the following command also:

Sidekiq::RetrySet.new.clear

There are more information here at the following link, you may checkout: https://github.com/mperham/sidekiq/wiki/API

Rubyrider
  • 3,567
  • 1
  • 28
  • 34
8

There is a API for accessing real-time information about workers, queues and jobs.
Visit here https://github.com/mperham/sidekiq/wiki/API

Ranjithkumar Ravi
  • 3,352
  • 2
  • 20
  • 22
2

A workaround is to use the testing module (require 'sidekiq/testing') and to drain the worker (MyWorker.drain).

chikamichi
  • 2,199
  • 1
  • 24
  • 16
2

There were hanged 'workers' in default queue and I was able to see them though web interface. But they weren't available from console if I used Sidekiq::Queue.new.size

irb(main):002:0> Sidekiq::Queue.new.size
2014-03-04T14:37:43Z 17256 TID-oujb9c974 INFO: Sidekiq client with redis options {:namespace=>"sidekiq_staging"}
=> 0

Using redis-cli I was able to find them

redis 127.0.0.1:6379> keys *
    1) "sidekiq_staging:worker:ip-xxx-xxx-xxx-xxx:7635c39a29d7b255b564970bea51c026-69853672483440:default"
    2) "sidekiq_staging:worker:ip-xxx-xxx-xxx-xxx:0cf585f5e93e1850eee1ae4613a08e45-70328697677500:default:started"
    3) "sidekiq_staging:worker:ip-xxx-xxx-xxx-xxx:7635c39a29d7b255b564970bea51c026-69853672320140:default:started"
    ...

The solution was:

irb(main):003:0>  Sidekiq.redis { |r| r.del "workers", 0, -1 }
=> 1

Also in the Sidekiq v3 there is a command

Sidekiq::Workers.new.prune

But for some reason it didn't work for me that day

Ivan Linko
  • 966
  • 7
  • 15
2

And if you want to clear the sidekiq retry queue, it's this: Sidekiq::RetrySet.new.clear

courtsimas
  • 764
  • 1
  • 13
  • 20
2
$ redis-cli
> select 0 # (or whichever namespace Sidekiq is using)
> keys * # (just to get an idea of what you're working with)
> smembers queues
> lrange queue:queue_name 0 -1 # (queue_name must be your relevant queue)
> lrem queue:queue_name -1 "payload"
  • 9
    Don't ever run `keys *` on production unless you use that redis only for sidekiq. Especially don't run it if you have large dataset (cache, etc). Redis is single-threaded and `keys *` blocks -- this can result into a several minutes of downtime on large datasets (several Gbs). – timurb Feb 09 '17 at 07:34
1

Rake task for clear all sidekiq queues:

namespace :sidekiq do
  desc 'Clear sidekiq queue'
  task clear: :environment do
    require 'sidekiq/api'
    Sidekiq::Queue.all.each(&:clear)
  end
end

Usage:

rake sidekiq:clear
Darkside
  • 659
  • 8
  • 20
0

This is not a direct solution for the Rails console, but for a quick monitoring of the Sidekiq task count and queue size, you can use sidekiqmon binary that ships with Sidekiq 6+:

$ sidekiqmon
Sidekiq 6.4.2
2022-07-25 11:05:56 UTC

---- Overview ----
  Processed: 20,313,347
     Failed: 57,120
       Busy: 9
   Enqueued: 17
    Retries: 0
  Scheduled: 37
       Dead: 2,382

---- Processes (1) ----
36f993209f93:15:a498f85c6a12 [server]
  Started: 2022-07-25 10:49:43 +0000 (16 minutes ago)
  Threads: 10 (9 busy)
   Queues: default, elasticsearch, statistics

---- Queues (3) ----
NAME             SIZE  LATENCY
default             0     0.00
elasticsearch      17     0.74
statistics          0     0.00
slhck
  • 36,575
  • 28
  • 148
  • 201