Commit 707621f2 authored by Dylan Griffith's avatar Dylan Griffith

Merge branch 'forbiq_sidekiq_txn_fixes' into 'master'

Fix sidekiq transaction check to be decomposition aware

See merge request gitlab-org/gitlab!75463
parents 08ca2442 8dab2a57
......@@ -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
......
# frozen_string_literal: true
require 'spec_helper'
RSpec.describe 'Sidekiq::Worker' do
let(:worker_class) do
Class.new do
include Sidekiq::Worker
def perform
end
end
end
it 'allows sidekiq worker outside of a transaction' do
expect { worker_class.perform_async }.not_to raise_error
end
it 'forbids queue sidekiq worker in a transaction' do
Project.transaction do
expect { worker_class.perform_async }.to raise_error(Sidekiq::Worker::EnqueueFromTransactionError)
end
end
it 'allows sidekiq worker in a transaction if skipped' do
Sidekiq::Worker.skipping_transaction_check do
Project.transaction do
expect { worker_class.perform_async }.not_to raise_error
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