78
78
def set_default_series(self, series):
79
79
return self._set_value("default_series", series)
81
def _key_path(self, key):
82
return "%s/%s" % (GLOBAL_SETTINGS, key)
82
85
def _get_value(self, key, default=None):
84
content, stat = yield self._client.get(GLOBAL_SETTINGS)
87
content, stat = yield self._client.get(self._key_path(key))
85
88
except zookeeper.NoNodeException:
86
89
returnValue(default)
87
data = yaml.load(content)
88
returnValue(data.get(key, default))
90
returnValue(yaml.load(content))
90
93
def _set_value(self, key, value):
94
exists = yield self._client.exists(GLOBAL_SETTINGS)
96
yield self._client.create(GLOBAL_SETTINGS)
92
98
def set_value(old_content, stat):
96
data = yaml.load(old_content)
98
return yaml.safe_dump(data)
100
return retry_change(self._client, GLOBAL_SETTINGS, set_value)
102
def watch_settings_changes(self, callback, error_callback=None):
99
return yaml.safe_dump(value)
101
result = yield retry_change(
102
self._client, self._key_path(key), set_value)
105
def watch_debug_log(self, callback, error_callback=None):
103
106
"""Register a callback to invoked when the runtime changes.
105
108
This watch primarily serves to get a persistent watch of the
115
118
consumed by the error callback.
117
120
assert callable(callback), "Invalid callback"
118
watcher = _RuntimeWatcher(self._client, callback, error_callback)
121
path = self._key_path("debug_log")
122
watcher = _RuntimeWatcher(self._client, path, callback, error_callback)
119
123
return watcher.start()
122
126
class _RuntimeWatcher(object):
124
def __init__(self, client, callback, error_callback=None):
128
def __init__(self, client, path, callback, error_callback=None):
125
129
self._client = client
126
131
self._callback = callback
127
132
self._watching = False
128
133
self._error_callback = error_callback
145
150
# This logic will break if the node is removed, and so will
146
151
# the function below, but the internal logic never removes
147
152
# it, so we do not handle this case.
148
exists_d, watch_d = self._client.exists_and_watch(GLOBAL_SETTINGS)
153
exists_d, watch_d = self._client.exists_and_watch(self._path)
149
154
exists = yield exists_d
167
172
if not self._watching or not self._client.connected:
168
173
returnValue(False)
170
exists_d, watch_d = self._client.exists_and_watch(GLOBAL_SETTINGS)
175
exists_d, watch_d = self._client.exists_and_watch(self._path)
173
178
yield self._callback(change_event)