refactor: ppcv select with for update and skip locked

This commit is contained in:
ruthra kumar
2026-05-20 11:19:00 +05:30
parent ee33574a6d
commit eba58b2837

View File

@@ -93,12 +93,16 @@ class ProcessPeriodClosingVoucher(Document):
def start_pcv_processing(docname: str): def start_pcv_processing(docname: str):
if frappe.db.get_value("Process Period Closing Voucher", docname, "status") in ["Queued", "Running"]: if frappe.db.get_value("Process Period Closing Voucher", docname, "status") in ["Queued", "Running"]:
frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Running") frappe.db.set_value("Process Period Closing Voucher", docname, "status", "Running")
if normal_balances := frappe.db.get_all(
"Process Period Closing Voucher Detail", ppcvd = qb.DocType("Process Period Closing Voucher Detail")
filters={"parent": docname, "status": "Queued"}, if normal_balances := (
fields=["processing_date", "report_type", "parentfield"], qb.from_(ppcvd)
order_by="parentfield, idx, processing_date", .select(ppcvd.processing_date, ppcvd.report_type, ppcvd.parentfield)
limit=4, .where(ppcvd.parent.eq(docname) & ppcvd.status.eq("Queued"))
.orderby(ppcvd.parentfield, ppcvd.idx, ppcvd.processing_date)
.limit(4)
.for_update(skip_locked=True)
.run(as_dict=True)
): ):
if not is_scheduler_inactive(): if not is_scheduler_inactive():
for x in normal_balances: for x in normal_balances:
@@ -238,12 +242,15 @@ def get_gle_for_closing_account(pcv, dimension_balance, dimensions):
@frappe.whitelist() @frappe.whitelist()
def schedule_next_date(docname: str): def schedule_next_date(docname: str):
if to_process := frappe.db.get_all( ppcvd = qb.DocType("Process Period Closing Voucher Detail")
"Process Period Closing Voucher Detail", if to_process := (
filters={"parent": docname, "status": "Queued"}, qb.from_(ppcvd)
fields=["processing_date", "report_type", "parentfield"], .select(ppcvd.processing_date, ppcvd.report_type, ppcvd.parentfield)
order_by="parentfield, idx, processing_date", .where(ppcvd.parent.eq(docname) & ppcvd.status.eq("Queued"))
limit=1, .orderby(ppcvd.parentfield, ppcvd.idx, ppcvd.processing_date)
.limit(1)
.for_update(skip_locked=True)
.run(as_dict=True)
): ):
if not is_scheduler_inactive(): if not is_scheduler_inactive():
frappe.db.set_value( frappe.db.set_value(