X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=lockfile.py;h=4b6aadeffde8ec7bf255025873d720e2b3afda93;hb=865825894beeedd47d26dd092d40bfee582f5475;hp=8a71ca769900983689479157de0cb3132ab731a9;hpb=55a3172e37855f388b9ba0dfc91641a6c9ad1376;p=python_utils.git diff --git a/lockfile.py b/lockfile.py index 8a71ca7..4b6aade 100644 --- a/lockfile.py +++ b/lockfile.py @@ -15,15 +15,13 @@ import datetime_utils import decorator_utils -cfg = config.add_commandline_args( - f'Lockfile ({__file__})', - 'Args related to lockfiles') +cfg = config.add_commandline_args(f'Lockfile ({__file__})', 'Args related to lockfiles') cfg.add_argument( '--lockfile_held_duration_warning_threshold_sec', type=float, default=10.0, metavar='SECONDS', - help='If a lock is held for longer than this threshold we log a warning' + help='If a lock is held for longer than this threshold we log a warning', ) logger = logging.getLogger(__name__) @@ -50,13 +48,14 @@ class LockFile(object): # some logic for detecting stale locks. """ + def __init__( - self, - lockfile_path: str, - *, - do_signal_cleanup: bool = True, - expiration_timestamp: Optional[float] = None, - override_command: Optional[str] = None, + self, + lockfile_path: str, + *, + do_signal_cleanup: bool = True, + expiration_timestamp: Optional[float] = None, + override_command: Optional[str] = None, ) -> None: self.is_locked = False self.lockfile = lockfile_path @@ -93,16 +92,15 @@ class LockFile(object): return False def acquire_with_retries( - self, - *, - initial_delay: float = 1.0, - backoff_factor: float = 2.0, - max_attempts = 5 + self, + *, + initial_delay: float = 1.0, + backoff_factor: float = 2.0, + max_attempts=5, ) -> bool: - - @decorator_utils.retry_if_false(tries = max_attempts, - delay_sec = initial_delay, - backoff = backoff_factor) + @decorator_utils.retry_if_false( + tries=max_attempts, delay_sec=initial_delay, backoff=backoff_factor + ) def _try_acquire_lock_with_retries() -> bool: success = self.try_acquire_lock_once() if not success and os.path.exists(self.lockfile): @@ -132,7 +130,10 @@ class LockFile(object): if self.locktime: ts = datetime.datetime.now().timestamp() duration = ts - self.locktime - if duration >= config.config['lockfile_held_duration_warning_threshold_sec']: + if ( + duration + >= config.config['lockfile_held_duration_warning_threshold_sec'] + ): str_duration = datetime_utils.describe_duration_briefly(duration) msg = f'Held {self.lockfile} for {str_duration}' logger.warning(msg) @@ -152,11 +153,10 @@ class LockFile(object): cmd = self.override_command else: cmd = ' '.join(sys.argv) - print(cmd) contents = LockFileContents( - pid = os.getpid(), - commandline = cmd, - expiration_timestamp = self.expiration_timestamp, + pid=os.getpid(), + commandline=cmd, + expiration_timestamp=self.expiration_timestamp, ) return json.dumps(contents.__dict__)