sidekiq - ActiveRecord no enqueueing jobs after transaction commit in Rails 7.2 - Stack Overflow

admin2025-04-17  3

Rails 7.2 released a feature that enqueues active jobs by default after all transactions are committed and they deprecated the global setting

I am unable to get this behavior to work:

ActiveRecord::Base.transaction do
  user = User.find(1)
  user.first_name = "Race Condition #{SecureRandom.uuid}"
  user.save!

  ActiveRecord.after_all_transactions_commit do
    LotsOfWork.perform_later(user.id)
  end

  LotsOfWork.perform_later(user.id)
end

The code in the after_all_transactions_commit block does execute outside of the transaction but the perform_later inside the transaction does not wait until the transaction is over. I have even tried to manually set the option as mentioned in the docs:

class LotsOfWork < ApplicationJob
  self.enqueue_after_transaction_commit = :always
  sidekiq_options backtrace: true, retry: 0

  def perform(user_id)
    user = User.find(patient_id)
    user.do_lots_of_work
  end
end

No luck! I'm on the latest version of Sidekiq 7.3.8 and ActiveJob 7.2.2.1. Am I misunderstanding the feature or is it possible Sidekiq or Rails has a bug?

转载请注明原文地址:http://anycun.com/QandA/1744896625a89161.html