diff --git a/frappe/core/doctype/report/report.py b/frappe/core/doctype/report/report.py index b5d4419467..212b5c782f 100644 --- a/frappe/core/doctype/report/report.py +++ b/frappe/core/doctype/report/report.py @@ -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()