Skip to content

Commit

Permalink
chore: retry failed outgoing mail logs
Browse files Browse the repository at this point in the history
  • Loading branch information
s-aga-r committed Oct 26, 2024
1 parent eae32ff commit 2b5cae9
Show file tree
Hide file tree
Showing 2 changed files with 22 additions and 7 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@
"processed_after",
"transfer_started_after",
"transfer_completed_after",
"failed_count",
"section_break_rwda",
"spam_check_response",
"message"
Expand Down Expand Up @@ -302,11 +303,22 @@
"non_negative": 1,
"precision": "2",
"read_only": 1
},
{
"default": "0",
"depends_on": "eval: doc.failed_count",
"fieldname": "failed_count",
"fieldtype": "Int",
"label": "Failed Count",
"no_copy": 1,
"non_negative": 1,
"read_only": 1,
"search_index": 1
}
],
"index_web_pages_for_search": 1,
"links": [],
"modified": "2024-10-26 13:13:29.554348",
"modified": "2024-10-26 13:36:06.779670",
"modified_by": "Administrator",
"module": "Mail Server",
"name": "Outgoing Mail Log",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -204,7 +204,7 @@ def _db_set(
def retry_failed(self) -> None:
"""Retries failed email."""

if self.status == "Failed":
if self.status == "Failed" and self.failed_count < 3:
self._db_set(status="Accepted", error_log=None, error_message=None, commit=True)
self.push_to_queue()

Expand All @@ -227,7 +227,7 @@ def push_to_queue(self) -> None:
self.load_from_db()

# Ensure the document is "Accepted"
if self.status != "Accepted":
if not (self.status == "Accepted" and self.failed_count < 3):
return

from mail_server.rabbitmq import OUTGOING_MAIL_QUEUE, rabbitmq_context
Expand Down Expand Up @@ -265,7 +265,9 @@ def push_to_queue(self) -> None:
)
except Exception:
error_log = frappe.get_traceback(with_context=False)
self._db_set(status="Failed", error_log=error_log, commit=True)
self._db_set(
status="Failed", error_log=error_log, failed_count=self.failed_count + 1, commit=True
)


def create_outgoing_mail_log(
Expand Down Expand Up @@ -324,7 +326,7 @@ def push_emails_to_queue() -> None:
OML.domain_name,
GroupConcat(MLR.email).as_("recipients"),
)
.where(OML.status == "Accepted")
.where((OML.failed_count < 3) & (OML.status.isin(["Accepted", "Failed"])))
.groupby(OML.name)
.orderby(OML.priority, order=Order.desc)
.orderby(OML.received_at)
Expand All @@ -344,10 +346,10 @@ def push_emails_to_queue() -> None:
transfer_started_at = %s,
transfer_started_after = TIMESTAMPDIFF(SECOND, `processed_at`, `transfer_started_at`)
WHERE
status = %s AND
status IN %s AND
name IN %s
""",
("Queuing (RMQ)", now(), "Accepted", tuple(mail_list)),
("Queuing (RMQ)", now(), ("Accepted", "Failed"), tuple(mail_list)),
)
frappe.db.commit()

Expand Down Expand Up @@ -388,6 +390,7 @@ def push_emails_to_queue() -> None:
frappe.qb.update(OML)
.set(OML.status, "Failed")
.set(OML.error_log, error_log)
.set(OML.failed_count, OML.failed_count + 1)
.where((OML.name.isin(mail_list)) & (OML.status == "Queuing (RMQ)"))
).run()

Expand Down

0 comments on commit 2b5cae9

Please sign in to comment.