X-Git-Url: https://wannabe.guru.org/gitweb/?a=blobdiff_plain;f=lockfile.py;h=ebd91158a54b80ed275bed990a3d9e35fedb0e0f;hb=17e8082381dbbf691dfb19fb1b38a97e48d6ab87;hp=ca190df09f9245bca7153945f0fee61859daa1d7;hpb=5f75cf834725ac26b289cc5f157af0cb71cd5f0e;p=python_utils.git diff --git a/lockfile.py b/lockfile.py index ca190df..ebd9115 100644 --- a/lockfile.py +++ b/lockfile.py @@ -8,8 +8,10 @@ import os import signal import sys from typing import Optional +import warnings import config +import datetime_utils import decorator_utils @@ -86,7 +88,8 @@ class LockFile(object): return True except OSError: pass - logger.warning(f'Could not acquire {self.lockfile}.') + msg = f'Could not acquire {self.lockfile}.' + logger.warning(msg) return False def acquire_with_retries( @@ -131,7 +134,9 @@ class LockFile(object): duration = ts - self.locktime if duration >= config.config['lockfile_held_duration_warning_threshold_sec']: str_duration = datetime_utils.describe_duration_briefly(duration) - logger.warning(f'Held {self.lockfile} for {str_duration}') + msg = f'Held {self.lockfile} for {str_duration}' + logger.warning(msg) + warnings.warn(msg, stacklevel=2) self.release() def __del__(self): @@ -147,7 +152,6 @@ class LockFile(object): cmd = self.override_command else: cmd = ' '.join(sys.argv) - print(cmd) contents = LockFileContents( pid = os.getpid(), commandline = cmd, @@ -169,16 +173,16 @@ class LockFile(object): try: os.kill(contents.pid, 0) except OSError: - logger.warning(f'Lockfile {self.lockfile}\'s pid ({contents.pid}) is stale; ' + - 'force acquiring') + msg = f'Lockfile {self.lockfile}\'s pid ({contents.pid}) is stale; force acquiring' + logger.warning(msg) self.release() # Has the lock expiration expired? if contents.expiration_timestamp is not None: now = datetime.datetime.now().timestamp() if now > contents.expiration_datetime: - logger.warning(f'Lockfile {self.lockfile} expiration time has passed; ' + - 'force acquiring') + msg = f'Lockfile {self.lockfile} expiration time has passed; force acquiring' + logger.warning(msg) self.release() except Exception: pass