Skip to content

Commit 129807a

Browse files
Add PID to .lock file.
1 parent 71dca90 commit 129807a

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

src/admin.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -275,6 +275,20 @@ def refresh_has_metakg():
275275
smartapi.save()
276276

277277

278+
def get_lock_pid():
279+
"""Read PID from lock file if it exists"""
280+
lock_file_path = ".lock"
281+
if os.path.exists(lock_file_path):
282+
try:
283+
with open(lock_file_path, "r") as f:
284+
pid = f.read().strip()
285+
return int(pid) if pid.isdigit() else None
286+
except Exception as e:
287+
logging.warning(f"Could not read PID from {lock_file_path}: {e}")
288+
return None
289+
return None
290+
291+
278292
restore = restore_from_file
279293
backup = backup_to_file
280294

@@ -301,7 +315,11 @@ def routine(no_backup=False, format="zip"):
301315
# it won't block here
302316
lock_acquired = _lock.acquire()
303317
if lock_acquired:
304-
logger.info("Schedule lock acquired successfully.")
318+
# Write PID to the lock file
319+
current_pid = os.getpid()
320+
with open(".lock", "w") as lock_file:
321+
lock_file.write(str(current_pid))
322+
logger.info(f"Schedule lock acquired successfully. PID {current_pid} written to lock file.")
305323
if not no_backup:
306324
logger.info(f"backup_to_s3(format={format})")
307325
backup_to_s3(format=format)
@@ -318,7 +336,11 @@ def routine(no_backup=False, format="zip"):
318336
else:
319337
logger.warning("Schedule lock acquired by another process. No need to run it in this process.")
320338
except Timeout:
321-
logger.warning("Schedule lock acquired by another process. No need to run it in this process.")
339+
existing_pid = get_lock_pid()
340+
if existing_pid:
341+
logger.warning(f"Schedule lock acquired by another process (PID: {existing_pid}). No need to run it in this process.")
342+
else:
343+
logger.warning("Schedule lock acquired by another process. No need to run it in this process.")
322344
except Exception as e:
323345
logger.error(f"An error occurred during the routine: {e}")
324346
logger.error("Stack trace:", exc_info=True)
@@ -331,8 +353,13 @@ def routine(no_backup=False, format="zip"):
331353
lock_file_path = ".lock"
332354
if os.path.exists(lock_file_path):
333355
try:
356+
# Log the PID that was in the lock file before deletion
357+
existing_pid = get_lock_pid()
334358
os.remove(lock_file_path)
335-
logger.info(".lock file manually deleted.")
359+
if existing_pid:
360+
logger.info(f".lock file manually deleted (contained PID: {existing_pid}).")
361+
else:
362+
logger.info(".lock file manually deleted.")
336363
except Exception as e:
337364
logger.warning(f"Could not delete .lock file: {e}")
338365

0 commit comments

Comments
 (0)