Class: ExaZK
- Inherits:
-
Object
- Object
- ExaZK
- Defined in:
- lib/exazk.rb
Instance Method Summary collapse
- #announce_routes ⇒ Object
-
#initialize(mutex, zk_string, routes, data) ⇒ ExaZK
constructor
A new instance of ExaZK.
- #main_loop ⇒ Object
- #mutex_exists? ⇒ Boolean
- #send_update(route) ⇒ Object
- #withdraw_routes ⇒ Object
Constructor Details
#initialize(mutex, zk_string, routes, data) ⇒ ExaZK
Returns a new instance of ExaZK.
6 7 8 9 10 11 12 13 14 15 |
# File 'lib/exazk.rb', line 6 def initialize(mutex, zk_string, routes, data) @mutex = mutex @zk_string = zk_string @routes = routes @data = { hostname: Socket.gethostname, data: data }.to_json @zk = ZK.new(@zk_string) end |
Instance Method Details
#announce_routes ⇒ Object
28 29 30 31 32 |
# File 'lib/exazk.rb', line 28 def announce_routes @routes.map do |route| route % 'announce' end end |
#main_loop ⇒ Object
48 49 50 51 52 53 54 55 56 |
# File 'lib/exazk.rb', line 48 def main_loop withdraw_routes.each do |route| send_update(route) end unless mutex_exists? announce_routes.each do |route| send_update(route) end if mutex_exists? end |
#mutex_exists? ⇒ Boolean
34 35 36 37 38 39 40 41 42 43 44 45 46 |
# File 'lib/exazk.rb', line 34 def mutex_exists? if @zk.exists?(@mutex) data = JSON.parse(@zk.get(@mutex).first) return true if data['hostname'] == Socket.gethostname else begin @zk.create(@mutex, "#{@data}", :ephemeral => true) return true rescue ZK::Exceptions::NodeExists return false end end end |
#send_update(route) ⇒ Object
17 18 19 20 |
# File 'lib/exazk.rb', line 17 def send_update(route) $stdout.write route $stdout.flush end |
#withdraw_routes ⇒ Object
22 23 24 25 26 |
# File 'lib/exazk.rb', line 22 def withdraw_routes @routes.map do |route| route % 'withdraw' end end |