# In config/environments/production.rb or config/initializers/delayed_job.rb# Optional but recommended for less future surprises.# Fail at startup if method does not exist instead of later in a background job.[[ExceptionNotifier::Notifier,:background_exception_notification]].eachdo|object,method_name|raiseNoMethodError,"undefined method `#{method_name}' for #{object.inspect}"unlessobject.respond_to?(method_name,true)end# Chain delayed job's handle_failed_job method to do exception notificationDelayed::Worker.class_evaldo.defhandle_failed_job_with_notification(job,error)handle_failed_job_without_notification(job,error)# only actually send mail in productionifRails.env.production?# rescue if ExceptionNotifier fails for some reasonbeginExceptionNotifier::Notifier.background_exception_notification(error)rescueException=>eRails.logger.error"ExceptionNotifier failed: #{e.class.name}: #{e.message}"e.backtrace.eachdo|f|Rails.logger.error" #{f}"endRails.logger.flushendendend.alias_method_chain:handle_failed_job,:notification.end