Callback after delayed_job process job
I need to update a model after delayed_job
processes a task on it, e.g:
foo.delay.something
After something
is done, I need to update the foo开发者_JAVA百科 object, what is the best way to achieve this? I was thinking in coding a callback on the Delayed::Backend::ActiveRecord::Job
class, but there's should be something cleaner and better to do this.
I would just updated it at the end of the #foo method:
def foo
# do work here
update_attribute :processed, true
end
I don't understand why you wouldn't do it as part of the job that's already acting on the object.
updating a record as suggested is fine , but it's only part of the solution ...
a callback would help if I want more control on what to do if it fails .. i.e. :
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
精彩评论