2018-12-13 13:39:08 +05:30
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2018-03-17 18:26:18 +05:30
|
|
|
module AfterCommitQueue
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_commit :_run_after_commit_queue
|
|
|
|
after_rollback :_clear_after_commit_queue
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_after_commit(&block)
|
|
|
|
_after_commit_queue << block if block
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_after_commit_or_now(&block)
|
2021-10-27 15:23:28 +05:30
|
|
|
if Gitlab::Database.main.inside_transaction?
|
2021-06-08 01:23:25 +05:30
|
|
|
if ActiveRecord::Base.connection.current_transaction.records&.include?(self)
|
2018-03-17 18:26:18 +05:30
|
|
|
run_after_commit(&block)
|
|
|
|
else
|
|
|
|
# If the current transaction does not include this record, we can run
|
|
|
|
# the block now, even if it queues a Sidekiq job.
|
|
|
|
Sidekiq::Worker.skipping_transaction_check do
|
|
|
|
instance_eval(&block)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
else
|
|
|
|
instance_eval(&block)
|
|
|
|
end
|
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
protected
|
|
|
|
|
|
|
|
def _run_after_commit_queue
|
|
|
|
while action = _after_commit_queue.pop
|
|
|
|
self.instance_eval(&action)
|
|
|
|
end
|
|
|
|
end
|
|
|
|
|
|
|
|
def _after_commit_queue
|
|
|
|
@after_commit_queue ||= []
|
|
|
|
end
|
|
|
|
|
|
|
|
def _clear_after_commit_queue
|
|
|
|
_after_commit_queue.clear
|
|
|
|
end
|
|
|
|
end
|