Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
async def exists(self, path, watch=False):
path = self.normalize_path(path)
try:
await self.send(protocol.ExistsRequest(path=path, watch=watch))
except exc.NoNode:
return False
return True
async def watch_loop(self, path):
while self.callbacks[path]:
log.debug("Fetching data for %s", path)
try:
result = await self.fetch(path)
except exc.NoNode:
result = exc.NoNode
except exc.ZKError as e:
log.exception('Exception in watch loop: {}'.format(e))
log.info('Waiting for safe state...')
await self.client.session.ensure_safe_state()
continue
except Exception:
log.exception('Not handled in watch loop:')
raise
for callback in self.callbacks[path].copy():
maybe_future(callback(result), loop=self.client.loop)
if WatchEvent.CREATED not in self.watched_events and result == exc.NoNode:
return
try:
await self.client.wait_for_events(self.watched_events, path)
log.debug("Leaving double barrier %s", self.base_path)
owned_positions, participants = await self.analyze_siblings()
while len(participants) > 1:
if owned_positions["worker"] == 0:
await self.wait_on_sibling(participants[-1], timeout)
else:
await self.delete_unique_znode("worker")
await self.wait_on_sibling(participants[0], timeout)
owned_positions, participants = await self.analyze_siblings()
if len(participants) == 1 and "worker" in owned_positions:
await self.delete_unique_znode("worker")
try:
await self.client.delete(self.sentinel_path)
except exc.NoNode:
pass
async def fetch(self, path):
# exists() gives create, delete, and update watches
watch_via_exists = WatchEvent.CREATED in self.watched_events
if watch_via_exists:
exists = await self.client.exists(path, watch=True)
if not exists:
raise NoNode
data = await self.client.get_data(path=path, watch=not watch_via_exists)
return data
async def watch_loop(self, path):
while self.callbacks[path]:
log.debug("Fetching data for %s", path)
try:
result = await self.fetch(path)
except exc.NoNode:
result = exc.NoNode
except exc.ZKError as e:
log.exception('Exception in watch loop: {}'.format(e))
log.info('Waiting for safe state...')
await self.client.session.ensure_safe_state()
continue
except Exception:
log.exception('Not handled in watch loop:')
raise
for callback in self.callbacks[path].copy():
maybe_future(callback(result), loop=self.client.loop)
if WatchEvent.CREATED not in self.watched_events and result == exc.NoNode:
return
try:
await self.client.wait_for_events(self.watched_events, path)
except asyncio.CancelledError:
async def child_callback(self, new_children):
if new_children == NoNode:
return
removed_children = set(self.children.keys()) - set(new_children)
added_children = set(new_children) - set(self.children.keys())
for removed in removed_children:
log.debug("Removed child %s", self.dot_path + "." + removed)
child = self.children.pop(removed)
await child.stop()
for added in added_children:
log.debug("Added child %s", self.dot_path + "." + added)
self.children[added] = ZNodeCache(
self.path + "/" + added, self.defaults.get(added, {}),
self.client, self.data_watcher, self.child_watcher
)
await asyncio.gather(*(self.children[added].start() for added in added_children))
async def lift(self):
try:
await self.client.delete(self.path)
except exc.NoNode:
pass
async def delete_unique_znode(self, znode_label):
try:
await self.client.delete(self.owned_paths[znode_label])
except exc.NoNode:
pass