2021-02-03 16:09:17 -05:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-02-20 08:28:05 -05:00
|
|
|
module Sidekiq
|
|
|
|
module Worker
|
2017-12-22 05:38:35 -05:00
|
|
|
EnqueueFromTransactionError = Class.new(StandardError)
|
|
|
|
|
2017-06-02 13:12:08 -04:00
|
|
|
def self.skipping_transaction_check(&block)
|
2019-07-04 09:46:46 -04:00
|
|
|
previous_skip_transaction_check = self.skip_transaction_check
|
|
|
|
Thread.current[:sidekiq_worker_skip_transaction_check] = true
|
2017-06-02 13:12:08 -04:00
|
|
|
yield
|
|
|
|
ensure
|
2019-07-04 09:46:46 -04:00
|
|
|
Thread.current[:sidekiq_worker_skip_transaction_check] = previous_skip_transaction_check
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.skip_transaction_check
|
|
|
|
Thread.current[:sidekiq_worker_skip_transaction_check]
|
2017-06-02 13:12:08 -04:00
|
|
|
end
|
2017-06-01 17:35:32 -04:00
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
def self.inside_transaction?
|
|
|
|
::ApplicationRecord.inside_transaction? || ::Ci::ApplicationRecord.inside_transaction?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.raise_exception_for_being_inside_a_transaction?
|
|
|
|
!skip_transaction_check && inside_transaction?
|
|
|
|
end
|
|
|
|
|
|
|
|
def self.raise_inside_transaction_exception(cause:)
|
|
|
|
raise Sidekiq::Worker::EnqueueFromTransactionError, <<~MSG
|
|
|
|
#{cause} cannot be enqueued inside a transaction as this can lead to
|
|
|
|
race conditions when the worker runs before the transaction is committed and
|
|
|
|
tries to access a model that has not been saved yet.
|
|
|
|
|
|
|
|
Use an `after_commit` hook, or include `AfterCommitQueue` and use a `run_after_commit` block instead.
|
|
|
|
MSG
|
|
|
|
end
|
|
|
|
|
2017-02-20 08:28:05 -05:00
|
|
|
module ClassMethods
|
2017-12-22 05:38:35 -05:00
|
|
|
module NoEnqueueingFromTransactions
|
2017-02-20 08:28:05 -05:00
|
|
|
%i(perform_async perform_at perform_in).each do |name|
|
|
|
|
define_method(name) do |*args|
|
2022-05-31 05:08:17 -04:00
|
|
|
if Sidekiq::Worker.raise_exception_for_being_inside_a_transaction?
|
2018-03-05 06:31:23 -05:00
|
|
|
begin
|
2022-05-31 05:08:17 -04:00
|
|
|
Sidekiq::Worker.raise_inside_transaction_exception(cause: "#{self}.#{name}")
|
2018-03-05 06:31:23 -05:00
|
|
|
rescue Sidekiq::Worker::EnqueueFromTransactionError => e
|
2020-09-09 08:08:22 -04:00
|
|
|
Gitlab::AppLogger.error(e.message) if ::Rails.env.production?
|
2019-12-16 07:07:43 -05:00
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
2018-03-05 06:31:23 -05:00
|
|
|
end
|
2017-11-29 10:30:17 -05:00
|
|
|
end
|
2017-02-20 08:28:05 -05:00
|
|
|
|
2017-11-29 10:30:17 -05:00
|
|
|
super(*args)
|
2017-02-20 08:28:05 -05:00
|
|
|
end
|
|
|
|
end
|
2022-05-31 05:08:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
prepend NoEnqueueingFromTransactions
|
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2021-11-29 22:13:04 -05:00
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
# We deliver emails using the `deliver_later` method and it uses ActiveJob
|
|
|
|
# under the hood, which later processes the email via the defined ActiveJob adapter's `enqueue` method.
|
|
|
|
# For GitLab, the ActiveJob adapter is Sidekiq (in development and production environments).
|
|
|
|
# We need to set the following up to override the ActiveJob adapater
|
|
|
|
# so as to ensure that no mailer jobs are enqueued from within a transaction.
|
|
|
|
module ActiveJob
|
|
|
|
module QueueAdapters
|
|
|
|
module NoEnqueueingFromTransactions
|
|
|
|
%i(enqueue enqueue_at).each do |name|
|
|
|
|
define_method(name) do |*args|
|
|
|
|
if Sidekiq::Worker.raise_exception_for_being_inside_a_transaction?
|
|
|
|
begin
|
|
|
|
job = args.first
|
|
|
|
Sidekiq::Worker.raise_inside_transaction_exception(
|
|
|
|
cause: "The #{job.class} job, enqueued into the queue: #{job.queue_name}"
|
|
|
|
)
|
|
|
|
rescue Sidekiq::Worker::EnqueueFromTransactionError => e
|
|
|
|
Gitlab::AppLogger.error(e.message) if ::Rails.env.production?
|
|
|
|
Gitlab::ErrorTracking.track_and_raise_for_dev_exception(e)
|
|
|
|
end
|
|
|
|
end
|
2021-11-29 22:13:04 -05:00
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
super(*args)
|
2021-11-29 22:13:04 -05:00
|
|
|
end
|
2017-02-20 08:28:05 -05:00
|
|
|
end
|
2022-05-31 05:08:17 -04:00
|
|
|
end
|
|
|
|
|
|
|
|
# This adapter is used in development & production environments.
|
|
|
|
class SidekiqAdapter
|
|
|
|
prepend NoEnqueueingFromTransactions
|
|
|
|
end
|
2017-02-20 08:28:05 -05:00
|
|
|
|
2022-05-31 05:08:17 -04:00
|
|
|
# This adapter is used in test environment.
|
|
|
|
# If we don't override the test environment adapter,
|
|
|
|
# we won't be seeing any failing jobs during the CI run,
|
|
|
|
# even if we enqueue mailers from within a transaction.
|
|
|
|
class TestAdapter
|
2017-12-22 05:38:35 -05:00
|
|
|
prepend NoEnqueueingFromTransactions
|
2017-02-20 08:28:05 -05:00
|
|
|
end
|
|
|
|
end
|
|
|
|
end
|
2017-06-01 17:35:32 -04:00
|
|
|
|
|
|
|
module ActiveRecord
|
|
|
|
class Base
|
2017-06-02 13:12:08 -04:00
|
|
|
module SkipTransactionCheckAfterCommit
|
2020-10-02 08:09:03 -04:00
|
|
|
def committed!(*args, **kwargs)
|
2017-06-02 13:12:08 -04:00
|
|
|
Sidekiq::Worker.skipping_transaction_check { super }
|
2017-06-01 17:35:32 -04:00
|
|
|
end
|
|
|
|
end
|
|
|
|
|
2017-06-02 13:12:08 -04:00
|
|
|
prepend SkipTransactionCheckAfterCommit
|
2017-06-01 17:35:32 -04:00
|
|
|
end
|
|
|
|
end
|