(English version is written after Japanese version)
Resque には失敗したジョブを Redis に貯めておいてエラー内容を確認したりジョブをリトライさせられる機能がある。これは Failure Backend というメカニズムで成り立っている。ジョブが失敗したらメールで通知するシンプルな Failure Backend を作りながら、このメカニズムへの理解を深めてみよう。
独自の Failure Backend を利用する手順はこうだ。
Resuqe::Failure::Base
を継承したクラスを作るsave
メソッドを定義してその中で何か面白いことをする (メールを送るとか)- Resque がその Failure Backend を利用するように設定する
例としてジョブの失敗内容をメールする Failure Backend はこのように書ける。
https://gist.github.com/4125211#file_email_notification.rb
この Failure Backend を Resque から利用できるようにするには、以下のように設定する。 Rails であれば config/initializers/resque.rb
あたりに書くのが適切だろう。
https://gist.github.com/4125211#file_resque_config.rb
デフォルトではResque::Failure::Redis
が選ばれているのでジョブが失敗すると Redis に保存される。 Resque::Failure.backend=
に Failure Backend のクラスを代入すればそのクラスが使われるが、 Redis へ保存する仕組みは残しつつメール通知も行いたいなど、複数の Failure Backend を同時に使いたい場合は Resque::Failure::Multiple
を使う。
あとはジョブを失敗させれば、 Redis に保存される従来の機能を活かしたまま、メールで通知を受け取ることができる。
検証に使ったコードは Gist で公開している。
Normally Resque saves failed jobs in Redis, so you can both check the backtrace and retry the job. These feature is made up of Failure Backend mechanism. Let's write a simple email notification class and study about Failure Backend deeply.
You can use your own Failure Backend in three steps.
- Create a class inherited the
Resque::Failure::Base
- Define
save
method then write something you want to do (e.g. sending email) - Tell Resque to use this class as a failure backend
For example, an email notification Failure Backend is below.
https://gist.github.com/4125211#file_email_notification.rb
For to make Resque to use this Failure Backend, make the follow settings. In Rails case, these settings will fit at config/initializers/resque.rb
.
https://gist.github.com/4125211#file_resque_config.rb
The default Failure Backend is Resque::Failure::Redis
so failed jobs are saved to Redis. To use your own class, assign the Failure Backend class to Resque::Failure.backend=
. But probably you want to use both Redis store and email notification, so try to use Resque::Failure::Multiple
and to use multiple failure backends at a same time.
When a job failed, you can receive an email notification without spoiling Redis store.
Full example code is published at Gist.