Class: Isolator::ActiveSupportTransactionSubscriber::Subscriber
- Inherits:
-
Isolator::ActiveSupportSubscriber::Subscriber
- Object
- Isolator::ActiveSupportSubscriber::Subscriber
- Isolator::ActiveSupportTransactionSubscriber::Subscriber
- Defined in:
- lib/isolator/orm_adapters/active_support_transaction_subscriber.rb
Instance Attribute Summary collapse
-
#stacks ⇒ Object
readonly
Returns the value of attribute stacks.
Instance Method Summary collapse
- #finish(event, id, payload) ⇒ Object
-
#initialize ⇒ Subscriber
constructor
A new instance of Subscriber.
- #start(event, id, payload) ⇒ Object
Constructor Details
#initialize ⇒ Subscriber
Returns a new instance of Subscriber.
9 10 11 |
# File 'lib/isolator/orm_adapters/active_support_transaction_subscriber.rb', line 9 def initialize @stacks = Hash.new { |h, k| h[k] = [] } end |
Instance Attribute Details
#stacks ⇒ Object (readonly)
Returns the value of attribute stacks.
7 8 9 |
# File 'lib/isolator/orm_adapters/active_support_transaction_subscriber.rb', line 7 def stacks @stacks end |
Instance Method Details
#finish(event, id, payload) ⇒ Object
32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 |
# File 'lib/isolator/orm_adapters/active_support_transaction_subscriber.rb', line 32 def finish(event, id, payload) if event.start_with?("sql.") if start_event?(payload[:sql]) connection_id = extract_connection_id(payload) stack = stacks[connection_id] stack << :raw Isolator.incr_transactions!(connection_id) end if finish_event?(payload[:sql]) connection_id = extract_connection_id(payload) stack = stacks[connection_id] # Decrement only if the transaction was started in the raw mode, # otherwise we should wait for the "transaction" event if stack.last == :raw stack.pop stacks.delete(connection_id) if stack.empty? Isolator.decr_transactions!(connection_id) end end end if event.start_with?("transaction.") connection_id = extract_transaction_connection_id(payload) stack = stacks[connection_id] if stack.last == :transaction stack.pop stacks.delete(connection_id) if stack.empty? Isolator.decr_transactions!(connection_id) end end end |
#start(event, id, payload) ⇒ Object
13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 |
# File 'lib/isolator/orm_adapters/active_support_transaction_subscriber.rb', line 13 def start(event, id, payload) if event.start_with?("transaction.") connection_id = extract_transaction_connection_id(payload) stack = stacks[connection_id] # transaction.active_record can be issued without a query (when we restart the transaction), # so we should add a new one on the stack. # Example: https://github.com/rails/rails/blob/ce49fa9b31cd4a21d43db39d0cea364bce28b51d/activerecord/lib/active_record/connection_adapters/abstract/transaction.rb#L337 if stack.last == :raw # Update the type of the last transaction event stack.pop stack << :transaction else stack << :transaction Isolator.incr_transactions!(connection_id) end end end |