fix: dont let email queue stay in Sending status

This commit is contained in:
sokumon 2025-07-11 18:45:20 +05:30
parent b443851ce1
commit 2bbf72061c
2 changed files with 26 additions and 0 deletions

View file

@ -1,5 +1,6 @@
# Copyright (c) 2022, Frappe Technologies Pvt. Ltd. and Contributors
# License: MIT. See LICENSE
from datetime import timedelta
import frappe
from frappe import _, msgprint
@ -177,3 +178,27 @@ def get_queue():
{"now": now_datetime()},
as_dict=True,
)
def mark_sending_emails_as_not_sent():
emails_in_sending = frappe.get_all(
"Email Queue", filters={"status": "Sending"}, fields=["name", "modified"]
)
for e in emails_in_sending:
if now_datetime() - e["modified"] > timedelta(minutes=15):
update_fields = {}
email_queue = frappe.get_doc("Email Queue", e["name"])
sent_to_atleast_one_recipient = any(
rec.recipient for rec in email_queue.recipients if rec.is_mail_sent()
)
if email_queue.retry < cint(frappe.db.get_system_setting("email_retry_limit")) or 3:
update_fields.update(
{
"status": "Partially Sent" if sent_to_atleast_one_recipient else "Not Sent",
"retry": email_queue.retry + 1,
}
)
else:
update_fields.update({"status": "Error"})
update_fields.update({"error": frappe.get_traceback()})
email_queue.update_status(**update_fields, commit=True)

View file

@ -217,6 +217,7 @@ scheduler_events = {
},
"all": [
"frappe.email.queue.flush",
"frappe.email.queue.mark_sending_emails_as_not_sent",
"frappe.monitor.flush",
"frappe.integrations.doctype.google_calendar.google_calendar.sync",
],