Add a doctest to type_utils.py.
[python_utils.git] / lockfile.py
index 4b6aadeffde8ec7bf255025873d720e2b3afda93..290710759dbc702fff757ae2561fe5c0af3051a6 100644 (file)
@@ -1,25 +1,24 @@
 #!/usr/bin/env python3
 
-from dataclasses import dataclass
 import datetime
 import json
 import logging
 import os
 import signal
 import sys
-from typing import Optional
 import warnings
+from dataclasses import dataclass
+from typing import Optional
 
 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,
+    default=60.0,
     metavar='SECONDS',
     help='If a lock is held for longer than this threshold we log a warning',
 )
@@ -34,7 +33,7 @@ class LockFileException(Exception):
 class LockFileContents:
     pid: int
     commandline: str
-    expiration_timestamp: float
+    expiration_timestamp: Optional[float]
 
 
 class LockFile(object):
@@ -130,10 +129,7 @@ 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)
@@ -181,7 +177,7 @@ class LockFile(object):
                     # Has the lock expiration expired?
                     if contents.expiration_timestamp is not None:
                         now = datetime.datetime.now().timestamp()
-                        if now > contents.expiration_datetime:
+                        if now > contents.expiration_timestamp:
                             msg = f'Lockfile {self.lockfile} expiration time has passed; force acquiring'
                             logger.warning(msg)
                             self.release()