Class: Breakout::Worker::App

Inherits:
Object
  • Object
show all
Includes:
API
Defined in:
lib/breakout/app.rb

Instance Attribute Summary collapse

Instance Method Summary collapse

Methods included from API

#disconnect, #done_work, #send_messages

Instance Attribute Details

#socketObject

Returns the value of attribute socket.



6
7
8
# File 'lib/breakout/app.rb', line 6

def socket
  @socket
end

#stopObject

Returns the value of attribute stop.



6
7
8
# File 'lib/breakout/app.rb', line 6

def stop
  @stop
end

#worker_by_routeObject

Returns the value of attribute worker_by_route.



6
7
8
# File 'lib/breakout/app.rb', line 6

def worker_by_route
  @worker_by_route
end

Instance Method Details

#dispatch(data) ⇒ Object



8
9
10
11
12
# File 'lib/breakout/app.rb', line 8

def dispatch(data)
  route, bid, message = data.split("\n", 3)
  raise "#{route}\n#{bid}\n#{message}" unless worker = worker_by_route[route]
  worker.do_work(bid, message)
end

#run(url = nil) ⇒ Object



14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/breakout/app.rb', line 14

def run(url=nil)
  unless url
    url = Breakout.worker_url
  end
  
  self.socket = Socket.new(url)
  self.worker_by_route = Hash.new
  Worker::WORKERS.each do |klass|
    worker = klass.new
    worker.socket = socket
    worker_by_route[klass.route] = worker
  end

  done_work
  while data = socket.receive() do
    dispatch(data)
    if @stop
      done_work false
      break
    end
    done_work
  end

end