Is there a way to add a callback to a failed attempt in Delayed::Job?
Occasion开发者_StackOverflow社区ally a job will increment an attempt and not perform the method regenerate_styles
. I've noticed that if I pass a method rengerate_styles
manually to whatever the failed job is - 100% of the time always fixes the problem.
So if the image is not processing, and I pass :
Photo.find(image).regenerate_styles!
It works as expected.
Is it possible to write a callback that detects a failed attempt, and runs a method? Or is there something in this code distinguishing to why it would not be processing some of the time.
the model
after_save do |image|
if image.source_changed?
Delayed::Job.enqueue PhotoJob.new(image.id)
end
end
def regenerate_styles!
self.photo.reprocess!
self.processing = false
self.save(false)
end
the photojob
class PhotoJob < Struct.new(:image_id)
def perform
Photo.find(self.image_id).regenerate_styles!
end
end
Starting from DelayedJob 2.1 there is an hook called :error. Just define a method called error in your job definition.
def error(job, error)
# do whatever you want here
end
精彩评论