Class: Datadog::Event

Inherits:
Object
  • Object
show all
Defined in:
lib/ddtrace/event.rb

Overview

A simple pub-sub event model for components to exchange messages through.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(name) ⇒ Event

Returns a new instance of Event.



11
12
13
14
15
# File 'lib/ddtrace/event.rb', line 11

def initialize(name)
  @name = name
  @subscriptions = {}
  @mutex = Mutex.new
end

Instance Attribute Details

#nameObject (readonly)

Returns the value of attribute name.



7
8
9
# File 'lib/ddtrace/event.rb', line 7

def name
  @name
end

#subscriptionsObject (readonly)

Returns the value of attribute subscriptions.



7
8
9
# File 'lib/ddtrace/event.rb', line 7

def subscriptions
  @subscriptions
end

Instance Method Details

#publish(*args) ⇒ Object



39
40
41
42
43
44
45
46
47
48
49
50
51
# File 'lib/ddtrace/event.rb', line 39

def publish(*args)
  @mutex.synchronize do
    subscriptions.each do |key, block|
      begin
        block.call(*args)
      rescue StandardError => e
        Datadog.logger.debug("Error while handling '#{key}' for '#{name}' event: #{e.message}")
      end
    end

    true
  end
end

#subscribe(key, &block) ⇒ Object

Raises:

  • (ArgumentError)


17
18
19
20
21
22
23
# File 'lib/ddtrace/event.rb', line 17

def subscribe(key, &block)
  raise ArgumentError, 'Must give a block to subscribe!' unless block

  @mutex.synchronize do
    subscriptions[key] = block
  end
end

#unsubscribe(key) ⇒ Object



25
26
27
28
29
# File 'lib/ddtrace/event.rb', line 25

def unsubscribe(key)
  @mutex.synchronize do
    subscriptions.delete(key)
  end
end

#unsubscribe_all!Object



31
32
33
34
35
36
37
# File 'lib/ddtrace/event.rb', line 31

def unsubscribe_all!
  @mutex.synchronize do
    subscriptions.clear
  end

  true
end