Class: Clepsydra::Subscriber

Inherits:
Object
  • Object
show all
Defined in:
lib/clepsydra/subscriber.rb

Defined Under Namespace

Classes: NoSuchEventError

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(event_name, monotonic, listener) ⇒ Subscriber

Returns a new instance of Subscriber.



9
10
11
12
13
14
15
16
17
18
19
# File 'lib/clepsydra/subscriber.rb', line 9

def initialize(event_name, monotonic, listener)
  @id = if monotonic
          "monotonic_subscriber_#{Clepsydra::TokenProvider.generate}"
        else
          "subscriber_#{Clepsydra::TokenProvider.generate}"
        end
  @event_name = event_name
  @monotonic = monotonic
  @listener = listener
  @start_times = {}
end

Instance Attribute Details

#event_nameObject (readonly)

Returns the value of attribute event_name.



7
8
9
# File 'lib/clepsydra/subscriber.rb', line 7

def event_name
  @event_name
end

#idObject (readonly)

Returns the value of attribute id.



7
8
9
# File 'lib/clepsydra/subscriber.rb', line 7

def id
  @id
end

Instance Method Details

#finish(event_id, instrumenter_id, payload) ⇒ Object

Raises:



25
26
27
28
29
30
31
# File 'lib/clepsydra/subscriber.rb', line 25

def finish(event_id, instrumenter_id, payload)
  started = @start_times.delete(event_id)

  raise NoSuchEventError, "#{event_id} for #{@event_name} does not exist or already completed" if started.nil?

  @listener.call(@event_name, event_id, instrumenter_id, @id, started, current_time, payload)
end

#start(event_id) ⇒ Object



21
22
23
# File 'lib/clepsydra/subscriber.rb', line 21

def start(event_id)
  @start_times[event_id] = current_time
end