Class: Startback::Event::Engine::Runner

Inherits:
Object
  • Object
show all
Defined in:
lib/startback/event/engine.rb

Constant Summary collapse

DEFAULT_SERVER_ENGINE_OPTIONS =
{
  daemonize: false,
  worker_type: 'process',
  workers: 1
}

Instance Attribute Summary collapse

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(engine, options = {}) ⇒ Runner

Returns a new instance of Runner.

Raises:

  • (ArgumentError)


100
101
102
103
104
105
106
# File 'lib/startback/event/engine.rb', line 100

def initialize(engine, options = {})
  raise ArgumentError if engine.nil?

  @engine = engine
  @options = DEFAULT_SERVER_ENGINE_OPTIONS.merge(options)
  require 'serverengine'
end

Instance Attribute Details

#engineObject (readonly)

Returns the value of attribute engine.



107
108
109
# File 'lib/startback/event/engine.rb', line 107

def engine
  @engine
end

#optionsObject (readonly)

Returns the value of attribute options.



107
108
109
# File 'lib/startback/event/engine.rb', line 107

def options
  @options
end

Class Method Details

.build_health_check(engine) ⇒ Object



122
123
124
125
126
127
128
129
130
131
# File 'lib/startback/event/engine.rb', line 122

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

.build_worker(engine, health) ⇒ Object



133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
# File 'lib/startback/event/engine.rb', line 133

def build_worker(engine, health)
  Module.new do
    include Support::Env

    def initialize
      @stop_flag = ServerEngine::BlockingFlag.new
    end

    define_method(:health) do
      health
    end

    define_method(:engine) do
      engine
    end

    def run
      ran = false
      until @stop_flag.set?
        if ran
          engine.send(:log, :warn, engine, "Restarting internal loop")
        else
          engine.send(:log, :info, engine, "Starting internal loop")
        end
        Rack::Handler::WEBrick.run(health, {
          :Port => env('STARTBACK_ENGINE_PORT', '3000').to_i,
          :Host => env('STARTBACK_ENGINE_LISTEN', '0.0.0.0')
        })
        ran = true
      end
    end

    def stop
      engine.send(:log, :info, engine, "Stopping internal loop")
      @stop_flag.set!
      Rack::Handler::WEBrick.shutdown
    end
  end
end

.run(*args, &bl) ⇒ Object



118
119
120
# File 'lib/startback/event/engine.rb', line 118

def run(*args, &bl)
  new.run(*args, &bl)
end

Instance Method Details

#run(options = {}) ⇒ Object



109
110
111
112
113
114
115
# File 'lib/startback/event/engine.rb', line 109

def run(options = {})
  health = self.class.build_health_check(engine)
  worker = self.class.build_worker(engine, health)
  se = ServerEngine.create(nil, worker, options)
  se.run
  se
end