开发者

Callback after delayed_job process job

开发者 https://www.devze.com 2023-01-07 18:12 出处:网络
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 t

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
0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号