Skip to content

Deprecate setting config.active_job.enqueue_after_transaction_commit - #53375

Merged
rafaelfranca merged 1 commit into
mainfrom
rm-queue-adapter
Oct 19, 2024
Merged

rafaelfranca merged 1 commit into
mainfrom
rm-queue-adapter

Conversation

@rafaelfranca

Copy link
Copy Markdown
Member

Also change the configuration to be a boolean instead of a symbol.

The value of this setting isn't delegated to the queue adapter anymore, since the behavior that this setting controls is too dangerous to leave this change of behavior as something that users don't control.

Fixes #52675.

Also change the configuration to be a boolean instead of a symbol.

The value of this setting isn't delegated to the queue adapter anymore,
since the behavior that this setting controls is too dangerous to
leave this change of behavior as something that users don't control.

Fixes #52675.
@rosa

rosa commented Nov 23, 2024

Copy link
Copy Markdown
Member

@rafaelfranca, just in case it was an oversight or unintended: it seems this PR changed the previous default value in Rails 7.2 for popular adapters like Sidekiq or Resque, and Solid Queue too (not popular but with the same behaviour 😅 ), if the user hadn't touched anything. I realised this because of rails/solid_queue#409. In Rails 7.2, we had the following logic:

So, if someone didn't touch enqueue_after_transaction_commit in Rails 7.2 and they were using Sidekiq, Resque or Solid Queue, their jobs would have been automatically deferred until all transactions were committed. With this PR this changed because the adapter is not checked anymore, and the default value for enqueue_after_transaction_commit is false. Or am I reading something wrongly? 🤔

In any case this is ok, for Solid Queue it meant updating the README, that was based on the previous behaviour, but I thought I'd double-check just in case it wasn't intentional.

@janko

janko commented Dec 18, 2024 •

Copy link
Copy Markdown
Contributor

@rafaelfranca This change is missing from the Active Job 8.0 changelog.

This is a breaking change for apps using Sidekiq and relying on the adapter default, as now jobs are no longer queued after transaction commit by default.

@rafaelfranca

Copy link
Copy Markdown
Member Author

It was intended. We decided to not let the adapter decide it #52659 (comment).

I realize that broke the behavior for adapter that thought they were in charge. I'll add to the release notes.

But now that I think about it, not having this config being global kinda of defeats the automatic enqueuing of jobs after transaction feature.

Maybe I misunderstood @dhh's comment #52659 (comment).

@dhh do you think we should allow this a global config and always defer enqueuing of jobs to after the commit? meaning default behavior is enqueue_after_transaction_commit = true

@dhh

dhh commented Dec 19, 2024 •

Copy link
Copy Markdown
Member

Yes, by default, all jobs should enqueue after the commit. And this should be a setting that Active Job controls. And it should also be downplayed as something people might want to change. I don't think people should change this. In fact, if we didn't have the history of going back and forth on it, I wouldn't even have made it a setting.

@rafaelfranca

Copy link
Copy Markdown
Member Author

Ok. I think we can do that. For 8.0 the ship has sailed since false was the default in case the adapter didn't implement.

@ghiculescu

ghiculescu commented Feb 4, 2025 •

Copy link
Copy Markdown
Member

@rafaelfranca @dhh something to consider as this config is removed is how it should interact with gems that provide their own active jobs. For example the noticed gem provides Noticed::ApplicationJob which descends from ActiveJob::Base.

  • When enqueue_after_transaction_commit was a config, changing it would also change behaviour for the noticed jobs without you needing to do anything extra. ✅
  • Now that enqueue_after_transaction_commit is not a config, but can be set on jobs, you need to also set it on all gem-provided jobs to ensure consistent behaviour across the app. ⚠️
  • This only matters if you want behaviour different to the default, but given that the default has changed between Rails versions that is going to be something lots of people want?

Now I think about it, the same issue applies, for example, to the Active Storage built in jobs.


We are working around this with an initializer:

Rails.application.config.after_initialize do
  ActiveSupport.on_load(:active_job) do
    ActiveJob::Base.enqueue_after_transaction_commit = true
  end
end

And a test:

  test "enqueue_after_transaction_commit is true" do
    # https://github.com/rails/rails/pull/53375#issuecomment-2632770426
    assert_same true, ApplicationJob.enqueue_after_transaction_commit
    assert_same true, Noticed::ApplicationJob.enqueue_after_transaction_commit
    assert_same true, ActiveStorage::AnalyzeJob.enqueue_after_transaction_commit
  end

... but at this point it kinda feels like a global config. (Presumably the initializer can be removed in Rails 8.1 if my understanding of the above comments is correct. And the test can be removed when I have confidence this won't change anymore 😆 )

@mugitti9

Copy link
Copy Markdown
Contributor

@dhh (and everyone here)

“Yes, by default, all jobs should enqueue after the commit.”

I’ve opened a follow-up PR that proposes a concrete patch to move Rails toward that default behavior:
#55788

The change sets enqueue_after_transaction_commit = true by default when ActiveRecord is available, and adds validation to prevent enabling it in environments where transaction hooks are not supported. This helps avoid premature job execution inside transactions—a source of real inconsistencies in many applications.

I’d appreciate your thoughts on whether this approach aligns with the intended behavior.

Thanks as always for maintaining Rails.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Deprecate config.active_job.enqueue_after_transaction_commit config

6 participants