简体   繁体   English

在Rails中取消了一个被拖延的Sidekiq工作

[英]cancelling a sheduled Sidekiq job in Rails

Some Sidekiq jobs in my app are scheduled to change the state of a resource to cancelled unless a user responds within a certain timeframe. 我的应用程序中的某些Sidekiq作业计划将资源状态更改为cancelled除非用户在特定时间范围内响应。 There is a lot of information about how to best accomplish this task, but none of it actually cancels the job. 有一个大量 信息, 关于如何最好地完成这个任务,但没有它实际上取消作业。

To cancel a job, the code in the wiki says: 要取消作业,wiki中的代码说:

class MyWorker
 include Sidekiq::Worker

 def perform(thing_id)
  return if cancelled?
  thing = Thing.find thing_id
  thing.renege!
 end

 def cancelled?
  Sidekiq.redis {|c| c.exists("cancelled-#{jid}") }
 end

 def self.cancel!(jid)
  Sidekiq.redis {|c| c.setex("cancelled-#{jid}", 86400, 1) }
 end
end

Yet here it's suggested that I do something like 然而, 这里建议我做类似的事情

 def perform(thing_id)
  thing = Thing.find thing_id
  while !cancel?(thing)
   thing.ignore!
  end
 end

 def cancel?(thing_id)
  thing = Thing.find thing_id
  thing.matched? || thing.passed?
 end

What's confusing about this and similar code on the wiki is none of it actually cancels the job. 令人困惑的是,维基上的这个和类似代码实际上并没有取消这项工作。 The above example just performs an update on thing if cancelled? 如果cancelled? ,上面的例子只对thing进行更新cancelled? returns false (as it should), but doesn't cancel if and when it returns true in the future. 返回false (应该如此),但是如果以后它返回true则不会取消。 It just fails with an aasm transition error message and gets sent to the RetrySet. 它只是失败并带有aasm转换错误消息并被发送到RetrySet。 Calling MyWorker.cancel! jid 调用MyWorker.cancel! jid MyWorker.cancel! jid in model code throws an undefined variable error. 模型代码中的MyWorker.cancel! jid抛出一个未定义的变量错误。 How can I access that jid in the model? 如何在模型中访问该jid? How can actually cancel or delete that specific job? 如何实际取消删除该特定工作? Thanks! 谢谢!

# The wiki code
class MyWorker
 include Sidekiq::Worker

 def perform(thing_id)
  return if cancelled?

  # do actual work
 end

 def cancelled?
  Sidekiq.redis {|c| c.exists("cancelled-#{jid}") }
 end

 def self.cancel!(jid)
  Sidekiq.redis {|c| c.setex("cancelled-#{jid}", 86400, 1) }
 end
end

# create job
jid = MyWorker.perform_async("foo")

# cancel job
MyWorker.cancel!(jid)

You can do this but it won't be efficient. 你可以这样做,但效率不高。 It's a linear scan for find a scheduled job by JID. 这是JID查找预定作业的线性扫描。

require 'sidekiq/api' Sidekiq::ScheduledSet.new.find_job(jid).try(:delete) Alternatively your job can look to see if it's still relevant when it runs. 要求'sidekiq / api'Sidekiq :: ScheduledSet.new.find_job(jid).try(:delete)或者你的工作可以查看它在运行时是否仍然相关。

Ok, so turns out I had one question already answered. 好的,事实证明我已经回答了一个问题。 One of the code sets I included was a functionally similar version of the code from the wiki. 我包含的代码集之一是来自wiki的功能类似的代码版本。 The solution to the other question ("how can I access that jid in the model?") seems really obvious if you're not still new to programming, but basically: store the jid in a database column and then retrieve/update it whenever it's needed! 另一个问题的解决方案(“我如何访问模型中的jid?”)似乎非常明显,如果你还不是编程新手,但基本上:将jid存储在数据库列中,然后每当检索/更新它它需要! Duh! 咄!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM