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

Unexpected behaviour in ActiveJob callbacks #32

Open
shahidkhaliq opened this issue May 24, 2024 · 1 comment
Open

Unexpected behaviour in ActiveJob callbacks #32

shahidkhaliq opened this issue May 24, 2024 · 1 comment

Comments

@shahidkhaliq
Copy link

shahidkhaliq commented May 24, 2024

Hello, thanks for all the hard work on this gem! I've been trying to use this gem to prevent ActiveJob jobs from enqueueing within transactions but have run into some unexpected behavior. If my job looks like the following:

class TestJob < ActiveJob::Base
  before_enqueue do |job|
    puts "1"
  end

  around_enqueue do |job, block|
    puts "2"
    block.call
  end

  include AfterCommitEverywhere

  around_enqueue do |job, block|
    puts "3"
    after_commit { block.call }
  end

  around_enqueue do |job, block|
    puts "4"
    block.call
  end

  def perform; end
end

And I try to enqueue it from within a transaction:

ActiveRecord::Base.transaction do
  TestJob.perform_later
end

It outputs 123 1234 instead of 1234 that I'm expecting. It seems like all the callbacks preceding the callback that has after_commit are run twice instead of once.

This is on Rails 6.0.6.1 with after_commit_everywhere (1.1.0).

Am I doing something wrong here or is this a bug in the gem?

@Envek
Copy link
Owner

Envek commented May 28, 2024

To be honest I'm not sure why this code runs like that.

However, it doesn't seem that your intention to postpone enqueue from callbacks would work at all, as ActiveJob callbacks are built with plain ActiveSupport callbacks and you need to halt callback chain by throwing :abort, see: https://api.rubyonrails.org/classes/ActiveSupport/Callbacks.html#method-i-run_callbacks

Anyway, I would recommend implementing this in some other way, e.g. by redefining the whole enqueue method of ActiveJob job.

Also see #27 (comment)

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