Laravel Artisan CLI safely stop daemon queue workers Laravel Artisan CLI safely stop daemon queue workers laravel laravel

Laravel Artisan CLI safely stop daemon queue workers


When using the --daemon flag workers shouldn't quit when the queue is empty.

I think what you are looking for is in the documentation for queues.

The php artisan queue:restart command will prompt the workers to restart after they are done their current job.


We've implemented something like this in our application - but it was not something that was built-in to Laravel itself. You would have to edit this file, by adding another condition to the if-block so that it would call the stop function. You can do this by either setting a static variable in the Worker class that gets changed whenever you run a custom command that you'll have to make (i.e. php artisan queue:pause) or by checking an atomic value somewhere (i.e. set it in some cache like redis, memcached, APC or even MySQL, though this would mean you'll have one MySQL query for every cycle of this while-loop) that you set using the same custom command.


Since Laravel 5.5 there is an event called Illuminate\Queue\Events\Looping that gets fired from the daemonShouldRun() call inside the main worker loop of Illuminate\Queue\Worker. So if you setup a listener to do your should process jobs check, and return false then the queue worker(s) will stop until the check returns true. There's a sleep between the next time it checks it which you can customise by passing --sleep <seconds> to the queue:work command.

I'm currently using this technique during deployments to stop workers which run inside docker containers, as it's not so easy to run the suggested queue:restart on them without hacking around.