5

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 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.

Saurabh
  • 71,488
  • 40
  • 181
  • 244
jpemberthy
  • 7,473
  • 8
  • 44
  • 52

3 Answers3

3

I would just updated it at the end of the #foo method:

def foo
  # do work here
  update_attribute :processed, true
end
bkeepers
  • 2,135
  • 2
  • 16
  • 9
1

I don't understand why you wouldn't do it as part of the job that's already acting on the object.

Joe Martinez
  • 854
  • 4
  • 9
  • Yes I could `delay` the entire flow, but I'd like to know how to trigger an `after_processed` callback. – jpemberthy Jul 15 '10 at 22:15
  • I have a similar situation, but in my case, it works to just queue another job at the end of the first. Also, it's important to note that the jobs that get created are ActiveRecord objects themselves, so if you want to add lifecycle callbacks there, you can do so. – Joe Martinez Jul 19 '10 at 13:02
  • Hey thanks, personally, for this specific case, I like more the idea of adding callbacks to the Job instances rather than enqueue a job after another, So, I'm going for the callback on the Job instances :) – jpemberthy Jul 19 '10 at 18:03
0

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
iwasrobbed
  • 46,496
  • 21
  • 150
  • 195