Class: Discover::Registration
- Inherits:
-
Object
- Object
- Discover::Registration
- Includes:
- Celluloid
- Defined in:
- lib/discover.rb
Constant Summary collapse
- HEARTBEAT_INTERVAL =
5
Instance Method Summary collapse
-
#initialize(client, name, address, attributes = {}, standby = false) ⇒ Registration
constructor
A new instance of Registration.
- #register ⇒ Object
- #send_register_request ⇒ Object
- #send_unregister_request ⇒ Object
- #start_heartbeat ⇒ Object
- #stop_heartbeat ⇒ Object
- #unregister ⇒ Object
- #wait_for_election ⇒ Object
- #watch_leaders ⇒ Object
Constructor Details
#initialize(client, name, address, attributes = {}, standby = false) ⇒ Registration
Returns a new instance of Registration.
51 52 53 54 55 56 57 |
# File 'lib/discover.rb', line 51 def initialize(client, name, address, attributes = {}, standby = false) @client = client @name = name @address = address @attributes = attributes @standby = standby end |
Instance Method Details
#register ⇒ Object
59 60 61 62 63 |
# File 'lib/discover.rb', line 59 def register send_register_request start_heartbeat wait_for_election if @standby end |
#send_register_request ⇒ Object
71 72 73 74 75 76 77 78 79 |
# File 'lib/discover.rb', line 71 def send_register_request args = { "Name" => @name, "Addr" => @address, "Attrs" => @attributes } @client.request("Agent.Register", args).value end |
#send_unregister_request ⇒ Object
81 82 83 84 85 86 87 88 |
# File 'lib/discover.rb', line 81 def send_unregister_request args = { "Name" => @name, "Addr" => @address } @client.request("Agent.Unregister", args).value end |
#start_heartbeat ⇒ Object
90 91 92 93 94 95 96 97 98 |
# File 'lib/discover.rb', line 90 def start_heartbeat @heartbeat = every(HEARTBEAT_INTERVAL) do @client.request( "Agent.Heartbeat", "Name" => @name, "Addr" => @address ) end end |
#stop_heartbeat ⇒ Object
100 101 102 |
# File 'lib/discover.rb', line 100 def stop_heartbeat @heartbeat.cancel end |
#unregister ⇒ Object
65 66 67 68 69 |
# File 'lib/discover.rb', line 65 def unregister stop_heartbeat send_unregister_request @client.remove_registration(self) end |
#wait_for_election ⇒ Object
104 105 106 107 |
# File 'lib/discover.rb', line 104 def wait_for_election async.watch_leaders wait :elected end |
#watch_leaders ⇒ Object
109 110 111 112 113 114 115 |
# File 'lib/discover.rb', line 109 def watch_leaders @client.service(@name).each_leader do |leader| if leader.address == @address signal :elected end end end |