Queue worker supervision helps keep your background jobs running smoothly. It automatically restarts workers if they stop or crash.
Queue worker supervision in Laravel
Start learning this pattern below
Jump into concepts and practice - no test required
php artisan queue:work --daemon # To supervise workers, use a process monitor like Supervisor or Laravel Horizon # Example Supervisor config snippet: [program:laravel-worker] process_name=%(program_name)s_%(process_num)02d command=php /path/to/artisan queue:work --sleep=3 --tries=3 --timeout=90 autostart=true autorestart=true numprocs=3 redirect_stderr=true stdout_logfile=/path/to/worker.log
Laravel itself does not supervise queue workers; you use external tools like Supervisor or Laravel Horizon.
Supervisor config runs multiple worker processes and restarts them if they fail.
[program:laravel-worker] command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 numprocs=1 autostart=true autorestart=true stdout_logfile=/var/log/worker.log
[program:laravel-worker] command=php /var/www/html/artisan queue:work --sleep=3 --tries=3 numprocs=5 autostart=true autorestart=true stdout_logfile=/var/log/worker.log
php artisan horizon
# Laravel Horizon supervises queue workers with a nice dashboard.This Laravel command starts a queue worker. You can supervise it with Supervisor to keep it running automatically.
<?php // This is a simple Laravel command to start a queue worker // Normally, you run this in terminal or supervise it with Supervisor namespace App\Console\Commands; use Illuminate\Console\Command; class StartQueueWorker extends Command { protected $signature = 'queue:start-worker'; protected $description = 'Start a queue worker process'; public function handle() { $this->info('Starting queue worker...'); // This will run the queue worker in daemon mode passthru('php artisan queue:work --sleep=3 --tries=3 --timeout=90'); } } // To supervise this worker, create a Supervisor config like: // [program:laravel-worker] // command=php /path/to/artisan queue:start-worker // autostart=true // autorestart=true // numprocs=1 // stdout_logfile=/path/to/worker.log // Output when running the command manually: // Starting queue worker... // [worker output appears here as jobs are processed]
Queue worker supervision ensures jobs keep running even if a worker crashes.
Supervisor or Laravel Horizon are common tools for this purpose.
Restarting workers after code changes helps apply updates without downtime.
Queue worker supervision keeps background jobs running reliably.
Use Supervisor or Laravel Horizon to monitor and restart workers automatically.
This helps your app handle tasks smoothly without manual restarts.
Practice
Solution
Step 1: Understand Supervisor's role
Supervisor monitors processes and restarts them if they stop.Step 2: Apply this to Laravel queue workers
Laravel queue workers run background jobs; Supervisor ensures they keep running.Final Answer:
To automatically restart queue workers if they stop unexpectedly -> Option AQuick Check:
Supervisor restarts workers automatically [OK]
- Thinking Supervisor speeds up job execution
- Confusing logging with supervision
- Believing Supervisor makes jobs synchronous
Solution
Step 1: Recall Supervisor config syntax
Supervisor uses 'command=' to specify the process command.Step 2: Match with Laravel queue worker command
Laravel queue worker runs with 'php artisan queue:work'.Final Answer:
command=php artisan queue:work -> Option BQuick Check:
Supervisor uses 'command=' for process commands [OK]
- Using 'run=' instead of 'command='
- Confusing 'execute=' or 'start=' as valid directives
- Omitting the full artisan command
[program:laravel-worker] command=php /var/www/artisan queue:work --sleep=3 --tries=3 numprocs=1 autostart=true autorestart=true user=www-data
What happens if the Laravel queue worker process crashes?
Solution
Step 1: Check 'autorestart' setting
autorestart=true means Supervisor restarts crashed processes automatically.Step 2: Understand autostart role
autostart=true means the worker starts on Supervisor start, but restart depends on autorestart.Final Answer:
Supervisor will restart the worker automatically -> Option AQuick Check:
autorestart=true means auto restart on crash [OK]
- Confusing autostart with autorestart
- Assuming manual restart is needed
- Thinking Supervisor only logs crashes
Solution
Step 1: Identify why workers don't restart
Workers won't restart if 'autorestart' is false or missing.Step 2: Check other options' impact
'numprocs=0' disables processes, 'user=root' is allowed but not recommended, '--tries' controls job retries, not restarts.Final Answer:
The 'autorestart' option is set to false or missing -> Option DQuick Check:
autorestart=false stops auto restart [OK]
- Confusing job retry with process restart
- Thinking 'user=root' causes restart failure
- Assuming '--tries' affects Supervisor restart
Solution
Step 1: Understand running multiple workers
Supervisor can run multiple processes with 'numprocs' and unique names.Step 2: Use 'process_name' for unique worker IDs
Using '%(program_name)s_%(process_num)s' creates unique names for each worker.Final Answer:
Set 'numprocs' to the number of workers needed and use '%(program_name)s_%(process_num)s' in 'process_name' -> Option CQuick Check:
Multiple workers = numprocs + unique process_name [OK]
- Duplicating config files with same program name
- Increasing sleep time reduces throughput
- Disabling autostart prevents automatic worker start
