From 4e8bbd6c93daf3bc9458382050861e2249cbaf6b Mon Sep 17 00:00:00 2001 From: Aradhya Date: Fri, 2 Dec 2022 12:47:17 +0530 Subject: [PATCH] refactor: allowing unlocking of doc when job id is not set --- .../doctype/submission_queue/submission_queue.js | 2 +- .../doctype/submission_queue/submission_queue.py | 14 +++++++++----- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/frappe/core/doctype/submission_queue/submission_queue.js b/frappe/core/doctype/submission_queue/submission_queue.js index 93d6b981dc..414c8c9ee0 100644 --- a/frappe/core/doctype/submission_queue/submission_queue.js +++ b/frappe/core/doctype/submission_queue/submission_queue.js @@ -3,7 +3,7 @@ frappe.ui.form.on("Submission Queue", { refresh: function (frm) { - if (frm.doc.status === "Queued" && frm.doc.job_id) { + if (frm.doc.status === "Queued") { frm.add_custom_button(__("Unlock Reference Document"), () => { frappe.confirm(__("Are you sure you want to go ahead with this action?"), () => { frm.call("unlock_doc"); diff --git a/frappe/core/doctype/submission_queue/submission_queue.py b/frappe/core/doctype/submission_queue/submission_queue.py index 2bb4200a87..caa0352c97 100644 --- a/frappe/core/doctype/submission_queue/submission_queue.py +++ b/frappe/core/doctype/submission_queue/submission_queue.py @@ -69,6 +69,10 @@ class SubmissionQueue(Document): def background_submission(self, to_be_queued_doc: Document, action_for_queuing: str): # Set the job id for that submission doctype + submission_doc = frappe.get_doc(self.doctype, self.name) + if submission_doc.state == "Failed": + # If the document has already been unlocked by _unlock_reference_doc_unlock_reference_doc + return self.update_job_id(get_current_job().id) _action = action_for_queuing.lower() if _action == "update": @@ -129,9 +133,10 @@ class SubmissionQueue(Document): enqueue_create_notification([notify_to], notification_doc) def _unlock_reference_doc(self): - """ - Only execute if self.job_id is defined. - """ + if not self.job_id: + self.queued_doc.unlock() + frappe.db.set_value(self.doctype, self.name, {"status": "Failed"}) + try: job = Job.fetch(self.job_id, connection=get_redis_conn()) status = job.get_status(refresh=True) @@ -156,8 +161,7 @@ class SubmissionQueue(Document): # NOTE: this can lead to some weird unlocking/locking behaviours. # for example: hitting unlock on a submission could lead to unlocking of another submission # of the same reference document. - - if self.status != "Queued" and not self.job_id: + if self.status != "Queued": return self._unlock_reference_doc()