Created
September 14, 2012 19:48
-
-
Save eric/3724271 to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Zookeeper | |
class BroadcastTrigger | |
def initialize(zk, path) | |
@zk = zk | |
@path = path | |
end | |
def trigger | |
@zk.set(@path, "#{$$}") | |
rescue ZK::Exceptions::NoNode | |
@zk.mkdir_p(@path, :data => "#{$$}") | |
end | |
def watch(&block) | |
@callback = block | |
configure_watches | |
self | |
end | |
def stop | |
if @register | |
@register.unregister | |
end | |
if @on_connected | |
@on_connected.unregister | |
end | |
end | |
def configure_watches | |
@register ||= @zk.register(@path) do | |
if stat = @zk.stat(@path, :watch => true) | |
update_version(stat.version) | |
end | |
end | |
@on_connected ||= @zk.on_connected do | |
if stat = @zk.stat(@path, :watch => true) | |
update_version(stat.version) | |
end | |
end | |
if stat = @zk.stat(@path, :watch => true) | |
@version = stat.version | |
end | |
end | |
def update_version(new_version) | |
old_version, @version = @version, new_version | |
if old_version != new_version | |
@callback.call | |
end | |
end | |
end | |
end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment