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.
- #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.
Methods inherited from Base
#cast_ballot!, #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 = {}) ⇒ Observer
Returns a new instance of Observer.
339 340 341 342 343 344 345 346 |
# File 'lib/zk/election.rb', line 339 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
397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 |
# File 'lib/zk/election.rb', line 397 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
349 350 351 |
# File 'lib/zk/election.rb', line 349 def leader_alive #:nodoc: synchronize { @leader_alive } end |
#observe! ⇒ Object
364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 |
# File 'lib/zk/election.rb', line 364 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
354 355 356 |
# File 'lib/zk/election.rb', line 354 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
360 361 362 |
# File 'lib/zk/election.rb', line 360 def on_new_leader(&blk) @new_leader_cbs << blk end |