fix: Set prepared report even if req times out (#29874)

This commit is contained in:
Ankush Menat 2025-01-22 13:53:41 +05:30 committed by GitHub
parent bebc40087c
commit 8071d38ad2
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -2,6 +2,7 @@
# License: MIT. See LICENSE
import datetime
import json
import threading
import frappe
import frappe.desk.query_report
@ -160,6 +161,14 @@ class Report(Document):
threshold = 15
start_time = datetime.datetime.now()
prepared_report_watcher = None
if not self.prepared_report:
prepared_report_watcher = threading.Timer(
interval=threshold,
function=enable_prepared_report,
kwargs={"report": self.name, "site": frappe.local.site},
)
prepared_report_watcher.start()
# The JOB
if self.is_standard == "Yes":
@ -167,10 +176,8 @@ class Report(Document):
else:
res = self.execute_script(filters)
# automatically set as prepared
prepared_report_watcher and prepared_report_watcher.cancel()
execution_time = (datetime.datetime.now() - start_time).total_seconds()
if execution_time > threshold and not self.prepared_report and not frappe.conf.developer_mode:
frappe.enqueue(enable_prepared_report, report=self.name)
frappe.cache.hset("report_execution_time", self.name, execution_time)
@ -418,5 +425,9 @@ def get_group_by_column_label(args, meta):
return label
def enable_prepared_report(report: str):
def enable_prepared_report(report: str, site: str):
frappe.init(site)
frappe.connect()
frappe.db.set_value("Report", report, "prepared_report", 1)
frappe.db.commit()
frappe.destroy()