DEV3 Technologies home

DEV3 Developer Forum

How do I keep a Laravel queue worker running with Supervisor and restart it on deploy?

DevOps helpSolved0 views
dev3 ·

php artisan queue:work stops when I close SSH, and after deploying new code the worker still runs old code. How do I configure Supervisor properly?

#supervisor#laravel#queue

0

1 reply

dev3 · Accepted answer

Queue workers are long-running PHP processes that boot the framework once, so they keep old code in memory until restarted. Supervisor keeps them alive, and queue:restart makes them reload after deploys.

Install: sudo apt install supervisor. Create /etc/supervisor/conf.d/laravel-worker.conf:

[program:laravel-worker]
process_name=%(program_name)s_%(process_num)02d
command=php /var/www/app/artisan queue:work --sleep=3 --tries=3 --max-time=3600
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
user=www-data
numprocs=2
redirect_stderr=true
stdout_logfile=/var/www/app/storage/logs/worker.log
stopwaitsecs=3600

Load it:

sudo supervisorctl reread
sudo supervisorctl update
sudo supervisorctl status

On every deploy run:

php artisan queue:restart

This signals workers to exit after finishing the current job; Supervisor starts fresh ones with the new code. It relies on the cache store to carry the signal, so the cache driver must be shared by CLI and workers (not array).

Tips

  • --max-time=3600 makes workers recycle hourly to limit memory growth.
  • stopwaitsecs must exceed your longest job, or Supervisor kills it mid-way.
  • --tries and failed_jobs table: use php artisan queue:failed and queue:retry all.
  • Use queue:work, not queue:listen (slower).
  • Laravel Horizon is optional and Redis-only; Supervisor is enough for database or SQS queues.

An alternative is a systemd unit with Restart=always for the same command.

0

Your reply

Every post is reviewed by DEV3 before it appears. Usually reviewed within 24 hours.

Markdown supported. Use ``` for code blocks. Images are not allowed.