refactor: allowing unlocking of doc when job id is not set

This commit is contained in:
Aradhya 2022-12-02 12:47:17 +05:30 committed by phot0n
parent 27a2689678
commit 4e8bbd6c93
2 changed files with 10 additions and 6 deletions

View file

@ -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");

View file

@ -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()