Skip to content

Commit

Permalink
DB pool timeout error reproducer
Browse files Browse the repository at this point in the history
  • Loading branch information
adamruzicka committed Aug 4, 2020
1 parent 63e0db8 commit 0a0e566
Showing 1 changed file with 5 additions and 11 deletions.
16 changes: 5 additions & 11 deletions lib/dynflow/executors.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,13 @@ module Executors
require 'dynflow/executors/parallel'

class << self
# Every time we run a code that can be defined outside of Dynflow,
# we should wrap it with this method, and we can ensure here to do
# necessary cleanup, such as cleaning ActiveRecord connections
def run_user_code
# Here we cover a case where the connection was already checked out from
# the pool and had opened transactions. In that case, we should leave the
# cleanup to the other runtime unit which opened the transaction. If the
# connection was checked out or there are no opened transactions, we can
# safely perform the cleanup.
no_previously_opened_transactions = active_record_open_transactions.zero?
yield
# Always acquire a connection from the DB pool, even if it may not be
# needed
::ActiveRecord::Base.connection_pool.with_connection do |_conn|
yield
end
ensure
::ActiveRecord::Base.clear_active_connections! if no_previously_opened_transactions && active_record_connected?
::Logging.mdc.clear if defined? ::Logging
end

Expand Down

0 comments on commit 0a0e566

Please sign in to comment.