class Synapse::UnitOfWork::UnitOfWorkListenerCollection

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.

Public Class Methods

new() click to toggle source

@return [undefined]

# File lib/synapse/uow/listener_collection.rb, line 16
def initialize
  @listeners = Array.new
  @logger = Logging.logger[self.class]
end

Public Instance Methods

<<(listener)
Alias for: push
after_commit(unit) click to toggle source

@param [UnitOfWork] unit @return [undefined]

# 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) click to toggle source

@param [UnitOfWork] unit @return [undefined]

# 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) click to toggle source

@param [UnitOfWork] unit @param [EventMessage] event @return [EventMessage]

# 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) click to toggle source

@param [UnitOfWork] unit @param [Array<AggregateRoot>] aggregates @param [Hash<EventBus, Array>] events @return [undefined]

# 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) click to toggle source

@param [UnitOfWork] unit @param [Object] transaction @return [undefined]

# 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) click to toggle source

@param [UnitOfWork] unit @param [Error] cause @return [undefined]

# 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) click to toggle source

@param [UnitOfWork] unit @return [undefined]

# 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) click to toggle source

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

@param [UnitOfWorkListener] listener @return [undefined]

# File lib/synapse/uow/listener_collection.rb, line 25
def push(listener)
  @logger.debug "Registering listener {#{listener.class}}"
  @listeners.push listener
end
Also aliased as: <<