collectiveidea/delayed_job — pull request #721 by Nowaker (2014-10-07 13:51:45+00:00)
Title: pass the exception to failure hook

I use delayed_job to run background tasks in [VirtKick](https://www.virtkick.io/), example tasks are creating a new machine, creating a new disk, mounting an ISO, etc. Some I/O related tasks can't be performed when load is high, libvirt is just rejecting the call. Therefore, I set some big `max_attempts` and `reschedule_at` in 2 seconds. That's fine.

```ruby
module Bugsnagable
def error job, e
unless Rails.env.test?
puts e.message
puts e.backtrace.map { |e| ' ' + e }.join "\n"
end
Bugsnag.notify_or_ignore e
end
end
```
I catch every unhandled exception with [Bugsnag](https://bugsnag.com/) so as to know of every exception. The problem is this reports too much. If the job fails for the first time and succeeds for the second, one exception will be reported anyway. One way would be to check if `attempt == max_attempts` before reporting to Bugsnag, but I believe the best way is to introduce `def failure(job, exception)`.

I didn't write any specs at this point - tell me if you're OK with `def failure job, exception` first. :) Please also see my questions directly in the code.