From a6c73dfdbe1ea0adfd55bca2ace4d5fdf0137dba Mon Sep 17 00:00:00 2001 From: Ankush Menat Date: Fri, 16 May 2025 17:50:40 +0530 Subject: [PATCH] fix: avoid flushing to monitor logs concurrently (#32552) --- frappe/monitor.py | 25 +++++++++++++------------ 1 file changed, 13 insertions(+), 12 deletions(-) diff --git a/frappe/monitor.py b/frappe/monitor.py index 8b9179e6f9..48e0965e5e 100644 --- a/frappe/monitor.py +++ b/frappe/monitor.py @@ -11,6 +11,7 @@ import rq import frappe from frappe.utils.data import cint +from frappe.utils.synchronization import filelock MONITOR_REDIS_KEY = "monitor-transactions" MONITOR_MAX_ENTRIES = 1000000 @@ -122,15 +123,15 @@ class Monitor: def flush(): - try: - # Fetch all the logs without removing from cache - logs = frappe.cache.lrange(MONITOR_REDIS_KEY, 0, -1) - if logs: - logs = list(map(frappe.safe_decode, logs)) - with open(log_file(), "a") as f: - f.write("\n".join(logs)) - f.write("\n") - # Remove fetched entries from cache - frappe.cache.ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1) - except Exception: - traceback.print_exc() + logs = frappe.cache.lrange(MONITOR_REDIS_KEY, 0, -1) + if not logs: + return + + logs = list(map(frappe.safe_decode, logs)) + with filelock("monitor_flush", is_global=True, timeout=5): + with open(log_file(), "a") as f: + f.write("\n".join(logs)) + f.write("\n") + + # Remove fetched entries from cache + frappe.cache.ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1)