Ruby-on-rails 如何在sidekiq中删除作业
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/21101253/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
how to delete a job in sidekiq
提问by Matthew Berman
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.
我在我的 rails 应用程序中使用 sidekiq。我的应用程序的用户创建了启动 sidekiq 作业的报告。但是,有时用户希望能够取消“处理”报告。删除报告很容易,但我还需要能够删除 sidekiq 作业。
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.
并且每个工作人员都有包含 report_id 的参数,因此我可以确定哪个作业属于哪个报告。但是,我不确定如何实际删除该作业。需要注意的是,我想删除当前繁忙的作业,或者设置在重试中。
回答by Simone Carletti
According to this Sidekiq documentation pageto delete a job with a single id you need to iterate the queue and call .deleteon it.
根据此Sidekiq 文档页面,要删除具有单个 ID 的作业,您需要迭代队列并调用.delete它。
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-statusthat provides you the ability to cancel a single job
还有一个名为sidekiq-status的插件,可以让您取消单个作业
scheduled_job_id = MyJob.perform_in 3600
Sidekiq::Status.cancel scheduled_job_id #=> true
回答by Rick
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_id] 是与报告相关的 JID。其次是:
job.delete
I found no need to iterate through the entire queue as described by other answers here.
我发现不需要像这里的其他答案所描述的那样遍历整个队列。
回答by KimiGao
I had the same problem, but the difference is that I needed to cancel a scheduled job, and my solution is:
我遇到了同样的问题,但不同的是我需要取消预定的作业,我的解决方案是:
Sidekiq::ScheduledSet.new.each do |_job|
next unless [online_jid, offline_jid].include? _job.jid
status = _job.delete
end
回答by thekingoftruth
If you want to cancel a scheduled job, I'm not sure about @KimiGao's answer, but this is what I adapted from Sidekiq's current API documentation:
如果你想取消预定的工作,我不确定@KimiGao 的回答,但这是我从 Sidekiq 的当前 API 文档中改编的:
jid = MyCustomWorker.perform_async
r = Sidekiq::ScheduledSet.new
jobs = r.select{|job| job.jid == jid }
jobs.each(&:delete)
Hope it helps.
希望能帮助到你。
回答by Micha? Kurek
You can delete sidekiq job filtering by worker class and args:
您可以通过工人类和参数删除 sidekiq 作业过滤:
class UserReportsWorker
include Sidekiq::Worker
def perform(report_id)
# ...
end
end
jobs = Sidekiq::ScheduledSet.new.select do |retri|
retri.klass == "UserReportsWorker" && retri.args == [42]
end
jobs.each(&:delete)
回答by hobbydev
Or you can use sidekiq page on rails server.
或者您可以在 rails 服务器上使用 sidekiq 页面。
For example, http://localhost:3000/sidekiq, you can stop/remove the sidekiq jobs.
例如,http://localhost:3000/sidekiq,您可以停止/删除 sidekiq 作业。
Before that, you have to updates the routes.rb.
在此之前,您必须更新 routes.rb。
require 'sidekiq/web'
mount Sidekiq::Web => '/sidekiq'
回答by Céline Martinet Sanchez
I had the same problem.
I solved it by registering the job id when I initialize it and by creating another function cancel!to delete it.
我有同样的问题。我通过在初始化时注册作业 id 并创建另一个函数cancel!来删除它来解决它。
Here is the code:
这是代码:
after_enqueue do |job|
sidekiq_job = nil
queue = Sidekiq::Queue.new
sidekiq_job = queue.detect do |j|
j.item['args'][0]['job_id'] == job.job_id
end
if sidekiq_job.nil?
scheduled = Sidekiq::ScheduledSet.new
sidekiq_job = scheduled.detect do |j|
j.item['args'][0]['job_id'] == job.job_id
end
end
if sidekiq_job.present?
booking = job.arguments.first
booking.close_comments_jid = sidekiq_job.jid
booking.save
end
end
def perform(booking)
# do something
end
def self.cancel!(booking)
queue = Sidekiq::Queue.new
sidekiq_job = queue.find_job(booking.close_comments_jid)
if sidekiq_job.nil?
scheduled = Sidekiq::ScheduledSet.new
sidekiq_job = scheduled.find_job(booking.close_comments_jid)
end
if sidekiq_job.nil?
# Report bug in my Bug Tracking tool
else
sidekiq_job.delete
end
end
回答by Pushp Raj Saurabh
There is simple way of deleting a job if you know the job_id:
如果您知道 job_id,有一种简单的方法可以删除作业:
job = Sidekiq::ScheduledSet.new.find_job(job_id)
begin
job.delete
rescue
Rails.logger.error "Job: (job_id: #{job_id}) not found while deleting jobs."
end

