Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to restart supervisor for a Laravel deployment?

I currently use a cron job to call php artisan queue:work --once every minute to work on my jobs queue in production.

I would like to use supervisor instead to handle my queues.

In the docs in the section of supervisor-configuration it states:

Since queue workers are long-lived processes, they will not pick up changes to your code without being restarted. So, the simplest way to deploy an application using queue workers is to restart the workers during your deployment process. You may gracefully restart all of the workers by issuing the queue:restart command:

php artisan queue:restart

This command will instruct all queue workers to gracefully "die" after they finish processing their current job so that no existing jobs are lost. Since the queue workers will die when the queue:restart command is executed, you should be running a process manager such as Supervisor to automatically restart the queue workers.

I don't understsand the last sentence. So lets say I have installed and configured the supervisor as described here and I manually logged into the server through ssh and started supervisor:

sudo supervisorctl start laravel-worker:*

Do I need to call php artisan queue:restart on deployment? If so, then this will only kill all current workers, how do I tell supervisor to restart the queue workers? Do I need to call sudo supervisorctl restart laravel-worker:* in the deployment after php artisan queue:restart?

like image 767
Adam Avatar asked Sep 02 '25 07:09

Adam


1 Answers

I struggled with this for quite some time. It is a little confusing, and the docs tend to point to each other rather than explain how the whole system works.

The whole point of installing supervisor on your server is to automate the queue process within any Laravel apps running on the machine. If you look at the example file on the help page you linked to, all it is doing is going into a specific Laravel instance and starting the queue.

When supervisor starts, it is the equivalent of:

php artisan queue:start

within your Laravel folder. Supervisor is running the process. But, you still have control to restart the queue, either through sudo supervisorctl restart laravel-worker:* or a php artisan queue:restart within a single Laravel folder. You do not need to call a restart to supervisor if you are manually restarting the queue with the aritsan command - that would be redundant. You can test this out by doing a restart of the queue and monitoring code changes updates, or looking at the queue itself to see that all jobs restart.

The 'gotcha' with all of this is that if you introduce new code and deploy it, you must remember to restart the queue for that instance.

To make things more complicated, but to eventually make it simple, take a look at Laravel Horizon, which basically takes the place of the supervisor in a way that is a bit easier to maintain.

like image 75
Watercayman Avatar answered Sep 04 '25 22:09

Watercayman