Skip to content

Commit 5abdc8b

Browse files
committed
cli: add --ensure-once to healthcheck
1 parent 3bea1b6 commit 5abdc8b

File tree

1 file changed

+26
-0
lines changed

1 file changed

+26
-0
lines changed

cli/commands/tool/healthcheck.py

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ class Arguments:
2727
from_email: str
2828
to_email: str
2929
smtp_server: str
30+
ensure_once: bool
3031

3132

3233
def _now() -> str:
@@ -62,7 +63,25 @@ def handle_error(msg: str, current_interval: int, args: Arguments) -> int:
6263
return current_interval + 1
6364

6465

66+
LOCK_FD = -1
67+
68+
6569
def run(args: Arguments) -> None:
70+
if args.ensure_once:
71+
global LOCK_FD
72+
import fcntl
73+
import errno
74+
import os
75+
76+
try:
77+
LOCK_FD = os.open("/tmp/tim_healthcheck.lock", os.O_RDWR | os.O_CREAT)
78+
fcntl.flock(LOCK_FD, fcntl.LOCK_EX | fcntl.LOCK_NB) # type: ignore
79+
except (IOError, OSError) as e:
80+
if e.errno in (errno.EACCES, errno.EAGAIN):
81+
log_info("Healthcheck watchdog is already running")
82+
return
83+
# No need to close the lock, it should be released when the process exits
84+
6685
log_info("Starting healthcheck watchdog")
6786
try:
6887
interval = 1
@@ -115,3 +134,10 @@ def init(parser: ArgumentParser) -> None:
115134
required=True,
116135
dest="smtp_server",
117136
)
137+
parser.add_argument(
138+
"--ensure-once",
139+
help="Exit if the watchdog is already running",
140+
action="store_true",
141+
default=False,
142+
dest="ensure_once",
143+
)

0 commit comments

Comments
 (0)