3 from dataclasses import dataclass
10 from typing import Optional
15 import decorator_utils
18 cfg = config.add_commandline_args(
19 f'Lockfile ({__file__})',
20 'Args related to lockfiles')
22 '--lockfile_held_duration_warning_threshold_sec',
26 help='If a lock is held for longer than this threshold we log a warning'
28 logger = logging.getLogger(__name__)
31 class LockFileException(Exception):
36 class LockFileContents:
39 expiration_timestamp: float
42 class LockFile(object):
43 """A file locking mechanism that has context-manager support so you
44 can use it in a with statement. e.g.
46 with LockFile('./foo.lock'):
47 # do a bunch of stuff... if the process dies we have a signal
48 # handler to do cleanup. Other code (in this process or another)
49 # that tries to take the same lockfile will block. There is also
50 # some logic for detecting stale locks.
57 do_signal_cleanup: bool = True,
58 expiration_timestamp: Optional[float] = None,
59 override_command: Optional[str] = None,
61 self.is_locked = False
62 self.lockfile = lockfile_path
63 self.override_command = override_command
65 signal.signal(signal.SIGINT, self._signal)
66 signal.signal(signal.SIGTERM, self._signal)
67 self.expiration_timestamp = expiration_timestamp
73 return not os.path.exists(self.lockfile)
75 def try_acquire_lock_once(self) -> bool:
76 logger.debug(f"Trying to acquire {self.lockfile}.")
78 # Attempt to create the lockfile. These flags cause
79 # os.open to raise an OSError if the file already
81 fd = os.open(self.lockfile, os.O_CREAT | os.O_EXCL | os.O_RDWR)
82 with os.fdopen(fd, "a") as f:
83 contents = self._get_lockfile_contents()
84 logger.debug(contents)
86 logger.debug(f'Success; I own {self.lockfile}.')
91 msg = f'Could not acquire {self.lockfile}.'
95 def acquire_with_retries(
98 initial_delay: float = 1.0,
99 backoff_factor: float = 2.0,
103 @decorator_utils.retry_if_false(tries = max_attempts,
104 delay_sec = initial_delay,
105 backoff = backoff_factor)
106 def _try_acquire_lock_with_retries() -> bool:
107 success = self.try_acquire_lock_once()
108 if not success and os.path.exists(self.lockfile):
109 self._detect_stale_lockfile()
112 if os.path.exists(self.lockfile):
113 self._detect_stale_lockfile()
114 return _try_acquire_lock_with_retries()
118 os.unlink(self.lockfile)
119 except Exception as e:
121 self.is_locked = False
124 if self.acquire_with_retries():
125 self.locktime = datetime.datetime.now().timestamp()
127 msg = f"Couldn't acquire {self.lockfile}; giving up."
129 raise LockFileException(msg)
131 def __exit__(self, type, value, traceback):
133 ts = datetime.datetime.now().timestamp()
134 duration = ts - self.locktime
135 if duration >= config.config['lockfile_held_duration_warning_threshold_sec']:
136 str_duration = datetime_utils.describe_duration_briefly(duration)
137 msg = f'Held {self.lockfile} for {str_duration}'
139 warnings.warn(msg, stacklevel=2)
146 def _signal(self, *args):
150 def _get_lockfile_contents(self) -> str:
151 if self.override_command:
152 cmd = self.override_command
154 cmd = ' '.join(sys.argv)
156 contents = LockFileContents(
159 expiration_timestamp = self.expiration_timestamp,
161 return json.dumps(contents.__dict__)
163 def _detect_stale_lockfile(self) -> None:
165 with open(self.lockfile, 'r') as rf:
166 lines = rf.readlines()
169 line_dict = json.loads(line)
170 contents = LockFileContents(**line_dict)
171 logger.debug(f'Blocking lock contents="{contents}"')
173 # Does the PID exist still?
175 os.kill(contents.pid, 0)
177 msg = f'Lockfile {self.lockfile}\'s pid ({contents.pid}) is stale; force acquiring'
181 # Has the lock expiration expired?
182 if contents.expiration_timestamp is not None:
183 now = datetime.datetime.now().timestamp()
184 if now > contents.expiration_datetime:
185 msg = f'Lockfile {self.lockfile} expiration time has passed; force acquiring'