Module: ActiveRecordDeadlockHandler::Callbacks

Defined in:
lib/active_record_deadlock_handler/callbacks.rb

Class Method Summary collapse

Class Method Details

.clearObject



22
23
24
# File 'lib/active_record_deadlock_handler/callbacks.rb', line 22

def clear
  LOCK.synchronize { @handlers.clear }
end

.register {|exception, attempt, config| ... } ⇒ Object

Register a callback to be invoked on each deadlock detection.

Yield Parameters:

  • exception (ActiveRecord::Deadlocked)

    the deadlock error

  • attempt (Integer)

    which retry attempt triggered this (1-indexed)

  • config (Configuration)

Raises:

  • (ArgumentError)


16
17
18
19
20
# File 'lib/active_record_deadlock_handler/callbacks.rb', line 16

def register(&block)
  raise ArgumentError, "block required" unless block

  LOCK.synchronize { @handlers << block }
end

.run(exception:, attempt:, config:) ⇒ Object

Invokes all registered callbacks with deadlock context. Runs outside the lock so slow callbacks don’t block registration.



28
29
30
31
32
33
34
35
# File 'lib/active_record_deadlock_handler/callbacks.rb', line 28

def run(exception:, attempt:, config:)
  handlers = LOCK.synchronize { @handlers.dup }
  handlers.each do |handler|
    handler.call(exception: exception, attempt: attempt, config: config)
  rescue StandardError => e
    warn "[ActiveRecordDeadlockHandler] Callback raised an error: #{e.class}: #{e.message}"
  end
end