def background_job_function():
frappe.msgprint(“backround job running”)
frappe.enqueue(
background_job_function(), # python function or a module path as string
queue="default", # one of short, default, long
timeout=None, # pass timeout manually
is_async=True, # if this is True, method is run in worker
now=False, # if this is True, method is run directly (not in a worker)
job_name=None, # specify a job name
enqueue_after_commit=False, # enqueue the job after the database commit is done at the end of the request
at_front=False, # put the job at the front of the queue
)
The above script i entered in server-script (After save) for a Doctype. It is running. But failed. If anybody helps?
Do not execute the function here. Just pass the function as callback/param to frappe.enqueue. Corrected code will be frappe.enqueue(background_job_function, ...)
frappe.enqueue(background_job_function, …)
after modifying this throw below error,
You cannot call the function recursively in background.
Define function first and pass it in enqueue separately.
Check docs Background Jobs
But I defined function before and called it below in the enqueue.
I wrote the below code inside server script. If it success i will try with more logic.
def background_job_function():
frappe.msgprint(“backround job running”)
frappe.enqueue(background_job_function, queue=‘short’)
I think, the above code executed asynchronously, not recursively. If it is failed, what are the other way to enqueue the function, where i initialize that?
Try,
frappe.enqueue("dot.path.to.background_job_function", ...)
The frappe.enqueue()
function in the server script doesn’t invoke the given function directly. Instead, it requires a path to the function. For instance, it works when create a function in the code and pass its path like frappe.enqueue("dot.path.to.background_job_function", ...)
. However, when trying to directly pass the function as frappe.enqueue(function, ...)
, it doesn’t execute.
Expected behavior is (inside server script):
def long_running_job(param1, param2):
# expensive tasks
pass
# directly pass the function
frappe.enqueue(long_running_job, queue='short', param1='A', param2='B')
This should enqueue the long_running_job
function, but in practice, it doesn’t work this way.