0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

ActiveJobのrescue_fromから例外が発生してもretry_onが動かない

Posted at

rspecretry_onのテスト書いて、ジョブクラスをリファクタしたら動かなくなりました。

環境

  • 以下の環境で発生しました。
- Version
ruby 3.3.5
rails 7.2.2
rspec 3.13

本題

  • ジョブクラスでrescue_fromを使っていると、retry_onが少なくとも動きませんでした。
  • 本番環境で再現するかわかりませんが、気をつけないといけないポイントかと思います。

retry_on が動作する

class BaseJob < ApplicationJob
  queue_as :default

  retry_on RetryError, wait: 30.seconds, attempts: 4

  def perform
    # 何かしらの処理
  rescue ActiveRecord::LockWaitTimeout => e
    raise e if e.message.exclude?('try restarting transaction')

    raise RetryError
  end
end

retry_on が動作しない

class BaseJob < ApplicationJob
  queue_as :default

  retry_on RetryError, wait: 30.seconds, attempts: 4
  rescue_from ActiveRecord::LockWaitTimeout, with: :error_handler

  def perform
    # 何かしらの処理
  end

  def error_handler(e)
    raise e if e.message.exclude?('try restarting transaction')
    raise RetryError
  end 
end
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?