Class: Cocaine::Worker

Inherits:
Object show all
Defined in:
lib/cocaine/server/worker.rb

Instance Method Summary collapse

Constructor Details

#initialize(options = {}) ⇒ Worker

Returns a new instance of Worker.



12
13
14
15
16
17
18
19
20
21
22
# File 'lib/cocaine/server/worker.rb', line 12

def initialize(options={})
  options = {
      endpoint: '',
      uuid: '',
  }.merge(options)

  @endpoint = options[:endpoint]
  @uuid = options[:uuid]

  @sandbox = Cocaine::Sandbox.new
end

Instance Method Details

#invoke(event, request, response) ⇒ Object



47
48
49
# File 'lib/cocaine/server/worker.rb', line 47

def invoke(event, request, response)
  @sandbox.invoke(event, request, response)
end

#on(event, handler) ⇒ Object



24
25
26
# File 'lib/cocaine/server/worker.rb', line 24

def on(event, handler)
  @sandbox.on event, handler
end

#runObject



28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
# File 'lib/cocaine/server/worker.rb', line 28

def run
  EM.error_handler { |error|
    short_reason = error.inspect
    traceback = error.backtrace.join("\n")
    $log.warn "error caught at the top of event loop:\n#{short_reason}\n#{traceback}"
  }

  EM.run do
    $log.debug 'starting worker'
    $log.debug "connecting to the #{@endpoint}"
    EM.connect @endpoint, nil, Cocaine::Connection do |conn|
      $log.debug "connection established with #{@endpoint}"
      @dispatcher = Cocaine::WorkerDispatcher.new self, conn
      @dispatcher.send_handshake 0, @uuid
      @dispatcher.send_heartbeat 0
    end
  end
end

#terminate(errno, reason) ⇒ Object



51
52
53
54
55
56
# File 'lib/cocaine/server/worker.rb', line 51

def terminate(errno, reason)
  $log.debug "terminating with: [#{errno}] #{reason}"
  @dispatcher.send_terminate 0, errno, reason
  EM.stop
  exit(errno)
end