所以我的模型中有一个 Sidekiq worker ,它看起来像这样:
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?
最佳答案
API文档概述了您可以做什么,但您确实需要深入了解源代码以发现所有功能。
你可以这样做,但效率不高。这是通过 JID 查找预定作业的线性扫描。
require 'sidekiq/api'
Sidekiq::ScheduledSet.new.find_job(jid).try(:delete)
或者,您的工作可以查看它在运行时是否仍然相关。
关于ruby-on-rails - rails : Cancelling a scheduled job in Sidekiq,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24180899/