fix: avoid flushing to monitor logs concurrently (#32552)
This commit is contained in:
parent
26b279864f
commit
a6c73dfdbe
1 changed files with 13 additions and 12 deletions
|
|
@ -11,6 +11,7 @@ import rq
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe.utils.data import cint
|
from frappe.utils.data import cint
|
||||||
|
from frappe.utils.synchronization import filelock
|
||||||
|
|
||||||
MONITOR_REDIS_KEY = "monitor-transactions"
|
MONITOR_REDIS_KEY = "monitor-transactions"
|
||||||
MONITOR_MAX_ENTRIES = 1000000
|
MONITOR_MAX_ENTRIES = 1000000
|
||||||
|
|
@ -122,15 +123,15 @@ class Monitor:
|
||||||
|
|
||||||
|
|
||||||
def flush():
|
def flush():
|
||||||
try:
|
logs = frappe.cache.lrange(MONITOR_REDIS_KEY, 0, -1)
|
||||||
# Fetch all the logs without removing from cache
|
if not logs:
|
||||||
logs = frappe.cache.lrange(MONITOR_REDIS_KEY, 0, -1)
|
return
|
||||||
if logs:
|
|
||||||
logs = list(map(frappe.safe_decode, logs))
|
logs = list(map(frappe.safe_decode, logs))
|
||||||
with open(log_file(), "a") as f:
|
with filelock("monitor_flush", is_global=True, timeout=5):
|
||||||
f.write("\n".join(logs))
|
with open(log_file(), "a") as f:
|
||||||
f.write("\n")
|
f.write("\n".join(logs))
|
||||||
# Remove fetched entries from cache
|
f.write("\n")
|
||||||
frappe.cache.ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1)
|
|
||||||
except Exception:
|
# Remove fetched entries from cache
|
||||||
traceback.print_exc()
|
frappe.cache.ltrim(MONITOR_REDIS_KEY, len(logs) - 1, -1)
|
||||||
|
|
|
||||||
Loading…
Add table
Reference in a new issue