Class: Serf::Runners::EmRunner

Inherits:
Object
  • Object
show all
Defined in:
lib/serf/runners/em_runner.rb

Overview

This runner simply wraps another runner to execute in the EventMachine deferred threadpool.

NOTE: Because the Serfer class validates messages before

sending them to runners (and handlers), this class simply
responds to the calling client with an 'MessageAcceptedEvent'
to signal that the message will be processed later.

Errors caught here will simply be logged. This is because the wrapped runner MUST handle its own errors. If an error should propagate up here, then it was most likely an error that occurred in a rescue block… we don’t want to complicate any extra publishing to error channels because that may have been the cause of the error.

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ EmRunner

Returns a new instance of EmRunner.



27
28
29
30
31
32
33
34
35
36
# File 'lib/serf/runners/em_runner.rb', line 27

def initialize(options={})
  # Manditory: Need a runner because EmRunner is just a wrapper.
  @runner = options.fetch(:runner)

  @mae_class = options.fetch(:message_accepted_event_class) {
    ::Serf::Messages::MessageAcceptedEvent
  }
  @evm = options.fetch(:event_machine) { ::EventMachine }
  @logger = options.fetch(:logger) { ::Serf::Util::NullObject.new }
end

Class Method Details

.build(options = {}) ⇒ Object



51
52
53
54
55
56
57
# File 'lib/serf/runners/em_runner.rb', line 51

def self.build(options={})
  options[:runner] = options.fetch(:runner) {
    factory = options[:runner_factory] || ::Serf::Runners::DirectRunner
    factory.build options
  }
  self.new options
end

Instance Method Details

#run(endpoints, env) ⇒ Object



38
39
40
41
42
43
44
45
46
47
48
49
# File 'lib/serf/runners/em_runner.rb', line 38

def run(endpoints, env)
  endpoints = endpoints.dup
  env = env.dup
  @evm.defer(proc do
    begin
      @runner.run endpoints, env
    rescue => e
      @logger.error "#{e.inspect}\n\n#{e.backtrace.join("\n")}"
    end
  end)
  return @mae_class.new message: env
end