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 ⇒ UnitOfWorkListenerCollection
constructor
A new instance of UnitOfWorkListenerCollection.
- #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 ⇒ UnitOfWorkListenerCollection
Returns a new instance of UnitOfWorkListenerCollection.
15 16 17 18 |
# File 'lib/synapse/uow/listener_collection.rb', line 15 def initialize @listeners = Array.new @logger = Logging.logger[self.class] end |
Instance Method Details
#after_commit(unit) ⇒ undefined
98 99 100 101 102 103 104 105 106 107 108 109 110 |
# File 'lib/synapse/uow/listener_collection.rb', line 98 def after_commit(unit) @logger.debug 'Notifying listeners that commit has finished' @listeners.reverse_each do |listener| if @logger.debug? @logger.debug 'Notifying [%s] of finished commit' % listener.class end listener.after_commit unit end @logger.debug 'Listeners successfully notified' end |
#on_cleanup(unit) ⇒ undefined
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 |
# File 'lib/synapse/uow/listener_collection.rb', line 131 def on_cleanup(unit) @logger.debug 'Notifying listeners of cleanup' @listeners.reverse_each do |listener| if @logger.debug? @logger.debug 'Notifying [%s] of cleanup' % listener.class end begin listener.on_cleanup unit rescue => exception # Ignore this exception so that we can continue cleaning up @logger.warn 'Listener raised an exception during cleanup: %s' % exception.inspect end end @logger.debug 'Listeners successfully notified' end |
#on_event_registered(unit, event) ⇒ EventMessage
53 54 55 56 57 58 59 |
# File 'lib/synapse/uow/listener_collection.rb', line 53 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
65 66 67 68 69 70 71 72 73 74 75 76 77 |
# File 'lib/synapse/uow/listener_collection.rb', line 65 def on_prepare_commit(unit, aggregates, events) @logger.debug 'Notifying listeners that commit was requested' @listeners.each do |listener| if @logger.debug? @logger.debug 'Notifying [%s] of commit' % listener.class end listener.on_prepare_commit unit, aggregates, events end @logger.debug 'Listeners successfully notified' end |
#on_prepare_transaction_commit(unit, transaction) ⇒ undefined
82 83 84 85 86 87 88 89 90 91 92 93 94 |
# File 'lib/synapse/uow/listener_collection.rb', line 82 def on_prepare_transaction_commit(unit, transaction) @logger.debug 'Notifying listeners that transactional commit was requested' @listeners.each do |listener| if @logger.debug? @logger.debug 'Notifying [%s] of transactional commit' % listener.class end listener.on_prepare_transaction_commit unit, transaction end @logger.debug 'Listeners successfully notified' end |
#on_rollback(unit, cause = nil) ⇒ undefined
115 116 117 118 119 120 121 122 123 124 125 126 127 |
# File 'lib/synapse/uow/listener_collection.rb', line 115 def on_rollback(unit, cause = nil) @logger.debug 'Notifying listeners of rollback' @listeners.reverse_each do |listener| if @logger.debug? @logger.debug 'Notifying [%s] of rollback' % listener.class end listener.on_rollback unit, cause end @logger.debug 'Listeners successfully notified' end |
#on_start(unit) ⇒ undefined
36 37 38 39 40 41 42 43 44 45 46 47 48 |
# File 'lib/synapse/uow/listener_collection.rb', line 36 def on_start(unit) @logger.debug 'Notifying listeners that unit of work is starting' @listeners.each do |listener| if @logger.debug? @logger.debug 'Notifying [%s] of start' % listener.class end listener.on_start unit end @logger.debug 'Listeners successfully notified' end |
#push(listener) ⇒ undefined Also known as: <<
Pushes a unit of work listener onto the end of this collection
24 25 26 27 28 29 30 |
# File 'lib/synapse/uow/listener_collection.rb', line 24 def push(listener) if @logger.debug? @logger.debug 'Registering listener [%s]' % listener.class end @listeners.push listener end |