I am using sidekiq in my rails app. Users of my app create reports that start a sidekiq job. However, sometimes users want to be able to cancel "processing" reports. Deleting the report is easy but I also need to be able to delete the sidekiq job as well.
So far I have been able to get a list of workers like so:
workers = Sidekiq::Workers.new
and each worker has args that include a report_id so I can identify which job belongs to which report. However, I'm not sure how to actually delete the job. It should be noted that I want to delete the job whether it is currently busy, or set in retry.
Sidekiq handles concurrency by using multiple threads in its process. This way, it can process multiple jobs at once, each thread processing one job at a time. By default, Sidekiq uses 10 threads per process. You can configure it to use more threads, thus increasing concurrency.
The only consistent fix I've found is rebooting. kill 'process_id' worked fine, to kill the process. Though then restarting sidekiq it can't find redis. Moreover, 'kill -term pid' will cause it to shut down as gracefully as it can in the next 10 seconds.
Your failed jobs will be visible via a Failures tab in the Web UI. If you have not previously used the Web UI, it is a part of the Sidekiq gem. See Sidekiq's docs about Web UI here.
If you build a web application you should minimize the amount of time spent responding to every user; a fast website means a happy user.
According to this Sidekiq documentation page to delete a job with a single id you need to iterate the queue and call .delete
on it.
queue = Sidekiq::Queue.new("mailer") queue.each do |job| job.klass # => 'MyWorker' job.args # => [1, 2, 3] job.delete if job.jid == 'abcdef1234567890' end
There is also a plugin called sidekiq-status that provides you the ability to cancel a single job
scheduled_job_id = MyJob.perform_in 3600 Sidekiq::Status.cancel scheduled_job_id #=> true
The simplest way I found to do this is:
job = Sidekiq::ScheduledSet.new.find_job([job_id])
where [job_id] is the JID that pertains to the report. Followed by:
job.delete
I found no need to iterate through the entire queue as described by other answers here.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With