如何在sidekiq中删除作业 [英] how to delete a job in sidekiq
问题描述
我在 Rails 应用中使用 sidekiq.我的应用程序的用户创建了启动 sidekiq 作业的报告.但是,有时用户希望能够取消处理"报告.删除报告很容易,但我还需要能够删除 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
并且每个工作人员都有包含 report_id 的参数,因此我可以确定哪个作业属于哪个报告.但是,我不确定如何实际删除该作业.需要注意的是,我想删除当前繁忙的作业,或者设置在重试中.
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 文档页面删除具有单个 ID 的作业,您需要迭代队列并对其调用 .delete
.
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
还有一个名为 sidekiq-status 的插件,可让您取消单个作业
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
这篇关于如何在sidekiq中删除作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!