Class: Startback::Event::Engine
- Inherits:
-
Object
- Object
- Startback::Event::Engine
- Includes:
- Support::Robustness
- Defined in:
- lib/startback/event/engine.rb
Overview
This class is the starting point of event handling in Startback. It holds a Bus instance to which emitters and listeners can connect.
The Engine exposes a rack app (.rack_app) with a /healthcheck webservice. It is supposed to be mounted to a webserver such as puma.
This class goes hand in hand with the ‘startback:engine` docker image. It can be extended by subclasses to override the following methods:
- bus to use something else than a simple memory bus
- on_health_check to check specific health conditions
- create_agents to instantiate all listening agents
(unless auto_create_agents is used)
- rack_app if you want to customize the API running
Constant Summary collapse
- DEFAULT_OPTIONS =
{ }
Instance Attribute Summary collapse
-
#context ⇒ Object
readonly
Returns the value of attribute context.
-
#options ⇒ Object
readonly
Returns the value of attribute options.
Class Method Summary collapse
-
.auto_create_agents(base_class = nil) ⇒ Object
Register a base class which will be used to discover the agents to start when the engine is ran.
- .auto_create_agents? ⇒ Boolean
Instance Method Summary collapse
- #bus ⇒ Object
- #connect ⇒ Object
- #create_agents(type = :all) ⇒ Object
- #factor_event(event_data) ⇒ Object
-
#initialize(options = {}, context = Context.new) ⇒ Engine
constructor
A new instance of Engine.
-
#on_health_check ⇒ Object
This method is executed on health check and can be overriden by subclasses to perform specific checks.
- #rack_app ⇒ Object
Methods included from Support::Robustness
#log, #monitor, #stop_errors, #try_max_times
Constructor Details
#initialize(options = {}, context = Context.new) ⇒ Engine
Returns a new instance of Engine.
30 31 32 33 34 |
# File 'lib/startback/event/engine.rb', line 30 def initialize( = {}, context = Context.new) @options = DEFAULT_OPTIONS.merge() @context = context @context.engine = self end |
Instance Attribute Details
#context ⇒ Object (readonly)
Returns the value of attribute context.
35 36 37 |
# File 'lib/startback/event/engine.rb', line 35 def context @context end |
#options ⇒ Object (readonly)
Returns the value of attribute options.
35 36 37 |
# File 'lib/startback/event/engine.rb', line 35 def @options end |
Class Method Details
.auto_create_agents(base_class = nil) ⇒ Object
Register a base class which will be used to discover the agents to start when the engine is ran.
44 45 46 47 |
# File 'lib/startback/event/engine.rb', line 44 def auto_create_agents(base_class = nil) @auto_create_agents ||= base_class @auto_create_agents end |
.auto_create_agents? ⇒ Boolean
38 39 40 |
# File 'lib/startback/event/engine.rb', line 38 def auto_create_agents? !!@auto_create_agents end |
Instance Method Details
#bus ⇒ Object
56 57 58 |
# File 'lib/startback/event/engine.rb', line 56 def bus @bus ||= ::Startback::Event::Bus.new end |
#connect ⇒ Object
60 61 62 63 |
# File 'lib/startback/event/engine.rb', line 60 def connect log(:debug, "Connecting to the bus now!", self) bus.connect end |
#create_agents(type = :all) ⇒ Object
65 66 67 68 69 70 71 72 |
# File 'lib/startback/event/engine.rb', line 65 def create_agents(type = :all) return unless parent = self.class.auto_create_agents ObjectSpace .each_object(Class) .select { |klass| klass <= parent } .each { |klass| klass.new(self, type) } end |
#factor_event(event_data) ⇒ Object
74 75 76 |
# File 'lib/startback/event/engine.rb', line 74 def factor_event(event_data) Event.json(event_data, context) end |
#on_health_check ⇒ Object
This method is executed on health check and can be overriden by subclasses to perform specific checks.
52 53 54 |
# File 'lib/startback/event/engine.rb', line 52 def on_health_check "Ok" end |