projects
/
pyutils.git
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Rename some directories (e.g. typez -> types) since they don't collide with
[pyutils.git]
/
src
/
pyutils
/
files
/
lockfile.py
diff --git
a/src/pyutils/files/lockfile.py
b/src/pyutils/files/lockfile.py
index 0febca6bf5b754682f5e91f8604c70e566fd8a0d..68f957cbc080deed793cf2e24c14baddeecd6e48 100644
(file)
--- a/
src/pyutils/files/lockfile.py
+++ b/
src/pyutils/files/lockfile.py
@@
-25,15
+25,15
@@
from dataclasses import dataclass
from typing import Literal, Optional
from pyutils import argparse_utils, config, decorator_utils
from typing import Literal, Optional
from pyutils import argparse_utils, config, decorator_utils
-from pyutils.datetime
z
import datetime_utils
+from pyutils.datetime
s
import datetime_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(
cfg.add_argument(
-
'--lockfile_held_duration_warning_threshold'
,
+
"--lockfile_held_duration_warning_threshold"
,
type=argparse_utils.valid_duration,
default=datetime.timedelta(60.0),
type=argparse_utils.valid_duration,
default=datetime.timedelta(60.0),
- metavar=
'DURATION'
,
- help=
'If a lock is held for longer than this threshold we log a warning'
,
+ metavar=
"DURATION"
,
+ help=
"If a lock is held for longer than this threshold we log a warning"
,
)
logger = logging.getLogger(__name__)
)
logger = logging.getLogger(__name__)
@@
-91,7
+91,7
@@
class LockFile(contextlib.AbstractContextManager):
"""
self.is_locked: bool = False
self.lockfile: str = lockfile_path
"""
self.is_locked: bool = False
self.lockfile: str = lockfile_path
- self.locktime: Optional[
in
t] = None
+ self.locktime: Optional[
floa
t] = None
self.override_command: Optional[str] = override_command
if do_signal_cleanup:
signal.signal(signal.SIGINT, self._signal)
self.override_command: Optional[str] = override_command
if do_signal_cleanup:
signal.signal(signal.SIGINT, self._signal)
@@
-122,12
+122,13
@@
class LockFile(contextlib.AbstractContextManager):
contents = self._get_lockfile_contents()
logger.debug(contents)
f.write(contents)
contents = self._get_lockfile_contents()
logger.debug(contents)
f.write(contents)
- logger.debug('Success; I own %s.', self.lockfile)
+ self.locktime = datetime.datetime.now().timestamp()
+ logger.debug("Success; I own %s.", self.lockfile)
self.is_locked = True
return True
except OSError:
pass
self.is_locked = True
return True
except OSError:
pass
- logger.warning(
'Couldn\'t acquire %s.'
, self.lockfile)
+ logger.warning(
"Couldn't acquire %s."
, self.lockfile)
return False
def acquire_with_retries(
return False
def acquire_with_retries(
@@
-174,7
+175,6
@@
class LockFile(contextlib.AbstractContextManager):
def __enter__(self):
if self.acquire_with_retries():
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)
return self
msg = f"Couldn't acquire {self.lockfile}; giving up."
logger.warning(msg)
@@
-187,12
+187,12
@@
class LockFile(contextlib.AbstractContextManager):
if (
duration
>= config.config[
if (
duration
>= config.config[
- 'lockfile_held_duration_warning_threshold'
+ "lockfile_held_duration_warning_threshold"
].total_seconds()
):
# Note: describe duration briefly only does 1s granularity...
str_duration = datetime_utils.describe_duration_briefly(int(duration))
].total_seconds()
):
# Note: describe duration briefly only does 1s granularity...
str_duration = datetime_utils.describe_duration_briefly(int(duration))
- msg = 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()
logger.warning(msg)
warnings.warn(msg, stacklevel=2)
self.release()
@@
-210,7
+210,7
@@
class LockFile(contextlib.AbstractContextManager):
if self.override_command:
cmd = self.override_command
else:
if self.override_command:
cmd = self.override_command
else:
- cmd =
' '
.join(sys.argv)
+ cmd =
" "
.join(sys.argv)
contents = LockFileContents(
pid=os.getpid(),
commandline=cmd,
contents = LockFileContents(
pid=os.getpid(),
commandline=cmd,
@@
-220,7
+220,7
@@
class LockFile(contextlib.AbstractContextManager):
def _detect_stale_lockfile(self) -> None:
try:
def _detect_stale_lockfile(self) -> None:
try:
- with open(self.lockfile,
'r'
) as rf:
+ with open(self.lockfile,
"r"
) as rf:
lines = rf.readlines()
if len(lines) == 1:
line = lines[0]
lines = rf.readlines()
if len(lines) == 1:
line = lines[0]
@@
-233,7
+233,7
@@
class LockFile(contextlib.AbstractContextManager):
os.kill(contents.pid, 0)
except OSError:
logger.warning(
os.kill(contents.pid, 0)
except OSError:
logger.warning(
-
'Lockfile %s\'s pid (%d) is stale; force acquiring...'
,
+
"Lockfile %s's pid (%d) is stale; force acquiring..."
,
self.lockfile,
contents.pid,
)
self.lockfile,
contents.pid,
)
@@
-244,7
+244,7
@@
class LockFile(contextlib.AbstractContextManager):
now = datetime.datetime.now().timestamp()
if now > contents.expiration_timestamp:
logger.warning(
now = datetime.datetime.now().timestamp()
if now > contents.expiration_timestamp:
logger.warning(
-
'Lockfile %s\'s expiration time has passed; force acquiring'
,
+
"Lockfile %s's expiration time has passed; force acquiring"
,
self.lockfile,
)
self.release()
self.lockfile,
)
self.release()