A whitelisted function that uses enqueue(aMethod) responds only when queued job ends

I need to launch a long running process through an API call.

I have this:

from frappe.utils.background_jobs import enqueue
     :     :     :     :
@frappe.whitelist()
def test_enqueue():
   ulog("Enqueuing Long Task")
   enqueue('myapp.myapp.doctype.myapp.myapp.longTask', now = True, timeout=120000)
   return { "result": "Enqueued Long Task" }

I expected to get the immediate response, {"message":{"result":"Enqueued Long Task"}}.
Instead, I get that response message only when the “enqueued” process completes (after about 10 minutes).

Have I misunderstood something?


Notes:

  1. Version: ERPNext V13.beta.6
  2. in ${benchDir}/config/supervisor.conf
     :     :     :     :
[program:frappe-bench-DENH-frappe-web]
command=/home/erpdev/frappe-bench-DENH/env/bin/gunicorn -b 127.0.0.1:8000 -w 4 -t 120 frappe.app:application --preload
priority=4
     :     :     :     :

Try to remove now = True
And you can use something like this
enqueue(method=make_payment_entry, queue='short', timeout=1000, is_async=True , kwargs =nmb_doc )

2 Likes

That did the trick.

Waaaaay too easy to assume async would be the default, no?

Hello,

I tried same way but somehow it is not working for me. I want message from message function after timeout time is completed.

import frappe
from frappe.utils import now_datetime
from datetime import datetime, timedelta

def message():
	frappe.msgprint("Inside")
	print("Completed Schedule Task.")

@frappe.whitelist()
def on_submit(docname,time):
	# Calculate the delay in seconds between now and the schedule_date_time
	schedule_time = datetime.strptime(time, '%Y-%m-%d %H:%M:%S')
	one_day_earlier = schedule_time - timedelta(days=1)
	hours = one_day_earlier.hour
	minutes = one_day_earlier.minute
	now = now_datetime()
	delay = (schedule_time - now).total_seconds()
	if delay > 0:
		frappe.enqueue(
				method=message,
				queue="long",
				timeout=delay,
				is_async=True,			
			)
		frappe.msgprint("Started")