تماشای این درس نیاز به اشتراک حرفه‌ای دارد.

Keeping Workers Alive0:00

A worker is a php process that's meant to run indefinitely, but in the real world it may exit or crash for different reasons. To ensure our workers are always up and running, we need to use a process monitoring tool that will start a new instance of the process if one crashes. One of the most popular tools is Supervisor, which can be easily installed on any Unix like operating systems. And if you are using Laravel Forge, this tool is already installed and configured by default. All we need to do is to create a worker from the Forge dashboard, fill the form here and hit start worker. Forge will configure Supervisor to keep that worker alive at all times.

Restarting Workers on Deploy0:42

hit start worker. Forge will configure Supervisor to keep that worker alive at all times. Now since the worker process is a long living one, it needs to be restarted after each deployment in order for the code changes to reflect. To do that, we need to make sure to run the php artisan queue:restart command in our deployment script. So let's check the deployment script on our Forge site here. And we can see that by default the new code will be pulled from git, dependencies will be installed by composer, PHP FPM will get restarted, migrations were run, and finally the queue restart signal will be sent by calling the php artisan queue:restart command.

be installed by composer, php-fpm will get restarted, migrations were run, and finally the queue restart signal will be sent by calling the php artisan queue:restart command. After php-fpm is restarted, your application visitors will start using the new code, your HTTP users will use the new code, while the workers are still running on older code. Eventually after sending the restart signal, eventually those workers will exit and be started again by Supervisor. The restart signal is a value Laravel stores in the cache that workers read before pulling new jobs.

Migration Risks with Old Workers1:55

The restart signal is a value Laravel stores in the cache that workers read before pulling new jobs. Once a worker detects this signal, it will exit. That means if you have multiple workers running and the restart signal was sent, some of these workers may not restart right away, they will wait for a job in hand to be processed before exiting. While other workers will restart immediately. If you are deploying new code along with migrations that will change the database schema, workers that are still using the old code may fail in the middle of running their last job, due to those migration changes, old code working with the new database schema.

Graceful Stop/Start via Supervisor2:29

that are still using the old code may fail in the middle of running their last job, due to those migration changes, old code working with the new database schema. To prevent this from happening, you will need to signal the workers to exit and then wait for them to exit. Only when all workers exit gracefully, you can start your deployment. To do that we need to use the supervisor stop command instead of the php artisan queue:restart command. So here we are going to call sudo stop and then use the worker name. If you are not sure about the worker name, ask for support. This stop command here will block the execution of the deployment script until all workers are shut down.

This stop command here will block the execution of the deployment script until all workers are shut down. Now your workers will be signaled by supervisor to stop after processing any jobs in hand. After all workers exit, the deployment script will continue as normal, migrations will run and in the end workers need to be started again. To do that let's replace the q restart command and use the start supervisor control command instead. You should know though that the supervisor control stop command may take time to execute, depending on how many workers you have and if any long running job is being processed. This will increase your deployment time.

Choosing Stop vs Restart3:54

depending on how many workers you have and if any long running job is being processed. This will increase your deployment time. So only use this method if you know for sure that you have made changes that will break jobs currently in the queue. Otherwise use the php artisan queue:restart command.

دوست دارید گاهی خبرهای Laracasts را ایمیل کنیم؟