Module: Webmachine::Events
- Defined in:
- lib/webmachine/events.rb,
lib/webmachine/events/instrumented_event.rb
Overview
Events implements the [ActiveSupport::Notifications](rubydoc.info/gems/activesupport/ActiveSupport/Notifications) instrumentation API. It delegates to the configured backend. The default backend is [AS::Notifications](rubydoc.info/gems/as-notifications/AS/Notifications).
# Published events
Webmachine publishes some internal events by default. All of them use the ‘wm.` prefix.
## ‘wm.dispatch` (Events.instrument)
The payload hash includes the following keys.
-
‘:resource` - The resource class name
-
‘:request` - A copy of the request object
-
‘:code` - The response code
Defined Under Namespace
Classes: InstrumentedEvent
Class Attribute Summary collapse
-
.backend ⇒ Object
The class that Events delegates all messages to.
Class Method Summary collapse
-
.instrument(name, payload = {}, &block) ⇒ Object
Instrument the given block by measuring the time taken to execute it and publish it.
-
.publish(name, *args) ⇒ Object
Publishes the given arguments to all listeners subscribed to the given event name.
-
.subscribe(name, *args, &block) ⇒ Object
Subscribes to the given event name.
-
.subscribed(callback, name, &block) ⇒ Object
Subscribe to an event temporarily while the block runs.
-
.unsubscribe(subscriber) ⇒ Object
Unsubscribes the given subscriber.
Class Attribute Details
.backend ⇒ Object
The class that Webmachine::Events delegates all messages to. (default [AS::Notifications](rubydoc.info/gems/as-notifications/AS/Notifications))
It can be changed to an [ActiveSupport::Notifications](rubydoc.info/gems/activesupport/ActiveSupport/Notifications) compatible backend early in the application startup process.
43 44 45 |
# File 'lib/webmachine/events.rb', line 43 def backend @backend end |
Class Method Details
.instrument(name, payload = {}, &block) ⇒ Object
Instrument the given block by measuring the time taken to execute it and publish it. Notice that events get sent even if an error occurs in the passed-in block.
If an exception happens during an instrumentation the payload will have a key ‘:exception` with an array of two elements as value: a string with the name of the exception class, and the exception message. (when using the default [AS::Notifications](rubydoc.info/gems/as-notifications/AS/Notifications) backend)
74 75 76 |
# File 'lib/webmachine/events.rb', line 74 def instrument(name, payload = {}, &block) backend.instrument(name, payload, &block) end |
.publish(name, *args) ⇒ Object
Publishes the given arguments to all listeners subscribed to the given event name.
50 51 52 |
# File 'lib/webmachine/events.rb', line 50 def publish(name, *args) backend.publish(name, *args) end |
.subscribe(name) {|name, start, end, event_id, payload| ... } ⇒ Object .subscribe(name) {|name, *args| ... } ⇒ Object .subscribe(name, listener) ⇒ Object
The documentation of this method describes its behaviour with the default backed [AS::Notifications](rubydoc.info/gems/as-notifications/AS/Notifications). It can change if a different backend is used.
Subscribes to the given event name.
137 138 139 |
# File 'lib/webmachine/events.rb', line 137 def subscribe(name, *args, &block) backend.subscribe(name, *args, &block) end |
.subscribed(callback, name, &block) ⇒ Object
The documentation of this method describes its behaviour with the default backed [AS::Notifications](rubydoc.info/gems/as-notifications/AS/Notifications). It can change if a different backend is used.
Subscribe to an event temporarily while the block runs.
The callback in the following example will be called for all “sql.active_record” events instrumented during the execution of the block. The callback is unsubscribed automatically after that.
157 158 159 |
# File 'lib/webmachine/events.rb', line 157 def subscribed(callback, name, &block) backend.subscribed(callback, name, &block) end |
.unsubscribe(subscriber) ⇒ Object
The documentation of this method describes its behaviour with the default backed [AS::Notifications](rubydoc.info/gems/as-notifications/AS/Notifications). It can change if a different backend is used.
Unsubscribes the given subscriber.
172 173 174 |
# File 'lib/webmachine/events.rb', line 172 def unsubscribe(subscriber) backend.unsubscribe(subscriber) end |