diff options
author | Douwe Maan <douwe@selenight.nl> | 2017-11-29 18:30:17 +0300 |
---|---|---|
committer | Douwe Maan <douwe@selenight.nl> | 2017-12-05 13:59:39 +0300 |
commit | 1e6ca3c41ead23c5e433460c8c807ea73d9ec0ef (patch) | |
tree | ed6a5da0def848adc1a15f80e69d9c55651895a4 /lib/after_commit_queue.rb | |
parent | a5c3f1c8ff7da20183b172b2b0693a6010c9e86d (diff) |
Consistently schedule Sidekiq jobs
Diffstat (limited to 'lib/after_commit_queue.rb')
-rw-r--r-- | lib/after_commit_queue.rb | 26 |
1 files changed, 24 insertions, 2 deletions
diff --git a/lib/after_commit_queue.rb b/lib/after_commit_queue.rb index 4750a2c373a..db63c5038ae 100644 --- a/lib/after_commit_queue.rb +++ b/lib/after_commit_queue.rb @@ -6,12 +6,34 @@ module AfterCommitQueue after_rollback :_clear_after_commit_queue end - def run_after_commit(method = nil, &block) - _after_commit_queue << proc { self.send(method) } if method # rubocop:disable GitlabSecurity/PublicSend + def run_after_commit(&block) _after_commit_queue << block if block + + true + end + + def run_after_commit_or_now(&block) + if AfterCommitQueue.inside_transaction? + run_after_commit(&block) + else + instance_eval(&block) + end + true end + def self.open_transactions_baseline + if ::Rails.env.test? + return DatabaseCleaner.connections.count { |conn| conn.strategy.is_a?(DatabaseCleaner::ActiveRecord::Transaction) } + end + + 0 + end + + def self.inside_transaction? + ActiveRecord::Base.connection.open_transactions > open_transactions_baseline + end + protected def _run_after_commit_queue |