Class: Synapse::UnitOfWork::UnitOfWorkListenerCollection
- Inherits:
-
UnitOfWorkListener
- Object
- UnitOfWorkListener
- Synapse::UnitOfWork::UnitOfWorkListenerCollection
- Defined in:
- lib/synapse/uow/listener_collection.rb
Overview
Represents a mechanism for notifying registered listeners in a specific order of precendence
When #on_start, #on_event_registered, #on_prepare_commit and #on_prepare_transaction_commit are invoked, the listeners will be notified the order they were added to this colleciton. When #after_commit, #on_rollback and #on_cleanup are called, listeners will be notified in the reverse order they were added to this collection.
This behavior is particularly useful for an auditing listener, which could log a commit before any listeners are allowed to do anything, and log that the commit is finished after all other listeners have finished.
Instance Method Summary collapse
- #after_commit(unit) ⇒ undefined
- #initialize ⇒ undefined constructor
- #on_cleanup(unit) ⇒ undefined
- #on_event_registered(unit, event) ⇒ EventMessage
- #on_prepare_commit(unit, aggregates, events) ⇒ undefined
- #on_prepare_transaction_commit(unit, transaction) ⇒ undefined
- #on_rollback(unit, cause = nil) ⇒ undefined
- #on_start(unit) ⇒ undefined
-
#push(listener) ⇒ undefined
(also: #<<)
Pushes a unit of work listener onto the end of this collection.
Constructor Details
#initialize ⇒ undefined
16 17 18 19 |
# File 'lib/synapse/uow/listener_collection.rb', line 16 def initialize @listeners = Array.new @logger = Logging.logger[self.class] end |
Instance Method Details
#after_commit(unit) ⇒ undefined
75 76 77 78 79 80 |
# File 'lib/synapse/uow/listener_collection.rb', line 75 def after_commit(unit) @listeners.reverse_each do |listener| @logger.debug "Notifying {#{listener.class}} that unit of work has been committed" listener.after_commit unit end end |
#on_cleanup(unit) ⇒ undefined
94 95 96 97 98 99 100 101 102 103 104 105 106 107 |
# File 'lib/synapse/uow/listener_collection.rb', line 94 def on_cleanup(unit) @listeners.reverse_each do |listener| @logger.debug "Notifying {#{listener.class}} that unit of work is cleaning up" begin listener.on_cleanup unit rescue => exception # Ignore this exception so that we can continue cleaning up backtrace = exception.backtrace.join $RS @logger.warn "Listener {#{listener.class}} raised exception during cleanup: " + "#{exception.inspect} #{backtrace}" end end end |
#on_event_registered(unit, event) ⇒ EventMessage
44 45 46 47 48 49 50 |
# File 'lib/synapse/uow/listener_collection.rb', line 44 def on_event_registered(unit, event) @listeners.each do |listener| event = listener.on_event_registered unit, event end event end |
#on_prepare_commit(unit, aggregates, events) ⇒ undefined
56 57 58 59 60 61 |
# File 'lib/synapse/uow/listener_collection.rb', line 56 def on_prepare_commit(unit, aggregates, events) @listeners.each do |listener| @logger.debug "Notifying {#{listener.class}} that unit of work is preparing for commit" listener.on_prepare_commit unit, aggregates, events end end |
#on_prepare_transaction_commit(unit, transaction) ⇒ undefined
66 67 68 69 70 71 |
# File 'lib/synapse/uow/listener_collection.rb', line 66 def on_prepare_transaction_commit(unit, transaction) @listeners.each do |listener| @logger.debug "Notifying {#{listener.class}} that unit of work is preparing for tx commit" listener.on_prepare_transaction_commit unit, transaction end end |
#on_rollback(unit, cause = nil) ⇒ undefined
85 86 87 88 89 90 |
# File 'lib/synapse/uow/listener_collection.rb', line 85 def on_rollback(unit, cause = nil) @listeners.reverse_each do |listener| @logger.debug "Notifying {#{listener.class}} that unit of work is rolling back" listener.on_rollback unit, cause end end |
#on_start(unit) ⇒ undefined
34 35 36 37 38 39 |
# File 'lib/synapse/uow/listener_collection.rb', line 34 def on_start(unit) @listeners.each do |listener| @logger.debug "Notifying {#{listener.class}} that unit of work is starting" listener.on_start unit end end |
#push(listener) ⇒ undefined Also known as: <<
Pushes a unit of work listener onto the end of this collection
25 26 27 28 |
# File 'lib/synapse/uow/listener_collection.rb', line 25 def push(listener) @logger.debug "Registering listener {#{listener.class}}" @listeners.push listener end |