I have a controller function which dispatchs a job
. When this job
is handled, at the end it dispatch the same job
again (with different args). There are 5 jobs of the same in total.
Queue driver: database
The problem is: I record the time duration from create()
to handle()
. The first job dispatched by the controller took 1700ms, where the other jobs dispatched by the jobs themselves took only 40ms.
Queue driver: sync
When I changed to use sync
queue driver, all the jobs worked in lightning speed.
Findings:
The first queue job took long time from create()
to handle()
. Before that the queue was empty. It might be the queue driver problem.
Why and how to fix please? thanks!!
UPDATE:
Added a TestJob
that dispatch itself when job was handled. That meant the queue always had a TestJob
being handled or waiting for handling.
Repeating my original jobs, they all took only <70ms from created()
to handle()
completed.
Conclusion:
I am pretty sure it is the queue driver problem. Looks like the worker fall asleep when the queue is empty. Do anyone know the fix please?
I post my answer here, hope to be useful to someone else on the internet.
Reference
https://divinglaravel.com/queue-system/workers
Answer
After putting echo
and echo debug_backtrace()[1]['function'];
everywhere, it was found that there was DEFAULT VALUE of sleep when queue was empty, inside Illuminate\Queue\Console\WorkCommand
protected $signature = 'queue:work{--sleep=3 : Number of seconds to sleep when no job is available}
Solution
so the solution is to mention sleep
in the console command:
php artisan queue:work --sleep=0
Thanks for reading.