mirror of
https://github.com/frappe/erpnext.git
synced 2026-05-26 00:14:50 +00:00
refactor: make sure only one task is running for doc
(cherry picked from commit 78c9cc63b1)
This commit is contained in:
@@ -24,7 +24,7 @@ frappe.ui.form.on("Transaction Deletion Record", {
|
|||||||
frm.add_custom_button(execute_btn, () => {
|
frm.add_custom_button(execute_btn, () => {
|
||||||
// Entry point for chain of events
|
// Entry point for chain of events
|
||||||
frm.call({
|
frm.call({
|
||||||
method: 'delete_bins',
|
method: 'process_tasks',
|
||||||
doc: frm.doc
|
doc: frm.doc
|
||||||
});
|
});
|
||||||
});
|
});
|
||||||
|
|||||||
@@ -1,6 +1,7 @@
|
|||||||
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
|
# Copyright (c) 2021, Frappe Technologies Pvt. Ltd. and contributors
|
||||||
# For license information, please see license.txt
|
# For license information, please see license.txt
|
||||||
|
|
||||||
|
from collections import OrderedDict
|
||||||
|
|
||||||
import frappe
|
import frappe
|
||||||
from frappe import _, qb
|
from frappe import _, qb
|
||||||
@@ -42,15 +43,17 @@ class TransactionDeletionRecord(Document):
|
|||||||
def __init__(self, *args, **kwargs):
|
def __init__(self, *args, **kwargs):
|
||||||
super(TransactionDeletionRecord, self).__init__(*args, **kwargs)
|
super(TransactionDeletionRecord, self).__init__(*args, **kwargs)
|
||||||
self.batch_size = 5000
|
self.batch_size = 5000
|
||||||
# Tasks are listged by their execution order
|
# Tasks are listed by their execution order
|
||||||
self.task_to_internal_method_map = {
|
self.task_to_internal_method_map = OrderedDict(
|
||||||
"Delete Bins": "delete_bins",
|
{
|
||||||
"Delete Leads and Addresses": "delete_lead_addresses",
|
"Delete Bins": "delete_bins",
|
||||||
"Reset Company Values": "reset_company_values",
|
"Delete Leads and Addresses": "delete_lead_addresses",
|
||||||
"Clear Notifications": "delete_notifications",
|
"Reset Company Values": "reset_company_values",
|
||||||
"Initialize Summary Table": "initialize_doctypes_to_be_deleted_table",
|
"Clear Notifications": "delete_notifications",
|
||||||
"Delete Transactions": "delete_company_transactions",
|
"Initialize Summary Table": "initialize_doctypes_to_be_deleted_table",
|
||||||
}
|
"Delete Transactions": "delete_company_transactions",
|
||||||
|
}
|
||||||
|
)
|
||||||
|
|
||||||
def validate(self):
|
def validate(self):
|
||||||
frappe.only_for("System Manager")
|
frappe.only_for("System Manager")
|
||||||
@@ -71,10 +74,19 @@ class TransactionDeletionRecord(Document):
|
|||||||
method = self.task_to_internal_method_map[task]
|
method = self.task_to_internal_method_map[task]
|
||||||
return f"{self.name}_{method}"
|
return f"{self.name}_{method}"
|
||||||
|
|
||||||
|
def generate_job_name_for_next_tasks(self, task=None):
|
||||||
|
job_names = []
|
||||||
|
current_task_idx = list(self.task_to_internal_method_map).index(task)
|
||||||
|
for idx, task in enumerate(self.task_to_internal_method_map.keys(), 0):
|
||||||
|
# generate job_name for next tasks
|
||||||
|
if idx > current_task_idx:
|
||||||
|
job_names.append(self.generate_job_name_for_task(task))
|
||||||
|
return job_names
|
||||||
|
|
||||||
def generate_job_name_for_all_tasks(self):
|
def generate_job_name_for_all_tasks(self):
|
||||||
job_names = []
|
job_names = []
|
||||||
for method in self.task_to_internal_method_map.values():
|
for task in self.task_to_internal_method_map.keys():
|
||||||
job_names.append(self.generate_job_name_for_task)
|
job_names.append(self.generate_job_name_for_task(task))
|
||||||
return job_names
|
return job_names
|
||||||
|
|
||||||
def before_submit(self):
|
def before_submit(self):
|
||||||
@@ -116,6 +128,10 @@ class TransactionDeletionRecord(Document):
|
|||||||
|
|
||||||
def chain_call(self, task=None):
|
def chain_call(self, task=None):
|
||||||
if task and task in self.task_to_internal_method_map:
|
if task and task in self.task_to_internal_method_map:
|
||||||
|
# make sure that none of next tasks are already running
|
||||||
|
job_names = self.generate_job_name_for_next_tasks(task=task)
|
||||||
|
self.validate_running_task_for_doc(job_names=job_names)
|
||||||
|
|
||||||
method = self.task_to_internal_method_map[task]
|
method = self.task_to_internal_method_map[task]
|
||||||
job_id = self.generate_job_name_for_task(task)
|
job_id = self.generate_job_name_for_task(task)
|
||||||
|
|
||||||
@@ -162,15 +178,15 @@ class TransactionDeletionRecord(Document):
|
|||||||
get_link_to_form("Transaction Deletion Record", self.name)
|
get_link_to_form("Transaction Deletion Record", self.name)
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
# make sure that job none of tasks are already running
|
|
||||||
job_names = self.generate_job_name_for_all_tasks()
|
|
||||||
self.validate_running_task_for_doc(job_names=job_names)
|
|
||||||
|
|
||||||
@frappe.whitelist()
|
@frappe.whitelist()
|
||||||
def delete_bins(self):
|
def process_tasks(self):
|
||||||
# This methid is the entry point for the chain of events that follow
|
# This method is the entry point for the chain of events that follow
|
||||||
self.db_set("status", "Running")
|
self.db_set("status", "Running")
|
||||||
|
self.chain_call(task="Delete Bins")
|
||||||
|
|
||||||
|
def delete_bins(self):
|
||||||
|
self.validate_doc_status()
|
||||||
if not self.delete_bin_data:
|
if not self.delete_bin_data:
|
||||||
frappe.db.sql(
|
frappe.db.sql(
|
||||||
"""delete from `tabBin` where warehouse in
|
"""delete from `tabBin` where warehouse in
|
||||||
|
|||||||
Reference in New Issue
Block a user