0
0
Laravelframework~5 mins

Queue worker supervision in Laravel

Choose your learning style9 modes available
Introduction

Queue worker supervision helps keep your background jobs running smoothly. It automatically restarts workers if they stop or crash.

You want to process tasks like sending emails or resizing images without slowing down the user experience.
Your app uses queues to handle jobs in the background and you want to make sure workers don't stop unexpectedly.
You want to automatically restart queue workers after code updates or errors.
You want to monitor and manage multiple queue workers easily.
You want to avoid manual restarts of queue workers after server reboots.
Syntax
Laravel
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.

Examples
Simple Supervisor config for one worker process that restarts automatically.
Laravel
[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
Supervisor config for five worker processes to handle more jobs in parallel.
Laravel
[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
Using Laravel Horizon to supervise and monitor queue workers with a web interface.
Laravel
php artisan horizon

# Laravel Horizon supervises queue workers with a nice dashboard.
Sample Program

This Laravel command starts a queue worker. You can supervise it with Supervisor to keep it running automatically.

Laravel
<?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]
OutputSuccess
Important Notes

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.

Summary

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.