5

我需要在delayed_job处理一个任务后更新一个模型,例如:

foo.delay.something

完成后something,我需要更新 foo 对象,实现此目的的最佳方法是什么?我正在考虑在Delayed::Backend::ActiveRecord::Job类上编写回调,但应该有一些更清洁和更好的方法来做到这一点。

4

3 回答 3

3

我会在 #foo 方法的末尾更新它:

def foo
  # do work here
  update_attribute :processed, true
end
于 2010-07-16T12:55:57.793 回答
1

我不明白你为什么不把它作为已经作用于对象的工作的一部分。

于 2010-07-15T20:30:05.240 回答
0

按照建议更新记录很好,但这只是解决方案的一部分......

如果我想更多地控制失败时的操作,回调会有所帮助..即:

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
于 2011-05-20T10:43:38.630 回答