Laravel Artisan CLI safely stop daemon queue workers

Vigs picture Vigs · May 5, 2015 · Viewed 31.5k times · Source

In order to process large numbers of jobs, I run a variable number of queue workers depending on howmuch work there is to complete. I don't want to run more workers than are necessary to complete the work that needs to be done in a time period that we deem appropriate.

At the moment, I start 5 daemon queue workers for testing purposes, however in production this number could be between 25 & 100 workers, possibly more. I understand that when deploying, I have to stop the queue workers by first placing the framework in maintenance mode by using php artisan down, because the --daemon flag causes the framework to only load when the worker starts up, hence new code would not take affect during the deploy until the worker restarts.

If i needed to stop the workers for some reason, I could put the application in maintenance mode using php artisan down which will cause the workers to die once they have finished processing their current job (if they are working one). However, there may be times where I want to kill the workers without putting the whole application in maintenance mode.

Is there a safe way to stop the workers in a way where they will continue processing their current job and then die without placing the whole application in maintenance mode?

Essentially what I need is a php artisan queue:stop, which behaves like php artisan queue:restart, but does not restart the worker once the job is done.

I was expecing there to be a like php artisan queue:stop command that would do this, but that doesn't seem to be the case.

Using ps aux | grep php I am able to get the process id's for the workers, and I could kill the processes that way, but I don't want to kill the process in the middle of it working on a job.

Thanks.

Answer

Nikko picture Nikko · May 7, 2015

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.