モンモンブログ

技術的な話など

DelayedJob による非同期処理の失敗を exception_notification でエラー通知させる

delayed_jobs による非同期処理時に、exception_notification によるエラー通知メール送信を行う - Qiita

こちらの記事を参考に DelayedJob のエラー通知を実装していたんですが、 Rails5 で alias_method_chain が Deprecated になってしまったので Module.prepend を使って書き直しました。

config/initializers/delayed_job.rb

module NotifyWhenDelayedJobFailed
  def handle_failed_job(job, error)
    # オリジナルの Delayed::Worker#handle_failed_job を実行
    super
    # 追加処理としてメール通知を行う
    ExceptionNotifier.notify_exception(error)
  end
end

class Delayed::Worker
  # 定義したモジュールを prepend
  prepend NotifyWhenDelayedJobFailed
end

Module.prepend については

» Ruby2.0のModule#prependは如何にしてalias_method_chainを撲滅するのか!? TECHSCORE BLOG

こちらが分かりやすかったです。