Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

sidekiq to cancel list to scheduled jobs

Tags:

ruby

sidekiq

I have several scheduled jobs running like this:

MyWorker.perform_at(3.hours.from_now, 'mike', 1)

I am wondering, if later, say an hour later, I feel like I want to cancel this job, how would I go about doing that?

like image 935
0xSina Avatar asked Apr 15 '13 07:04

0xSina


People also ask

What is a Sidekiq process?

Sidekiq server process pulls jobs from the queue in Redis and processes them. Like your web processes, Sidekiq boots Rails so your jobs and workers have the full Rails API, including Active Record, available for use. The server will instantiate the worker and call perform with the given arguments.


2 Answers

I've recently written a bit of code to handle this, it's available in my branch of the sidekiq-status gem. You can view it, or use it here: https://github.com/Robinson7D/sidekiq-status

(You would have to use that as the git: information in the gemfile, currently, until the main fork of the project implements this)

To use it, first you store the job_identifier:

job_identifier = MyWorker.perform_at(3.hours.from_now, 'mike', 1)

And when you wish to destroy it you call the Sidekiq::Status.cancel method:

Sidekiq::Status.cancel job_identifier 

Edit: since writing this post, my code's been accepted into the main fork of Sidekiq::Status - https://github.com/utgarda/sidekiq-status . You no longer have to use my fork. On Utgarda's fork you would trigger it by calling unschedule, instead of cancel:

Sidekiq::Status.unschedule job_identifier

Further: you can also delete jobs using the standard Sidekiq gem as explained here: https://github.com/mperham/sidekiq/wiki/API (though for their methods you require the unix-timestamp of when the job is scheduled for - you cannot delete with only the job's id; if you wish to delete a job without the timestamp, the Sidekiq::Status method may be right for you).

However, instead of the ways they outline in the wiki I would recommend something along the lines of Sidekiq::ScheduledSet.new().delete(unix_timestamp, jid) if you want to delete just one job.)

like image 74
DRobinson Avatar answered Oct 18 '22 09:10

DRobinson


As far as I know Sidekiq has no way to cancel a schedules job for now. I wrote a service which lets me cancel my scheduled mails because of that. But you cancel jobs fairly easy with redis commands:

You get schedules jobs like this:

schedules_jobs = $redis.zrange "schedule", 0, -1, {withscores: true}

Then you get an array of jobs and you pick one job and and cancel it like this:

$redis.zrem "schedule", schedules_jobs[0]
like image 39
Matjaz Muhic Avatar answered Oct 18 '22 09:10

Matjaz Muhic