From 8bbb69a8838de3d2af742e8aeaf8d4c9f9356dff Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Mon, 11 Mar 2024 19:42:31 +0530 Subject: [PATCH] fix: Prevent multiple schedulers from running on same bench (#25340) --- frappe/utils/scheduler.py | 10 ++++++---- frappe/utils/synchronization.py | 2 +- 2 files changed, 7 insertions(+), 5 deletions(-) diff --git a/frappe/utils/scheduler.py b/frappe/utils/scheduler.py index 6f9ce6b010..03368f956f 100644 --- a/frappe/utils/scheduler.py +++ b/frappe/utils/scheduler.py @@ -20,6 +20,7 @@ import setproctitle import frappe from frappe.utils import cint, get_datetime, get_sites, now_datetime from frappe.utils.background_jobs import set_niceness +from frappe.utils.synchronization import filelock DATETIME_FORMAT = "%Y-%m-%d %H:%M:%S" @@ -44,10 +45,11 @@ def start_scheduler() -> NoReturn: tick = cint(frappe.get_conf().scheduler_tick_interval) or 60 set_niceness() - while True: - _proctitle("idle") - time.sleep(tick) - enqueue_events_for_all_sites() + with filelock("scheduler_process", timeout=1, is_global=True): + while True: + _proctitle("idle") + time.sleep(tick) + enqueue_events_for_all_sites() def enqueue_events_for_all_sites() -> None: diff --git a/frappe/utils/synchronization.py b/frappe/utils/synchronization.py index 0f7aeb1527..f86b401d59 100644 --- a/frappe/utils/synchronization.py +++ b/frappe/utils/synchronization.py @@ -40,7 +40,7 @@ def filelock(lock_name: str, *, timeout=30, is_global=False): with _StrongFileLock(lock_path, timeout=timeout): yield except Timeout as e: - frappe.log_error("Filelock: Failed to aquire {lock_path}") + frappe.log_error(f"Filelock: Failed to aquire {lock_path}") raise LockTimeoutError( _("Failed to aquire lock: {}. Lock may be held by another process.").format(lock_name)