metavar='LOCKFILE_PATH',
help='Path to the lockfile to use to ensure that two instances of a command do not execute contemporaneously.',
)
+cfg.add_argument(
+ '--lockfile_audit_record',
+ default=None,
+ metavar='LOCKFILE_AUDIT_RECORD_FILENAME',
+ help='Path to a record of when the logfile was held/released and for what reason',
+)
cfg.add_argument(
'--timeout',
type=str,
do_signal_cleanup=True,
override_command=' '.join(config.config['command']),
expiration_timestamp=lockfile_expiration,
- ):
- return run_command(timeout, timestamp_file)
+ ) as lf:
+ record = config.config['lockfile_audit_record']
+ cmd = ' '.join(config.config['command'])
+ if record:
+ with open(record, 'a') as wf:
+ print(
+ f'{lockfile_path}, ACQUIRE, {lf.locktime}, {cmd}', file=wf
+ )
+ retval = run_command(timeout, timestamp_file)
+ if record:
+ with open(record, 'a') as wf:
+ print(
+ f'{lockfile_path}, RELEASE, {datetime.datetime.now().timestamp()}, {cmd}',
+ file=wf,
+ )
+ return retval
except lockfile.LockFileException as e:
logger.exception(e)
msg = f'Failed to acquire {lockfile_path}, giving up.'
"""
self.is_locked: bool = False
self.lockfile: str = lockfile_path
- self.locktime: Optional[int] = None
+ self.locktime: Optional[float] = None
self.override_command: Optional[str] = override_command
if do_signal_cleanup:
signal.signal(signal.SIGINT, self._signal)
contents = self._get_lockfile_contents()
logger.debug(contents)
f.write(contents)
+ self.locktime = datetime.datetime.now().timestamp()
logger.debug('Success; I own %s.', self.lockfile)
self.is_locked = True
return True
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)