As we explored the manual setup, in this lesson I will show you that there's a more convenient way to set up queues a lot faster.
Laravel Forge provides us with a convenient UI and helps us set up Queue Workers or Horizon daemon without having to scratch heads on how to configure supervisor.
When creating a new queue worker, you can choose configuration options as we did in the command line or in the supervisor configuration. The configuration will be used when executing the queue worker.
⚠️ You should ensure that the value of
Graceful Shutdown Seconds
is greater than theMaximum Seconds Per Job
. Otherwise, Supervisor may kill the job before it is finished processing.
We can inspect what really happened behind the scenes, after logging in to the server we can check what config for the supervisor was generated on the server:
/etc/supervisor/conf.d/worker-529763.conf
[program:worker-529763]command=php8.1 /home/forge/example.org/artisan queue:listen redis --sleep=1 --quiet --timeout=30 --delay=3 --tries=3 --queue="default" process_name=%(program_name)s_%(process_num)02dautostart=trueautorestart=truestopasgroup=truekillasgroup=trueuser=forgenumprocs=10stopwaitsecs=30stdout_logfile=/home/forge/.forge/worker-529763.log
$FORGE_PHP artisan horizon:terminate
Artisan command to the end of your site's deployment script.Default deployment script with added queue:restart
command looks like this:
cd /home/forge/example.orggit pull origin $FORGE_SITE_BRANCH $FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader ( flock -w 10 9 || exit 1 echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock if [ -f artisan ]; then $FORGE_PHP artisan migrate --forcefi $FORGE_PHP artisan queue:restart
We need to restart the queue worker for code changes to take an effect after deployment.
If your Laravel application is using Laravel Horizon, you should not set up queue workers as described above. Instead, you may enable Horizon on Forge using Forge's "daemon" feature.
php8.1 artisan horizon
Artisan command from your site's root directory.It is important to note that the number of processes is set to 1
because child workers are managed by Horizon itself in the config/horizon.php file. This allows us to set up a daemon for Horizon once, and push changes by directly updating the configuration in the source code.
⚠️ You should ensure that the value of Stop Seconds is greater than the number of seconds consumed by your longest-running job. Otherwise, Supervisor may kill the job before it is finished processing.
By logging into the server we can review the configuration that was created for the supervisor on the server:
/etc/supervisor/conf.d/daemon-736205.conf
[program:daemon-736205]directory=/home/forge/example.org/command=php8.1 artisan horizon process_name=%(program_name)s_%(process_num)02dautostart=trueautorestart=trueuser=forgenumprocs=1startsecs=1redirect_stderr=truestdout_logfile=/home/forge/.forge/daemon-736205.logstopwaitsecs=30stopsignal=SIGTERMstopasgroup=truekillasgroup=true
$FORGE_PHP artisan horizon:terminate
Artisan command to your site's deployment script.cd /home/forge/example.orggit pull origin $FORGE_SITE_BRANCH $FORGE_COMPOSER install --no-dev --no-interaction --prefer-dist --optimize-autoloader ( flock -w 10 9 || exit 1 echo 'Restarting FPM...'; sudo -S service $FORGE_PHP_FPM reload ) 9>/tmp/fpmlock if [ -f artisan ]; then $FORGE_PHP artisan migrate --forcefi $FORGE_PHP artisan horizon:terminate
horizon:terminate
lets Horizon finish currently working jobs and ends the process. The supervisor runs horizon again for us like in the first case. This is important for code changes to take an effect.