Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

how to delete a job in sidekiq

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.

like image 525
Matthew Berman Avatar asked Jan 13 '14 21:01

Matthew Berman


People also ask

How many jobs can Sidekiq handle?

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.

How do I stop Sidekiq?

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.

How do I see failed jobs on Sidekiq?

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.

Should I use Activejob Sidekiq?

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.


2 Answers

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 
like image 183
Simone Carletti Avatar answered Sep 20 '22 22:09

Simone Carletti


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.

like image 29
Rick Avatar answered Sep 18 '22 22:09

Rick