Skip to content

Commit

Permalink
Added exception handling in _zk_flock
Browse files Browse the repository at this point in the history
  • Loading branch information
Filippo Monari committed Nov 2, 2023
1 parent 4f4cf73 commit 0160045
Showing 1 changed file with 19 additions and 2 deletions.
21 changes: 19 additions & 2 deletions ch_backup/logic/lock_manager.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
from types import TracebackType
from typing import IO, Optional, Type

from kazoo.exceptions import LockTimeout
from kazoo.recipe.lock import Lock

from ch_backup import logging
Expand Down Expand Up @@ -107,7 +108,23 @@ def _zk_flock(self) -> None:
if not client.exists(self._process_zk_lockfile_path):
client.create(self._process_zk_lockfile_path, makepath=True)
self._zk_lock = client.Lock(self._process_zk_lockfile_path)
if not self._zk_lock.acquire(blocking=True, timeout=self._lock_timeout):
sys.exit(self._exitcode)
force_exit = self._exitcode == 0
try:
if not self._zk_lock.acquire(blocking=True, timeout=self._lock_timeout):
fail_on_lock_acquiring(force_exit)
except LockTimeout as e:
fail_on_lock_acquiring(force_exit, e)
else:
raise RuntimeError("ZK flock enabled, but zookeeper is not configured")


def fail_on_lock_acquiring(force_exit: bool, e: Optional[Exception] = None):
"""
Determines how to exit from the lock acquisition process
"""
if force_exit:
sys.exit(0)
err = RuntimeError("Lock was not acquired")
if e is not None:
raise err from e
raise err

0 comments on commit 0160045

Please sign in to comment.