Hi. I’m working on my frappe app to add some functionality to ERPNext.
In hooks.py a have this:
doc_events = {"Sales Invoice": {"on_submit": ["retail_misel.retail_misel.handle_pos_stock.main"]},}
retail_misel.retail_misel.handle_pos_stock
is this:
from frappe.desk.form.linked_with import (get_linked_docs, get_linked_doctypes)
import frappe
import time
def lateUpdate(si_name):
items = []
while True:
time.sleep(3)
pos_invoices=frappe.get_all("POS Invoice",filters={ "consolidated_invoice": si_name },fields=["name",'currency'])
if pos_invoices:
break
for inv in pos_invoices:
items.append(frappe.get_all('POS Invoice Item', filters={'parent': inv['name']},fields=['item_code', 'qty', 'custom_collected_at_pos']))
print('-->>>', pos_invoices, '<<<---')
print('-->>>', items, '<<<---')
def main (doc, method):
print('bf%%%%%%%%%%%%%%%%%%%$$$$$$$$$$$')
if doc.is_pos:
print('enqueueing ->', 'handle_pos_stock::{}'.format(doc.name))
call = frappe.enqueue(lateUpdate,queue='long',si_name=doc.name,job_id='handle_pos_stock::{}'.format(doc.name))
print('->>>>', call)
Some times, when the POS is closed more than one Sales Invoice is create, in this case the function main is called one time for each Sales Invoice created. The problem is that only when the first Sales Invoice is created frappe.enqueue creates a job, I tried passing the paramenter job_id with the Sales Invoice appended that way the job_id is unique.
How can I make sure that each time a Sales Invoice is created frappe.enqueue creates a new job? even if the last job hasn’t finished yet?
edit: To be more clear my problem is this:
the function main is runs multiple times when multiple Sales Invoice are created, when this happens only the first time main is ran a job is queued. I need to ensure each time main runs a new job is created.