Start using warnings from stdlib.
[python_utils.git] / lockfile.py
index d275f407ff237c38ceaec83c94a1abf92174b04a..7f10cc1f5894155c65ca352183afc0ea6d81fa37 100644 (file)
@@ -8,6 +8,7 @@ import os
 import signal
 import sys
 from typing import Optional
+import warnings
 
 import config
 import datetime_utils
@@ -87,7 +88,9 @@ 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)
+        warnings.warn(msg)
         return False
 
     def acquire_with_retries(
@@ -124,6 +127,7 @@ class LockFile(object):
             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):
@@ -132,7 +136,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):
@@ -170,16 +176,18 @@ 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)
+                        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.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)
+                            warnings.warn(msg)
                             self.release()
         except Exception:
             pass