Class: Majordomo::Broker
- Inherits:
-
Object
- Object
- Majordomo::Broker
- Defined in:
- lib/majordomo/broker.rb,
lib/majordomo/broker/worker.rb,
lib/majordomo/broker/service.rb
Defined Under Namespace
Instance Attribute Summary collapse
-
#services ⇒ Object
Returns the value of attribute services.
-
#socket ⇒ Object
Returns the value of attribute socket.
-
#waiting ⇒ Object
Returns the value of attribute waiting.
-
#workers ⇒ Object
Returns the value of attribute workers.
Instance Method Summary collapse
- #bind(endpoint) ⇒ Object
- #destroy ⇒ Object
-
#initialize(bind = 'tcp://*:5555', context = ZMQ::Context.new) ⇒ Broker
constructor
A new instance of Broker.
- #mediate ⇒ Object
- #message_client(sender, message) ⇒ Object
- #message_worker(sender, message) ⇒ Object
- #purge ⇒ Object
Constructor Details
#initialize(bind = 'tcp://*:5555', context = ZMQ::Context.new) ⇒ Broker
Returns a new instance of Broker.
9 10 11 12 13 14 15 16 17 18 19 20 21 22 |
# File 'lib/majordomo/broker.rb', line 9 def initialize(bind = 'tcp://*:5555', context = ZMQ::Context.new) @context = context @socket = @context.socket(ZMQ::ROUTER) @poller = ZMQ::Poller.new @services = {} @workers = {} @waiting = [] @heartbeat_at = Time.now + 0.001 * HEARTBEAT_INTERVAL @socket.bind(bind) @poller.register(@socket, ZMQ::POLLIN) trap(:INT) { exit } at_exit { destroy } end |
Instance Attribute Details
#services ⇒ Object
Returns the value of attribute services.
7 8 9 |
# File 'lib/majordomo/broker.rb', line 7 def services @services end |
#socket ⇒ Object
Returns the value of attribute socket.
7 8 9 |
# File 'lib/majordomo/broker.rb', line 7 def socket @socket end |
#waiting ⇒ Object
Returns the value of attribute waiting.
7 8 9 |
# File 'lib/majordomo/broker.rb', line 7 def waiting @waiting end |
#workers ⇒ Object
Returns the value of attribute workers.
7 8 9 |
# File 'lib/majordomo/broker.rb', line 7 def workers @workers end |
Instance Method Details
#bind(endpoint) ⇒ Object
57 58 59 |
# File 'lib/majordomo/broker.rb', line 57 def bind(endpoint) @socket.bind(endpoint) end |
#destroy ⇒ Object
52 53 54 55 |
# File 'lib/majordomo/broker.rb', line 52 def destroy @socket.close @context.terminate end |
#mediate ⇒ Object
24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 |
# File 'lib/majordomo/broker.rb', line 24 def mediate loop do items = @poller.poll(HEARTBEAT_INTERVAL) if items > 0 @socket.recv_strings( = []) sender = .shift .shift #empty header = .shift case header when CLIENT (sender, ) when WORKER (sender, ) end end if Time.now > @heartbeat_at purge waiting.each do |worker| worker.(HEARTBEAT) end @heartbeat_at = Time.now + 0.001 * HEARTBEAT_INTERVAL puts "Heartbeat #{waiting.count}" end end end |
#message_client(sender, message) ⇒ Object
108 109 110 111 112 113 114 115 116 117 |
# File 'lib/majordomo/broker.rb', line 108 def (sender, ) service_name = .shift service = Service.require(self, service_name) .unshift nil .unshift sender service.requests << service.dispatch end |
#message_worker(sender, message) ⇒ Object
61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 |
# File 'lib/majordomo/broker.rb', line 61 def (sender, ) command = .shift identity = sender.unpack('H*').first worker_ready = !!@workers[identity] worker = Worker.require(self, sender) case command when READY if worker_ready worker.delete(true) else service = .shift worker.service = Service.require(self, service) @waiting << worker worker.service.waiting << worker worker.service.workers += 1 worker.expires_at = Time.now + 0.001 * HEARTBEAT_EXPIRY worker.service.dispatch end when REPLY if worker_ready client = .shift .shift .unshift(worker.service.name) .unshift(CLIENT) .unshift(nil) .unshift(client) @socket.send_strings() else worker.delete(true) end when HEARTBEAT if worker_ready if @waiting.any? @waiting.delete(worker) @waiting << worker end worker.expires_at = Time.now + 0.001 * HEARTBEAT_EXPIRY else worker.delete(true) end when DISCONNECT worker.delete(false) end end |
#purge ⇒ Object
119 120 121 122 123 |
# File 'lib/majordomo/broker.rb', line 119 def purge waiting.each do |worker| worker.delete(false) if Time.now > worker.expires_at end end |