我需要在delayed_job
处理一个任务后更新一个模型,例如:
foo.delay.something
完成后something
,我需要更新 foo 对象,实现此目的的最佳方法是什么?我正在考虑在Delayed::Backend::ActiveRecord::Job
类上编写回调,但应该有一些更清洁和更好的方法来做到这一点。
我需要在delayed_job
处理一个任务后更新一个模型,例如:
foo.delay.something
完成后something
,我需要更新 foo 对象,实现此目的的最佳方法是什么?我正在考虑在Delayed::Backend::ActiveRecord::Job
类上编写回调,但应该有一些更清洁和更好的方法来做到这一点。
我会在 #foo 方法的末尾更新它:
def foo
# do work here
update_attribute :processed, true
end
我不明白你为什么不把它作为已经作用于对象的工作的一部分。
按照建议更新记录很好,但这只是解决方案的一部分......
如果我想更多地控制失败时的操作,回调会有所帮助..即:
Delayed::Job.enqueue InstructionRequestJob.new( p1, p2 )
InstructionRequestJob perform
- perform a task on a remote server
- get a response
- case response
when OK
update attribute ( as suggested)
else
# how many attempts ?
if too_many_attempts
update attribute
destroy the job
else
reschedule the job for another attempt
- end