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.
59 60 61 62 63 64 65 |
# File 'lib/discover.rb', line 59 def initialize(client, name, address, attributes = {}, standby = false) @client = client @name = name @address = address @attributes = attributes @standby = standby end |
Instance Method Details
#register ⇒ Object
67 68 69 70 71 |
# File 'lib/discover.rb', line 67 def register send_register_request start_heartbeat wait_for_election if @standby end |
#send_register_request ⇒ Object
79 80 81 82 83 84 85 86 87 |
# File 'lib/discover.rb', line 79 def send_register_request args = { "Name" => @name, "Addr" => @address, "Attrs" => @attributes } @client.request("Agent.Register", args).value end |
#send_unregister_request ⇒ Object
89 90 91 92 93 94 95 96 |
# File 'lib/discover.rb', line 89 def send_unregister_request args = { "Name" => @name, "Addr" => @address } @client.request("Agent.Unregister", args).value end |
#start_heartbeat ⇒ Object
98 99 100 101 102 103 104 105 106 |
# File 'lib/discover.rb', line 98 def start_heartbeat @heartbeat = every(HEARTBEAT_INTERVAL) do @client.request( "Agent.Heartbeat", "Name" => @name, "Addr" => @address ) end end |
#stop_heartbeat ⇒ Object
108 109 110 |
# File 'lib/discover.rb', line 108 def stop_heartbeat @heartbeat.cancel end |
#unregister ⇒ Object
73 74 75 76 77 |
# File 'lib/discover.rb', line 73 def unregister stop_heartbeat send_unregister_request @client.remove_registration(self) end |
#wait_for_election ⇒ Object
112 113 114 115 |
# File 'lib/discover.rb', line 112 def wait_for_election async.watch_leaders wait :elected end |
#watch_leaders ⇒ Object
117 118 119 120 121 122 123 |
# File 'lib/discover.rb', line 117 def watch_leaders @client.service(@name).each_leader do |leader| if leader.address == @address signal :elected end end end |