Class: Buster::Worker

Inherits:
Object
  • Object
show all
Defined in:
lib/buster/worker.rb

Class Method Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(context) ⇒ Worker

Returns a new instance of Worker.



3
4
5
# File 'lib/buster/worker.rb', line 3

def initialize(context)
  @context = context
end

Class Method Details

.start_worker(context) ⇒ Object



34
35
36
37
38
# File 'lib/buster/worker.rb', line 34

def self.start_worker(context)
  worker = Worker.new(context)
  t = Thread.new { worker.run }
  t.run
end

Instance Method Details

#runObject



7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
# File 'lib/buster/worker.rb', line 7

def run
  worker = @context.socket(ZMQ::DEALER)
  worker.connect("inproc://workers")
  while true
    worker.recv_strings(msgs = [])
    return if msgs.length < 2

    body = MessagePack.unpack(msgs.pop)
    message_name = msgs.pop
    reply_id = msgs.pop

    handler = find_handler message_name
    if handler.nil?
      puts "No handler found for #{message_name}"
      return
    end

    handler.reply_action = lambda do |name,props|
      worker.send_strings([reply_id, name.to_s, MessagePack.pack(props)])
    end

    handler.execute body
  end
  worker.close
end