chore: log traceback directly to the queue doc

This commit is contained in:
phot0n 2023-06-07 02:22:50 +05:30
parent 742a6082ac
commit df7afa93b8
2 changed files with 15 additions and 20 deletions

View file

@ -61,6 +61,7 @@
"options": "Not Sent\nSending\nSent\nPartially Sent\nError\nExpired"
},
{
"depends_on": "eval:doc.error",
"fieldname": "error",
"fieldtype": "Code",
"label": "Error"
@ -152,7 +153,7 @@
"idx": 1,
"in_create": 1,
"links": [],
"modified": "2023-06-05 12:15:17.850292",
"modified": "2023-06-07 02:21:28.769620",
"modified_by": "Administrator",
"module": "Email",
"name": "Email Queue",

View file

@ -140,7 +140,9 @@ class EmailQueue(Document):
method(self, self.sender, recipient.recipient, message)
else:
if not frappe.flags.in_test:
ctx.smtp_session.sendmail(from_addr=self.sender, to_addrs=recipient.recipient, msg=message)
ctx.smtp_server.session.sendmail(
from_addr=self.sender, to_addrs=recipient.recipient, msg=message
)
ctx.add_to_sent_list(recipient)
if frappe.flags.in_test:
@ -217,19 +219,24 @@ class SendMailContext:
smtplib.SMTPHeloError,
JobTimeoutException,
]
trace = "".join(traceback.format_tb(exc_tb)) if exc_tb else None
if not self.retain_smtp_session:
self.smtp_server.quit()
self.log_exception(exc_type, exc_val, exc_tb)
if exc_type in exceptions:
update_fields = {"status": "Partially Sent" if self.sent_to else "Not Sent"}
update_fields = {"status": "Partially Sent" if self.sent_to else "Not Sent", "error": trace}
elif exc_type:
update_fields = {"error": trace}
if self.queue_doc.retry < get_email_retry_limit():
update_fields = {"status": "Not Sent", "retry": self.queue_doc.retry + 1}
update_fields.update(
{
"status": "Partially Sent" if self.sent_to else "Not Sent",
"retry": self.queue_doc.retry + 1,
}
)
else:
update_fields = {"status": "Error"}
update_fields.update({"status": "Error"})
else:
update_fields = {
"status": "Sent",
@ -240,19 +247,6 @@ class SendMailContext:
self.queue_doc.update_status(**update_fields, commit=True)
def log_exception(self, exc_type, exc_val, exc_tb):
if exc_type:
traceback_string = "".join(traceback.format_tb(exc_tb))
traceback_string += f"\n Queue Name: {self.queue_doc.name}"
self.queue_doc.log_error("Email sending failed", traceback_string)
@property
def smtp_session(self):
if frappe.flags.in_test:
return
return self.smtp_server.session
def add_to_sent_list(self, recipient):
# Update recipient status
recipient.update_db(status="Sent", commit=True)