Class: Synapse::UnitOfWork::UnitOfWorkListenerCollection

Inherits:
UnitOfWorkListener show all
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

Constructor Details

#initializeUnitOfWorkListenerCollection

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

Parameters:

Returns:

  • (undefined)


74
75
76
77
78
79
# File 'lib/synapse/uow/listener_collection.rb', line 74

def after_commit(unit)
  @listeners.reverse_each do |listener|
    @logger.debug 'Notifying [%s] of finished commit' % listener.class
    listener.after_commit unit
  end
end

#on_cleanup(unit) ⇒ undefined

Parameters:

Returns:

  • (undefined)


93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/synapse/uow/listener_collection.rb', line 93

def on_cleanup(unit)
  @listeners.reverse_each do |listener|
    @logger.debug 'Notifying [%s] of cleanup' % listener.class

    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
end

#on_event_registered(unit, event) ⇒ EventMessage

Parameters:

Returns:

  • (EventMessage)


43
44
45
46
47
48
49
# File 'lib/synapse/uow/listener_collection.rb', line 43

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

Parameters:

Returns:

  • (undefined)


55
56
57
58
59
60
# File 'lib/synapse/uow/listener_collection.rb', line 55

def on_prepare_commit(unit, aggregates, events)
  @listeners.each do |listener|
    @logger.debug 'Notifying [%s] of commit' % listener.class
    listener.on_prepare_commit unit, aggregates, events
  end
end

#on_prepare_transaction_commit(unit, transaction) ⇒ undefined

Parameters:

Returns:

  • (undefined)


65
66
67
68
69
70
# File 'lib/synapse/uow/listener_collection.rb', line 65

def on_prepare_transaction_commit(unit, transaction)
  @listeners.each do |listener|
    @logger.debug 'Notifying [%s] of transactional commit' % listener.class
    listener.on_prepare_transaction_commit unit, transaction
  end
end

#on_rollback(unit, cause = nil) ⇒ undefined

Parameters:

  • unit (UnitOfWork)
  • cause (Error) (defaults to: nil)

Returns:

  • (undefined)


84
85
86
87
88
89
# File 'lib/synapse/uow/listener_collection.rb', line 84

def on_rollback(unit, cause = nil)
  @listeners.reverse_each do |listener|
    @logger.debug 'Notifying [%s] of rollback' % listener.class
    listener.on_rollback unit, cause
  end
end

#on_start(unit) ⇒ undefined

Parameters:

Returns:

  • (undefined)


33
34
35
36
37
38
# File 'lib/synapse/uow/listener_collection.rb', line 33

def on_start(unit)
  @listeners.each do |listener|
    @logger.debug 'Notifying [%s] of start' % listener.class
    listener.on_start unit
  end
end

#push(listener) ⇒ undefined Also known as: <<

Pushes a unit of work listener onto the end of this collection

Parameters:

Returns:

  • (undefined)


24
25
26
27
# File 'lib/synapse/uow/listener_collection.rb', line 24

def push(listener)
  @logger.debug 'Registering listener [%s]' % listener.class
  @listeners.push listener
end