问题
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:
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.
queue = Sidekiq::Queue.all
queue.each do |job|
p job.klass, job.args, job.jid
end
for a specific queue change this to Sidekiq::Queue.new('queue_name')
similarly you can get all scheduled jobs using Sidekiq::ScheduledSet.new
回答2:
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"]}
来源:https://stackoverflow.com/questions/48348723/sidekiq-list-all-jobs-queued-running