Commit 8dab2a57 authored by Thong Kuah's avatar Thong Kuah

Fix sidekiq transaction check to be decomposition aware

parent e43bf390
......@@ -20,7 +20,7 @@ module Sidekiq
module NoEnqueueingFromTransactions
%i(perform_async perform_at perform_in).each do |name|
define_method(name) do |*args|
if !Sidekiq::Worker.skip_transaction_check && ApplicationRecord.inside_transaction?
if !Sidekiq::Worker.skip_transaction_check && inside_transaction?
begin
raise Sidekiq::Worker::EnqueueFromTransactionError, <<~MSG
`#{self}.#{name}` cannot be called inside a transaction as this can lead to
......@@ -38,6 +38,12 @@ module Sidekiq
super(*args)
end
end
private
def inside_transaction?
::ApplicationRecord.inside_transaction? || ::Ci::ApplicationRecord.inside_transaction?
end
end
prepend NoEnqueueingFromTransactions
......
......@@ -29,4 +29,10 @@ RSpec.describe 'Sidekiq::Worker' do
end
end
end
it 'forbids queue sidekiq worker in a Ci::ApplicationRecord transaction' do
Ci::Pipeline.transaction do
expect { worker_class.perform_async }.to raise_error(Sidekiq::Worker::EnqueueFromTransactionError)
end
end
end
......@@ -239,6 +239,7 @@ RSpec.configure do |config|
# is not yet opened at the time that is triggered
config.prepend_before do
ApplicationRecord.set_open_transactions_baseline
::Ci::ApplicationRecord.set_open_transactions_baseline
end
config.append_before do
......@@ -247,6 +248,7 @@ RSpec.configure do |config|
config.append_after do
ApplicationRecord.reset_open_transactions_baseline
::Ci::ApplicationRecord.reset_open_transactions_baseline
end
config.before do |example|
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment