Rails:取消 Sidekiq 中的预定作业 [英] Rails: Cancelling a scheduled job in Sidekiq
问题描述
所以我的模型中有一个 Sidekiq 工人,它看起来像这样:
So I have a Sidekiq worker in my model which looks like this:
class Perk < ActiveRecord::Base
include Sidekiq::Worker
include Sidekiq::Status::Worker
after_save :update_release_time
def update_release_time
if self.release_time_changed?
#if scheduled job already exists then cancel and reschedule
# Sidekiq::Status.cancel scheduled_job_id
# scheduled_job_id = NotifierWorker.perform_at(time.seconds.from_now, .....)
#elsif scheduled job doesn't exist, then schedule for the first time
# scheduled_job_id = NotifierWorker.perform_at(time.seconds.from_now, .....)
#end
end
end
end
所以基本上,我的代码会检查发布时间是否已更改.如果是,则它必须取消先前计划的作业并在新的时间安排它.我如何实现这一点,即什么会代替我的伪代码?如何检查 scheduled_job_id
是否存在,然后获取其 id?
So basically, my code checks if the release time has changed. If it has, then it has to cancel the previously scheduled job and schedule it at a new time. How do I achieve this i.e what would go in place of my pseudo-code? How do I check if scheduled_job_id
exists and then fetch its id?
推荐答案
API文档概述了您可以做什么,但您确实需要深入研究源代码以发现所有功能.
The API documentation has an overview of what you can do but you really need to dive into the source to discover all the capabilities.
您可以这样做,但效率不高.这是通过 JID 查找预定作业的线性扫描.
You can do this but it won't be efficient. It's a linear scan for find a scheduled job by 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.
这篇关于Rails:取消 Sidekiq 中的预定作业的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!