42

Is there a way to get a list of all the jobs currently in the queue and running? Basically, I want to know if a job of given class is already there, I don't want to insert my other job. I've seen other option but I want to do it this way.

I can see here how to get the list of jobs in the queue.

queue = Sidekiq::Queue.new("mailer")
queue.each do |job|
  job.klass # => 'MyWorker'
end

from what I understand this will not include processing/running jobs. Any way to get them?

1

4 Answers 4

68

if you want to list all currently running jobs from console, try this

workers = Sidekiq::Workers.new
workers.each do |_process_id, _thread_id, work|
  p work
end

a work is a hash.

to list all queue data.

queues = Sidekiq::Queue.all
queues.each do |queue|
  queue.each do |job|
    p job.klass, job.args, job.jid
  end
end

for a specific queue change this to Sidekiq::Queue.new('queue_name')

similarly you can get all scheduled jobs using Sidekiq::ScheduledSet.new

3
  • 9
    this answer is possibly outdated: I get NoMethodError: undefined method klass'` Dec 6, 2019 at 0:48
  • job.klass is still available in 2020. check github.com/mperham/sidekiq/blob/…
    – Haseeb A
    Nov 2, 2020 at 5:27
  • It seems like calling job.jid generates the following error: NoMethodError: undefined method jid' for #<Sidekiq::Queue:0x00007ff144076178>` Is this because we're trying to get a job ID from a queue?
    – LewlSauce
    Nov 2, 2020 at 16:01
10

running jobs:

Sidekiq::Workers.new.each do |_process_id, _thread_id, work|
  p work
end

queued jobs across all queues:

Sidekiq::Queue.all.each do |queue|
  # p queue.name, queue.size
  queue.each do |job|
    p job.klass, job.args
  end
end
2
  • 5
    A bit of clarification: Sidekiq::Queue.all would only output jobs in progress, not the scheduled ones. As the accepted answer states, you need to use Sidekiq::ScheduledSet.new to get the scheduled jobs.
    – wondersz1
    Jun 29, 2020 at 8:32
  • 2
    @sharq Could you consider removing this answer as it is the same as the accepted?
    – Ulysse BN
    Sep 21, 2021 at 16:47
4

Assuming you passed the Hash as the argument to Sidekiq when you enqueued.

args = {
  "student_id": 1,
  "student_name": "Michael Moore"
    }

YourWorker.perform_in(1.second,args)

Then anywhere from your application, you could retrieve it as following

      ss = Sidekiq::ScheduledSet.new
      student_id_list = ss.map{|job| job['args'].first["student_id"]}
1

I am using this in the ApplicationJob to check if there is already a job in the queue with same name/arguments and prevent from queue it twice

apps/jobs/application_job.rb

class ApplicationJob < ActiveJob::Base
    

    # Check if there is the same job already queued
    around_enqueue do |job, block|
        
        existing_queued_jobs = list_queued_jobs(job.class, job.queue_name, job.arguments)
        if existing_queued_jobs.size == 0
            block.call # this will enqueue your job
        else
            puts "JOB not enqueue because already queued (#{job.class}, #{job.queue_name}, #{job.arguments})"
        end
    end


    def list_queued_jobs(job_class, queue_name,  arguments)
        found_jobs = []
        queues = Sidekiq::Queue.all
        queues.each do |queue|
            queue.each do |job|
                job.args.each do |arg|
                     found_jobs << job if arg['job_class'].to_s == job_class.to_s && arg['queue_name'] == queue_name && arg['arguments'] == arguments
                end
            end
        end
        return found_jobs
    end
   


end
1

Your Answer

Reminder: Answers generated by Artificial Intelligence tools are not allowed on Stack Overflow. Learn more

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.