refactor(minor): better notification
* changed completed_at fieldname to ended_at (in submission queue doctype) * added rollback on exception in queue_in_background method
This commit is contained in:
parent
ae24f17e42
commit
cb796a4e64
3 changed files with 51 additions and 32 deletions
|
|
@ -11,11 +11,11 @@
|
|||
"enqueued_by",
|
||||
"job_id",
|
||||
"column_break_5",
|
||||
"ended_at",
|
||||
"ref_doctype",
|
||||
"ref_docname",
|
||||
"section_break_8",
|
||||
"message",
|
||||
"completed_at"
|
||||
"message"
|
||||
],
|
||||
"fields": [
|
||||
{
|
||||
|
|
@ -75,15 +75,15 @@
|
|||
"read_only": 1
|
||||
},
|
||||
{
|
||||
"fieldname": "completed_at",
|
||||
"fieldtype": "Data",
|
||||
"label": "Completed At",
|
||||
"fieldname": "ended_at",
|
||||
"fieldtype": "Datetime",
|
||||
"label": "Ended At",
|
||||
"read_only": 1
|
||||
}
|
||||
],
|
||||
"index_web_pages_for_search": 1,
|
||||
"links": [],
|
||||
"modified": "2022-10-05 16:09:36.663459",
|
||||
"modified": "2022-10-06 01:11:11.835985",
|
||||
"modified_by": "Administrator",
|
||||
"module": "Core",
|
||||
"name": "Submission Queue",
|
||||
|
|
|
|||
|
|
@ -1,12 +1,11 @@
|
|||
# Copyright (c) 2022, Frappe Technologies and contributors
|
||||
# For license information, please see license.txt
|
||||
|
||||
from datetime import datetime
|
||||
|
||||
import frappe
|
||||
from frappe import _
|
||||
from frappe.desk.doctype.notification_log.notification_log import enqueue_create_notification
|
||||
from frappe.model.document import Document
|
||||
from frappe.utils import now
|
||||
|
||||
|
||||
class SubmissionQueue(Document):
|
||||
|
|
@ -15,10 +14,12 @@ class SubmissionQueue(Document):
|
|||
self.action_for_queuing = action
|
||||
super().insert()
|
||||
|
||||
def lock(self, timeout=None):
|
||||
def lock(self):
|
||||
self.to_be_queued_doc.lock()
|
||||
|
||||
def unlock(self):
|
||||
# NOTE: this is called in execute_action method of Document class
|
||||
# where get_doc is called hence we lose the to_be_queued_doc attribute
|
||||
frappe.get_doc(self.ref_doctype, self.ref_docname).unlock()
|
||||
|
||||
def after_insert(self):
|
||||
|
|
@ -26,9 +27,14 @@ class SubmissionQueue(Document):
|
|||
"queue_in_background",
|
||||
to_be_queued_doc=self.to_be_queued_doc,
|
||||
action_for_queuing=self.action_for_queuing,
|
||||
timeout=600,
|
||||
)
|
||||
frappe.db.set_value(
|
||||
self.doctype,
|
||||
self.name,
|
||||
{"job_id": job.id, "enqueued_at": now()},
|
||||
update_modified=False,
|
||||
)
|
||||
frappe.db.set_value(self.doctype, self.name, {"job_id": job.id}, update_modified=False)
|
||||
|
||||
|
||||
def queue_in_background(self, to_be_queued_doc: Document, action_for_queuing: str):
|
||||
_action = action_for_queuing.lower()
|
||||
|
|
@ -38,37 +44,44 @@ class SubmissionQueue(Document):
|
|||
|
||||
try:
|
||||
getattr(to_be_queued_doc, _action)()
|
||||
values = {"status": "Completed", "completed_at": datetime.now()}
|
||||
except Exception as e:
|
||||
values = {"status": "Failed", "message": str(e)}
|
||||
values = {"status": "Completed"}
|
||||
except Exception:
|
||||
values = {"status": "Failed", "message": frappe.get_traceback()}
|
||||
frappe.db.rollback()
|
||||
|
||||
values["ended_at"] = now()
|
||||
frappe.db.set_value(self.doctype, self.name, values, update_modified=False)
|
||||
notify(name=self.name)
|
||||
self.notify(values["status"], action_for_queuing)
|
||||
|
||||
def notify(self, submission_status: str, action: str):
|
||||
if submission_status == "Failed":
|
||||
doctype = "Submission Queue"
|
||||
docname = self.name
|
||||
message = _("Submission of {0} {1} with action {2} failed")
|
||||
else:
|
||||
doctype = self.ref_doctype
|
||||
docname = self.ref_docname
|
||||
message = _("Submission of {0} {1} with action {2} completed successfully")
|
||||
|
||||
def notify(name: str):
|
||||
# Todo: fix notification
|
||||
notification_doc = {
|
||||
"type": "Notification",
|
||||
"document_type": "Submission Queue",
|
||||
"document_name": name,
|
||||
"subject": "Job Queued",
|
||||
"from_user": frappe.session.user,
|
||||
"email_content": "Hello",
|
||||
notification_doc = {
|
||||
"type": "Alert",
|
||||
"document_type": doctype,
|
||||
"document_name": docname,
|
||||
"subject": message.format(
|
||||
frappe.bold(str(self.ref_doctype)), frappe.bold(self.ref_docname), frappe.bold(action)
|
||||
),
|
||||
}
|
||||
|
||||
mention = frappe.db.get_value("User", filters=frappe.db.get_value(
|
||||
"Submission Queue", filters=name, fieldname="created_by"
|
||||
), fieldname="email")
|
||||
if mention:
|
||||
enqueue_create_notification([mention], notification_doc)
|
||||
notify_to = frappe.db.get_value("User", self.enqueued_by, fieldname="email")
|
||||
enqueue_create_notification([notify_to], notification_doc)
|
||||
|
||||
|
||||
def queue_submission(doc: Document, action: str):
|
||||
queue = frappe.new_doc("Submission Queue")
|
||||
queue.state = "Queued"
|
||||
queue.enqueued_at = datetime.now()
|
||||
queue.enqueued_by = frappe.session.user
|
||||
queue.ref_doctype = doc.doctype
|
||||
queue.ref_docname = doc.name
|
||||
queue.insert(doc, action)
|
||||
|
||||
return queue.name
|
||||
|
|
|
|||
|
|
@ -19,8 +19,14 @@ def savedocs(doc, action):
|
|||
doc.docstatus = {"Save": 0, "Submit": 1, "Update": 1, "Cancel": 2}[action]
|
||||
if doc.docstatus == 1:
|
||||
if doc.meta.submit_in_background:
|
||||
queue_submission(doc, action)
|
||||
frappe.msgprint(frappe._("Queued for Submission"), indicator="green", alert=True)
|
||||
queue_name = queue_submission(doc, action)
|
||||
frappe.msgprint(
|
||||
frappe._("Queued for Submission. You can track the progress over {0}.").format(
|
||||
f"<a href='/app/submission-queue/{queue_name}'><b>here</b></a>"
|
||||
),
|
||||
indicator="green",
|
||||
alert=True,
|
||||
)
|
||||
return
|
||||
else:
|
||||
doc.submit()
|
||||
|
|
|
|||
Loading…
Add table
Reference in a new issue