Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Recommended ActiveJob error handling is no longer relevant as of Rails 7.1.3 #775

Open
jonny-ras opened this issue Aug 29, 2024 · 3 comments

Comments

@jonny-ras
Copy link

After we upgraded our app to Rails 7.1.3, we noticed that errors were no longer going to our configured DLQs.

We had our set up following the recommended setup, from this comment: #553 (comment)

In our ApplicationJob class we have the following, but we were never seeing any raised exceptions go to the DLQ.

retry_on ::StandardError, wait: 30.seconds, attempts: 3 do |job, exception|
   capture_exception(job, exception) unless ::Rails.env.development?
   job.log_error(message: "Error performing job", error: exception, p: job.arguments.first) if job.class.instance_variable_get(:@on_failure)
end

After a lot of digging into ActiveJob, I found this change: https://github.com/rails/rails/pull/48010/files

The relevant part of it is here: https://github.com/rails/rails/pull/48010/files#diff-c017408d8f8e9b0a914da28fdcebc291c5fba624174fb9ca5bec3b4fc1c61d51

According to the Rails docs, this after_discard block is meant to re-raise the exception, but from my testing it doesn't. If the after_discard block doesn't raise the exception, the exception is not automatically re-raised, and so Shoryuken thinks everything is OK, and the message doesn't DLQ.

I've fixed this by hooking into the ActiveSupport::Notification active_job.retry_stopped event, and re-raising the exception there.

# config/initializers/event_subscribers.rb
::ActiveSupport::Notifications.subscribe("retry_stopped.active_job", ::EventSubscribers::ActiveJobSubscriber.new)
# frozen_string_literal: true

# This class exists to circumvent ActiveJob not re-raising exceptions in the retry_on block.
# When those exceptions are not re-raised, the error is not bubbled up to Shoryuken, which means the message does not get DLQ'd.
#

module EventSubscribers
  class ActiveJobSubscriber
    def call(*args)
      event = ActiveSupport::Notifications::Event.new(*args)

      raise event.payload[:error] if event.name == "retry_stopped.active_job"
    end
  end
end

This issue is to mainly ask if this is indeed correct, should we be re-raising to Shoryuken for it to DLQ, and if not, how should I handle this?

I also assume the Rails code is broken, as it is not automatically re-raising the error for me, which is obviously not something to be handled by this gem, but it seems to be the current state of affairs (having checked Rails main, no changes there yet, I'll probably open an issue).

Copy link

This issue is now marked as stale because it hasn't seen activity for a while. Add a comment or it will be closed soon.

@github-actions github-actions bot added the Stale label Oct 29, 2024
@jonny-ras
Copy link
Author

Still relevant as I see it

@phstc
Copy link
Collaborator

phstc commented Oct 29, 2024

@jonny-ras I can no longer maintain this project. I updated the README with additional info 946ed1d

@github-actions github-actions bot removed the Stale label Oct 30, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants