From 83c10c92634d56f857beed83d4075eb7e6996858 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 21 Oct 2019 20:16:34 +0530 Subject: [PATCH 1/4] perf(database): Regularly clean old unhandled emails Co-authored-by: sahil28297 --- frappe/email/doctype/unhandled_email/unhandled_email.py | 4 ++++ frappe/hooks.py | 3 ++- frappe/patches.txt | 1 + 3 files changed, 7 insertions(+), 1 deletion(-) diff --git a/frappe/email/doctype/unhandled_email/unhandled_email.py b/frappe/email/doctype/unhandled_email/unhandled_email.py index 77ba8aea13..99971c658d 100644 --- a/frappe/email/doctype/unhandled_email/unhandled_email.py +++ b/frappe/email/doctype/unhandled_email/unhandled_email.py @@ -8,3 +8,7 @@ from frappe.model.document import Document class UnhandledEmail(Document): pass + + +def remove_old_unhandled_emails(): + frappe.db.sql("""DELETE FROM `tabUnhandled Email` WHERE datediff(curdate(), date(creation)) > 30""") diff --git a/frappe/hooks.py b/frappe/hooks.py index a35fd78ebb..b35387efe9 100644 --- a/frappe/hooks.py +++ b/frappe/hooks.py @@ -188,7 +188,8 @@ scheduler_events = { "frappe.social.doctype.energy_point_settings.energy_point_settings.allocate_review_points", "frappe.integrations.doctype.google_contacts.google_contacts.sync", "frappe.automation.doctype.auto_repeat.auto_repeat.make_auto_repeat_entry", - "frappe.automation.doctype.auto_repeat.auto_repeat.set_auto_repeat_as_completed" + "frappe.automation.doctype.auto_repeat.auto_repeat.set_auto_repeat_as_completed", + "frappe.email.doctype.unhandled_email.unhandled_email.remove_old_unhandled_emails" ], "daily_long": [ "frappe.integrations.doctype.dropbox_settings.dropbox_settings.take_backups_daily", diff --git a/frappe/patches.txt b/frappe/patches.txt index e4d3fd2039..9a67bc9376 100644 --- a/frappe/patches.txt +++ b/frappe/patches.txt @@ -255,3 +255,4 @@ frappe.patches.v12_0.set_default_incoming_email_port frappe.patches.v12_0.update_global_search frappe.patches.v12_0.setup_tags execute:frappe.reload_doc('desk', 'doctype', 'notification_settings') +execute:frappe.email.doctype.unhandled_email.unhandled_email.remove_old_unhandled_emails From cef8f4d4503cec44b9a73a0acfb95348c71fc8b7 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Mon, 21 Oct 2019 21:00:13 +0530 Subject: [PATCH 2/4] Update unhandled_email.py --- frappe/email/doctype/unhandled_email/unhandled_email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/email/doctype/unhandled_email/unhandled_email.py b/frappe/email/doctype/unhandled_email/unhandled_email.py index 99971c658d..9f9357c1f2 100644 --- a/frappe/email/doctype/unhandled_email/unhandled_email.py +++ b/frappe/email/doctype/unhandled_email/unhandled_email.py @@ -11,4 +11,4 @@ class UnhandledEmail(Document): def remove_old_unhandled_emails(): - frappe.db.sql("""DELETE FROM `tabUnhandled Email` WHERE datediff(curdate(), date(creation)) > 30""") + frappe.db.sql("""DELETE FROM `tabUnhandled Email` WHERE DATEDIFF(CURRENT_DATE(), DATE(creation)) > 30""") From 5b6b10fc279f292c100bf3ca576bbf8921e8a01e Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Tue, 22 Oct 2019 12:01:27 +0530 Subject: [PATCH 3/4] Update unhandled_email.py --- frappe/email/doctype/unhandled_email/unhandled_email.py | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/frappe/email/doctype/unhandled_email/unhandled_email.py b/frappe/email/doctype/unhandled_email/unhandled_email.py index 9f9357c1f2..a106d84103 100644 --- a/frappe/email/doctype/unhandled_email/unhandled_email.py +++ b/frappe/email/doctype/unhandled_email/unhandled_email.py @@ -11,4 +11,5 @@ class UnhandledEmail(Document): def remove_old_unhandled_emails(): - frappe.db.sql("""DELETE FROM `tabUnhandled Email` WHERE DATEDIFF(CURRENT_DATE(), DATE(creation)) > 30""") + frappe.db.sql("""DELETE FROM `tabUnhandled Email` + WHERE creation < %s""", frappe.utils.add_days(frappe.utils.nowdate(), -30)) From 7b405bbc5b53348c186567228ba6882be31db1b0 Mon Sep 17 00:00:00 2001 From: Aditya Hase Date: Tue, 22 Oct 2019 13:04:09 +0530 Subject: [PATCH 4/4] style: Remove trailing space --- frappe/email/doctype/unhandled_email/unhandled_email.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/frappe/email/doctype/unhandled_email/unhandled_email.py b/frappe/email/doctype/unhandled_email/unhandled_email.py index a106d84103..1276da71a1 100644 --- a/frappe/email/doctype/unhandled_email/unhandled_email.py +++ b/frappe/email/doctype/unhandled_email/unhandled_email.py @@ -11,5 +11,5 @@ class UnhandledEmail(Document): def remove_old_unhandled_emails(): - frappe.db.sql("""DELETE FROM `tabUnhandled Email` + frappe.db.sql("""DELETE FROM `tabUnhandled Email` WHERE creation < %s""", frappe.utils.add_days(frappe.utils.nowdate(), -30))