2018-10-06 19:10:08 -04:00
|
|
|
# frozen_string_literal: true
|
|
|
|
|
2017-08-17 05:00:31 -04:00
|
|
|
module AfterCommitQueue
|
|
|
|
extend ActiveSupport::Concern
|
|
|
|
|
|
|
|
included do
|
|
|
|
after_commit :_run_after_commit_queue
|
|
|
|
after_rollback :_clear_after_commit_queue
|
|
|
|
end
|
|
|
|
|
2017-11-29 10:30:17 -05:00
|
|
|
def run_after_commit(&block)
|
2017-08-17 05:00:31 -04:00
|
|
|
_after_commit_queue << block if block
|
2017-11-29 10:30:17 -05:00
|
|
|
|
|
|
|
true
|
|
|
|
end
|
|
|
|
|
|
|
|
def run_after_commit_or_now(&block)
|
2021-07-29 11:09:48 -04:00
|
|
|
if Gitlab::Database.main.inside_transaction?
|
2021-05-11 14:10:36 -04:00
|
|
|
if ActiveRecord::Base.connection.current_transaction.records&.include?(self)
|
2017-12-22 05:38:35 -05:00
|
|
|
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
|
2017-11-29 10:30:17 -05:00
|
|
|
else
|
|
|
|
instance_eval(&block)
|
|
|
|
end
|
|
|
|
|
2017-08-17 05:00:31 -04:00
|
|
|
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
|