Class: Shamu::Events::EventsService
- Inherits:
-
Services::Service
- Object
- Services::Service
- Shamu::Events::EventsService
- Defined in:
- lib/shamu/events/events_service.rb
Overview
The EventsService handles receiving messages (#publish) and dispatching them to all registered subscribers (#subscriber). The actual delivery and message transport is defined by the concrete implementations of the EventService. See "Included Event Systems" below.
Use .
or /
to namespace and group channels. Channels are not related
to each other but namespacing can help organize and group channels in
reports and back-end tools.
Events are not guaranteed to be delivered and may be delivered more than once. Event processing should be idempotent and resilient to message loss.
Included Event Systems
- In Memory intended for decoupling services all running withing the same process. This is the default.
- ActiveRecord for low volume high-latency communications in a smaller system.
Selecting an Event System
Shamu relies on Scorpion to resolve dependencies. To select which event system to use, prepare the scorpion with specific hunting instructions.
Scorpion.prepare do
capture Shamu::Events::ActiveRecord::Service
end
Direct Known Subclasses
Class Method Summary collapse
-
.bridge(from, to, *channels)
Subscribe to the given channels from one service and forward them to another service.
-
.create(scorpion, *args, &block) ⇒ EventsService
Prepare the default event service implementation to use.
Instance Method Summary collapse
-
#deserialize(raw) ⇒ Message
Deserialize a message back to a Message instance.
-
#publish(channel, message)
Publish a well-defined Message to a known channel so that any client that has subscribed will receive a copy of the message to process.
-
#serialize(message) ⇒ String
Serialize a message so that it can be transfered from publisher to subsriber.
-
#subscribe(channel) {|message| ... }
Subscribe to receive notifications of events on the named channel.
Methods inherited from Services::Service
#cache_for, #cached_lookup, #entity_list, #entity_lookup_list, #find_by_lookup, #lazy_association, #lookup_association
Class Method Details
.bridge(from, to, *channels)
This method returns an undefined value.
Subscribe to the given channels from one service and forward them to another service.
87 88 89 90 91 92 93 |
# File 'lib/shamu/events/events_service.rb', line 87 def self.bridge( from, to, *channels ) Array( channels ).each do |channel| from.subscribe( channel ) do || to.publish channel, end end end |
.create(scorpion, *args, &block) ⇒ EventsService
Prepare the default event service implementation to use. The default event service can be overridden when setting up the scorpion.
50 51 52 |
# File 'lib/shamu/events/events_service.rb', line 50 def self.create( scorpion, *args, &block ) @events_service ||= scorpion.fetch InMemory::Service end |
Instance Method Details
#deserialize(raw) ⇒ Message
Deserialize a message back to a Message instance.
116 117 118 119 120 |
# File 'lib/shamu/events/events_service.rb', line 116 def deserialize( raw ) hash = MultiJson.load( raw ) = hash["class"].constantize scorpion.fetch , hash["attributes"] end |
#publish(channel, message)
This method returns an undefined value.
Publish a well-defined Message to a known channel so that any client that has subscribed will receive a copy of the message to process.
Events are delivered asynchronously. There is no guarantee that a subscriber has received or processed a message.
64 65 66 |
# File 'lib/shamu/events/events_service.rb', line 64 def publish( channel, ) fail NotImplementedError end |
#serialize(message) ⇒ String
Serialize a message so that it can be transfered from publisher to subsriber.
104 105 106 107 108 |
# File 'lib/shamu/events/events_service.rb', line 104 def serialize( ) MultiJson.dump \ class: .class.name, attributes: .to_attributes end |
#subscribe(channel) {|message| ... }
This method returns an undefined value.
Subscribe to receive notifications of events on the named channel. Any
time a publisher pushes a message callback
will be invoked with a copy
of the message.
76 77 78 |
# File 'lib/shamu/events/events_service.rb', line 76 def subscribe( channel, &callback ) fail NotImplementedError end |