Class: Datadog::Event
- Inherits:
-
Object
- Object
- Datadog::Event
- Defined in:
- lib/ddtrace/event.rb
Overview
A simple pub-sub event model for components to exchange messages through.
Direct Known Subclasses
Tracer::TraceCompleted, Workers::TraceWriter::FlushCompleted
Instance Attribute Summary collapse
-
#name ⇒ Object
readonly
Returns the value of attribute name.
-
#subscriptions ⇒ Object
readonly
Returns the value of attribute subscriptions.
Instance Method Summary collapse
-
#initialize(name) ⇒ Event
constructor
A new instance of Event.
- #publish(*args) ⇒ Object
- #subscribe(key, &block) ⇒ Object
- #unsubscribe(key) ⇒ Object
- #unsubscribe_all! ⇒ Object
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
#name ⇒ Object (readonly)
Returns the value of attribute name.
7 8 9 |
# File 'lib/ddtrace/event.rb', line 7 def name @name end |
#subscriptions ⇒ Object (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.}") end end true end end |
#subscribe(key, &block) ⇒ Object
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 |