Start using warnings from stdlib.
[python_utils.git] / lockfile.py
index 1e0516bf75a340b4a15629141cdfbfb83fc3485c..7f10cc1f5894155c65ca352183afc0ea6d81fa37 100644 (file)
@@ -8,10 +8,23 @@ import os
 import signal
 import sys
 from typing import Optional
+import warnings
 
+import config
+import datetime_utils
 import decorator_utils
 
 
+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'
+)
 logger = logging.getLogger(__name__)
 
 
@@ -37,7 +50,6 @@ class LockFile(object):
         # some logic for detecting stale locks.
 
     """
-
     def __init__(
             self,
             lockfile_path: str,
@@ -76,7 +88,9 @@ class LockFile(object):
             return True
         except OSError:
             pass
-        logger.debug(f'Failed; I could not acquire {self.lockfile}.')
+        msg = f'Could not acquire {self.lockfile}.'
+        logger.warning(msg)
+        warnings.warn(msg)
         return False
 
     def acquire_with_retries(
@@ -109,12 +123,22 @@ class LockFile(object):
 
     def __enter__(self):
         if self.acquire_with_retries():
+            self.locktime = datetime.datetime.now().timestamp()
             return self
         msg = f"Couldn't acquire {self.lockfile}; giving up."
         logger.warning(msg)
+        warnings.warn(msg)
         raise LockFileException(msg)
 
     def __exit__(self, type, value, traceback):
+        if self.locktime:
+            ts = datetime.datetime.now().timestamp()
+            duration = ts - self.locktime
+            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)
+                warnings.warn(msg, stacklevel=2)
         self.release()
 
     def __del__(self):
@@ -152,15 +176,18 @@ class LockFile(object):
                     try:
                         os.kill(contents.pid, 0)
                     except OSError:
-                        logger.debug('The pid seems stale; killing the lock.')
+                        msg = f'Lockfile {self.lockfile}\'s pid ({contents.pid}) is stale; force acquiring'
+                        logger.warning(msg)
+                        warnings.warn(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.debug('The expiration time has passed; ' +
-                                         'killing the lock')
+                            msg = f'Lockfile {self.lockfile} expiration time has passed; force acquiring'
+                            logger.warning(msg)
+                            warnings.warn(msg)
                             self.release()
         except Exception:
             pass