Class: ZK::Election::Observer
Instance Attribute Summary
Attributes inherited from Base
#root_election_node, #vote_path, #zk
Instance Method Summary collapse
- #close ⇒ Object
-
#initialize(client, name, opts = {}) ⇒ Observer
constructor
A new instance of Observer.
-
#leader_alive ⇒ Object
our current idea about the state of the election.
- #long_live_the_king ⇒ Object protected
- #observe! ⇒ Object
-
#on_leaders_death(&blk) ⇒ Object
register callbacks that should be fired when a leader dies.
-
#on_new_leader(&blk) ⇒ Object
register callbacks for when the new leader has acknowledged their role returns a subscription object that can be used to cancel further events.
- #the_king_is_dead ⇒ Object protected
Methods inherited from Base
#cast_ballot!, #create_root_path!, #digit, #leader_ack_path, #leader_acked?, #leader_data, #on_leader_ack, #root_vote_path, #safe_call, #synchronize, #vote_basename
Constructor Details
#initialize(client, name, opts = {}) ⇒ Observer
Returns a new instance of Observer.
327 328 329 330 331 332 333 334 |
# File 'lib/zk/election.rb', line 327 def initialize(client, name, opts={}) super @leader_death_cbs = [] @new_leader_cbs = [] @deletion_sub = @creation_sub = nil @leader_alive = nil @observing = false end |
Instance Method Details
#close ⇒ Object
385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 |
# File 'lib/zk/election.rb', line 385 def close synchronize do return unless @observing @deletion_sub.unregister if @deletion_sub @creation_sub.unregister if @creation_sub @deletion_sub = @creation_sub = nil @leader_death_cbs.clear @new_leader_cbs.clear @leader_alive = nil @observing = false end end |
#leader_alive ⇒ Object
our current idea about the state of the election
337 338 339 |
# File 'lib/zk/election.rb', line 337 def leader_alive #:nodoc: synchronize { @leader_alive } end |
#long_live_the_king ⇒ Object (protected)
412 413 414 415 416 417 418 419 |
# File 'lib/zk/election.rb', line 412 def long_live_the_king synchronize do safe_call(*@new_leader_cbs) @leader_alive = true end the_king_is_dead unless leader_acked?(true) end |
#observe! ⇒ Object
352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 |
# File 'lib/zk/election.rb', line 352 def observe! synchronize do return if @observing @observing = true @leader_ack_sub ||= @zk.register(leader_ack_path) do |event| logger.debug { "leader_ack_callback, event.node_deleted? #{event.node_deleted?}, event.node_created? #{event.node_created?}" } if event.node_deleted? the_king_is_dead elsif event.node_created? long_live_the_king else acked = leader_acked?(true) # If the current state of the system is not what we think it should be # a transition has occurred and we should fire our callbacks if (acked and !@leader_alive) long_live_the_king elsif (!acked and @leader_alive) the_king_is_dead else # things are how we think they should be, so just wait for the # watch to fire end end end leader_acked?(true) ? long_live_the_king : the_king_is_dead end end |
#on_leaders_death(&blk) ⇒ Object
register callbacks that should be fired when a leader dies
342 343 344 |
# File 'lib/zk/election.rb', line 342 def on_leaders_death(&blk) @leader_death_cbs << blk end |
#on_new_leader(&blk) ⇒ Object
register callbacks for when the new leader has acknowledged their role returns a subscription object that can be used to cancel further events
348 349 350 |
# File 'lib/zk/election.rb', line 348 def on_new_leader(&blk) @new_leader_cbs << blk end |
#the_king_is_dead ⇒ Object (protected)
403 404 405 406 407 408 409 410 |
# File 'lib/zk/election.rb', line 403 def the_king_is_dead synchronize do safe_call(*@leader_death_cbs) @leader_alive = false end long_live_the_king if leader_acked?(true) end |