Class: Startback::Event::Engine

Inherits:
Object
  • Object
show all
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, and the possibility for the the listening part to start an infinite loop (ServerEngine).

The Engine automatically runs a Webrick small webapp with a /healthcheck webservice. The class can be extended and method ‘on_health_check` overriden to run specific checks.

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)

Defined Under Namespace

Classes: Runner

Constant Summary collapse

DEFAULT_OPTIONS =
{

  # To be passed to ServerEngine
  server_engine: {}

}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

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.



36
37
38
39
40
# File 'lib/startback/event/engine.rb', line 36

def initialize(options = {}, context = Context.new)
  @options = DEFAULT_OPTIONS.merge(options)
  @context = context
  @context.engine = self
end

Instance Attribute Details

#contextObject (readonly)

Returns the value of attribute context.



41
42
43
# File 'lib/startback/event/engine.rb', line 41

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



41
42
43
# File 'lib/startback/event/engine.rb', line 41

def options
  @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.



50
51
52
53
# File 'lib/startback/event/engine.rb', line 50

def auto_create_agents(base_class = nil)
  @auto_create_agents ||= base_class
  @auto_create_agents
end

.auto_create_agents?Boolean

Returns:

  • (Boolean)


44
45
46
# File 'lib/startback/event/engine.rb', line 44

def auto_create_agents?
  !!@auto_create_agents
end

Instance Method Details

#busObject



62
63
64
# File 'lib/startback/event/engine.rb', line 62

def bus
  ::Startback::Event::Bus.new
end

#connectObject



66
67
68
69
# File 'lib/startback/event/engine.rb', line 66

def connect
  log(:info, self, "Connecting to the bus now!")
  bus.connect
end

#create_agentsObject



79
80
81
82
83
84
85
86
# File 'lib/startback/event/engine.rb', line 79

def create_agents
  return unless parent = self.class.auto_create_agents

  ObjectSpace
    .each_object(Class)
    .select { |klass| klass < parent }
    .each { |klass| klass.new(self) }
end

#factor_event(event_data) ⇒ Object



88
89
90
# File 'lib/startback/event/engine.rb', line 88

def factor_event(event_data)
  Event.json(event_data, context)
end

#on_health_checkObject

This method is executed on health check and can be overriden by subclasses to perform specific checks.



58
59
60
# File 'lib/startback/event/engine.rb', line 58

def on_health_check
  "Ok"
end

#run(options = {}) ⇒ Object



71
72
73
74
75
76
77
# File 'lib/startback/event/engine.rb', line 71

def run(options = {})
  connect

  log(:info, self, "Running agents and server engine!")
  create_agents
  Runner.new(self, options[:server_engine] || {}).run
end