sidekiq to cancel list to scheduled jobs

跟風遠走 提交于 2020-01-01 08:03:33

问题


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?


回答1:


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.)




回答2:


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]


来源:https://stackoverflow.com/questions/16009639/sidekiq-to-cancel-list-to-scheduled-jobs

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!