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.

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

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.



30
31
32
33
34
# File 'lib/startback/event/engine.rb', line 30

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.



35
36
37
# File 'lib/startback/event/engine.rb', line 35

def context
  @context
end

#optionsObject (readonly)

Returns the value of attribute options.



35
36
37
# File 'lib/startback/event/engine.rb', line 35

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.



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

Returns:

  • (Boolean)


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

def auto_create_agents?
  !!@auto_create_agents
end

Instance Method Details

#busObject



56
57
58
# File 'lib/startback/event/engine.rb', line 56

def bus
  @bus ||= ::Startback::Event::Bus.new
end

#connectObject



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_checkObject

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

#rack_appObject



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

def rack_app
  engine = self
  Rack::Builder.new do
    use Startback::Web::CatchAll

    map '/health-check' do
      health = Startback::Web::HealthCheck.new {
        engine.on_health_check
      }
      run(health)
    end
  end
end