我需要能够为模型对象找到排队和/或工作作业和/或失败的作业,比如说,当模型对象被销毁时,我们想要查找所有模型对象并决定不删除或销毁作业(有条件的)。如何查找ActiveRecord模型对象的关联Resque作业?
有没有推荐的方法来做到这一点之前,我重新发明轮子?
例子:
如果你想创建一个before_destroy
回调时破坏的对象被销毁(排队和失败的作业),且仅当没有工作职位
的一些伪代码摧毁所有作业我在想什么在这个例子中使用的情况下,要做到:
报表模型
class Report < ActiveRecord::Base
before_destroy :check_if_working_jobs, :destroy_queued_and_failed_jobs
def check_if_working_jobs
# find all working jobs related to this report object
working_jobs = ProcessReportWorker.find_working_jobs_by_report_id(self.id)
return false unless working_jobs.empty?
end
def destroy_queued_and_failed_jobs
# find all jobs related to this report object
queued_jobs = ProcessReportWorker.find_queued_jobs_by_report_id(self.id)
failed_jobs = ProcessReportWorker.find_failed_jobs_by_report_id(self.id)
# destroy/remove all jobs found
(queued_jobs + failed_jobs).each do |job|
# destroy the job here ... commands?
end
end
end
报表处理工作呃类为resque/redis支持作业
class ProcessReportWorker
# find the jobs by report id which is one of the arguments for the job?
# envisioned as separate methods so they can be used independently as needed
def self.find_queued_jobs_by_report_id(id)
# parse all jobs in all queues to find based on the report id argument?
end
def self.find_working_jobs_by_report_id(id)
# parse all jobs in working queues to find based on the report id argument?
end
def self.find_failed_jobs_by_report_id(id)
# parse all jobs in failed queue to find based on the report id argument?
end
end
这种方法正在跟踪什么需要发生?
以上缺失的部分是什么,通过模型对象ID找到排队或工作的作业,然后将其销毁?
是否已有方法查找和/或销毁我在文档或我的搜索中遗漏的关联模型对象ID?
更新:修改了使用示例,只使用working_jobs作为检查是否应该删除的一种方法vs建议我们也会尝试删除working_jobs。 (因为删除工作任务比简单地删除重拨键项更涉及)