Search by

diiimonn / laravel-queue-on-demand

diiimonn

Laravel queue workers that exist only while there is work. One light dispatcher forks stock queue:work on demand; running jobs renew their lease, so long jobs never run twice.

Package info

github.com/diiimonn/laravel-queue-on-demand

pkg:composer/diiimonn/laravel-queue-on-demand

Statistics

Installs: 5

Dependents: 0

Suggesters: 0

Stars: 0

Open Issues: 0

v0.2.0 2026-12-04 20:25 UTC

This package is auto-updated.

Last update: 2026-12-04 20:28:10 UTC


README

tests

Queue workers that exist only while there is work. One light dispatcher starts stock queue:work processes when jobs arrive and lets them exit when the queue is empty, so an idle queue costs no memory. Running jobs renew their reservation from inside, so a long job is never picked up a second time.

How a Laravel queue grows from one worker to this, step by step: illustrated explainer.

Requirements

  • PHP 8.2+ with pcntl and posix (Linux or macOS)
  • Laravel 12 or 13
  • Queue driver: database, redis, sqs or beanstalkd

Installation

composer require diiimonn/laravel-queue-on-demand
php artisan vendor:publish --tag=queue-on-demand-config

Replace all your queue:work programs with a single dispatcher.

supervisor

[program:queue-on-demand]
command=php /var/www/app/artisan queue:on-demand
autostart=true
autorestart=true
stopasgroup=true
killasgroup=true
stopwaitsecs=3600
user=www-data

stopwaitsecs should exceed your longest job: on stop, workers finish the current job first.

systemd

[Service]
ExecStart=/usr/bin/php /var/www/app/artisan queue:on-demand
Restart=always
KillMode=control-group
KillSignal=SIGTERM
TimeoutStopSec=3600
User=www-data

pm2

pm2 start "php artisan queue:on-demand" --name queue-on-demand --kill-timeout 3600000

Configuration

config/queue-on-demand.php:

return [
    'connection' => null,          // queue connection to serve, null = queue.default
    'interval' => 2,               // seconds between counts of ready jobs
    'max_workers' => 4,            // hard limit on workers across all groups
    'spawn_per_tick' => 4,         // workers started per tick at most

    // Queues served together; queue order inside a group is the priority.
    'groups' => [
        'default' => ['queues' => ['high', 'default'], 'max' => 3, 'timeout' => 60],
        'reports' => ['queues' => ['reports'], 'max' => 1, 'timeout' => 1800,
                      'options' => ['--backoff' => 30]],   // any other queue:work option
    ],

    // Lifetime of each worker.
    'worker' => [
        'stop_when_empty_for' => 5,  // seconds without work before exiting, 0 = at once
        'max_jobs' => 100,
        'max_time' => 300,
        'memory' => 192,             // MB
        'tries' => 1,
    ],

    // Reservation renewal from inside a running job.
    'renewal' => [
        'enabled' => true,
        'interval' => 15,            // seconds
        'except_queues' => [],       // very short, very frequent jobs
    ],

    'release_stale_locks' => true, // free WithoutOverlapping locks of jobs whose worker died
    'spawn' => 'fork',             // or 'exec': a fresh php process per worker
    'idle_restart' => 3600,        // idle dispatcher exits to pick up new code, 0 = never
];

Set retry_after of your queue connection to about 3–4 × renewal.interval (60 with the defaults). A live job keeps renewing and never expires; the job of a crashed worker is released after that window and retried according to tries.

release_stale_locks: a worker killed in the middle of a job never releases the job's WithoutOverlapping lock, so the retry would wait for that lock to expire (forever without expireAfter()). The package notices that the previous attempt never ended and releases the lock when the job is taken again.

max_workers is the memory guard: each busy worker needs about as much memory as one request of your app.

spawn: fork starts workers fast and shares memory pages with the dispatcher. exec starts each worker as a new php artisan queue:work process: slower, but nothing is inherited from the dispatcher.

Managing

task command
run the dispatcher php artisan queue:on-demand
see what would start, start nothing php artisan queue:on-demand --once --dry-run
groups, ready jobs, running workers php artisan queue:on-demand:status
deploy new code php artisan queue:restart: workers finish their jobs, the dispatcher exits and the process manager starts it again
pause / resume a queue php artisan queue:pause database:reports / php artisan queue:resume database:reports
stop stop the program in your process manager; workers finish the current job

Failed jobs, retries and batches work as usual: queue:failed, queue:retry and the rest.

With the database driver the dispatcher warns about queues that have jobs but belong to no group: such jobs never fail, they just wait.

Drivers

driver ready jobs renewal
database any database the stock database queue supports yes
redis phpredis or predis; use a Redis without eviction (noeviction) for queues yes
sqs each count is an API request: set interval to 10–20 yes, the lease is the connection's retry_after
beanstalkd yes no: keep retry_after above your longest job

Other drivers can be added:

use Diiimonn\QueueOnDemand\Drivers;

app(Drivers::class)->extend(
    'my-driver',
    fn (array $config, string $connection, $app) => new MyReadyCounter($config),
    fn ($job, array $config, int $lease, $app) => new MyRenewal($job, $lease), // optional
);

A counter implements Counters\ReadyCounter, a renewal implements Renewal\Renewal.

Limitations

  • Needs pcntl, so no Windows.
  • Do not run it alongside Horizon or other queue:work processes on the same queues.

License

MIT