Module: Hallmonitor::Monitored

Included in:
Foo, Event
Defined in:
lib/hallmonitor/monitored.rb

Defined Under Namespace

Modules: ClassMethods

Class Method Summary collapse

Instance Method Summary collapse

Class Method Details

.included(base) ⇒ Object



52
53
54
# File 'lib/hallmonitor/monitored.rb', line 52

def self.included(base)
  base.extend(ClassMethods)
end

Instance Method Details

#emit(event = nil) {|to_emit| ... } ⇒ Object

Emits an event: self if the event param is nil, the passed in event if it’s an Event, or constructs a Event from the passed in param.

If the parameter is a Event, it will be emitted as is. Otherwise, a new Event will be created with the parameter and emitted.

Parameters:

  • event (Mixed) (defaults to: nil)

    The thing to emit, see method description

Yields:

  • (to_emit)

    The thing that’s going to be emitted

Returns:

  • nil



65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/hallmonitor/monitored.rb', line 65

def emit(event = nil)
  to_emit = self
  unless event.nil?
    to_emit = event.is_a?(Hallmonitor::Event) ? event : Hallmonitor::Event.new(event)
  end

  # If we were given a block, then we want to execute that
  yield(to_emit) if block_given?

  Dispatcher.output(to_emit)
  nil
end

#watch(name) {|event| ... } ⇒ Object

Note:

Will emit the timed event even if the block raises an error

Executes and times a block of code and emits a TimedEvent

Parameters:

  • name (String)

    The name of the event to emit

Yields:

  • (event)

    the event object that will be emitted

Returns:

  • Whatever the block’s return value is



83
84
85
86
87
88
89
90
91
92
# File 'lib/hallmonitor/monitored.rb', line 83

def watch(name)
  event = Hallmonitor::TimedEvent.new(name)
  event.start = Time.now
  begin
    yield(event)
  ensure
    event.stop = Time.now
    emit(event)
  end
end