Class: ZK::Election::Candidate
Overview
This class is for registering candidates in the leader election. This instance will participate in votes for becoming the leader and will be notified in the case where it needs to take over.
if data is given, it will be used as the content of both our ballot and the leader acknowledgement node if and when we become the leader.
Instance Attribute Summary
Attributes inherited from Base
#root_election_node, #vote_path, #zk
Instance Method Summary collapse
-
#initialize(client, name, opts = {}) ⇒ Candidate
constructor
A new instance of Candidate.
- #leader? ⇒ Boolean
-
#on_losing_election(&block) ⇒ Object
When we lose the election and are relegated to the shadows, waiting for the leader to make one small misstep, where we can finally claim what is rightfully ours! MWUAHAHAHAHAHA(cough).
-
#on_takeover_error ⇒ Object
These procs should be run in the case of an error when trying to assume the leadership role.
-
#on_winning_election(&block) ⇒ Object
When we win the election, we will call the procs registered using this method.
-
#vote! ⇒ Object
volunteer to become the leader.
-
#voted? ⇒ Boolean
true if leader has been determined at least once (used in tests).
Methods inherited from Base
#cast_ballot!, #close, #leader_ack_path, #leader_acked?, #leader_data, #on_leader_ack, #root_vote_path
Methods included from Logger
#logger, wrapped_logger, wrapped_logger=
Constructor Details
#initialize(client, name, opts = {}) ⇒ Candidate
Returns a new instance of Candidate.
191 192 193 194 195 196 197 198 199 200 201 202 203 |
# File 'lib/zk/election.rb', line 191 def initialize(client, name, opts={}) super(client, name, opts) opts = DEFAULT_OPTS.merge(opts) @leader = nil @data = opts[:data] || '' @vote_path = nil @winner_callbacks = [] @loser_callbacks = [] @next_node_ballot_sub = nil # the subscription for next-node failure end |
Instance Method Details
#leader? ⇒ Boolean
205 206 207 |
# File 'lib/zk/election.rb', line 205 def leader? false|@leader end |
#on_losing_election(&block) ⇒ Object
When we lose the election and are relegated to the shadows, waiting for the leader to make one small misstep, where we can finally claim what is rightfully ours! MWUAHAHAHAHAHA(cough)
223 224 225 |
# File 'lib/zk/election.rb', line 223 def on_losing_election(&block) @loser_callbacks << block end |
#on_takeover_error ⇒ Object
These procs should be run in the case of an error when trying to assume the leadership role. This should probably be a "hara-kiri" or STONITH type procedure (i.e. kill the candidate)
231 232 233 |
# File 'lib/zk/election.rb', line 231 def on_takeover_error #:nodoc: raise NotImplementedError end |
#on_winning_election(&block) ⇒ Object
When we win the election, we will call the procs registered using this method.
216 217 218 |
# File 'lib/zk/election.rb', line 216 def on_winning_election(&block) @winner_callbacks << block end |
#vote! ⇒ Object
volunteer to become the leader. if we win, on_winning_election blocks will be called, otherwise, wait for next election
+data+ will be placed in the znode representing our vote
239 240 241 242 243 244 245 |
# File 'lib/zk/election.rb', line 239 def vote! synchronize do clear_next_node_ballot_sub! cast_ballot!(@data) unless @vote_path check_election_results! end end |
#voted? ⇒ Boolean
true if leader has been determined at least once (used in tests)
210 211 212 |
# File 'lib/zk/election.rb', line 210 def voted? #:nodoc: !@leader.nil? end |