diiimonn / laravel-queue-on-demand
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
Requires
- php: ^8.2
- ext-pcntl: *
- ext-posix: *
- illuminate/cache: ^12.0|^13.0
- illuminate/console: ^12.0|^13.0
- illuminate/contracts: ^12.0|^13.0
- illuminate/database: ^12.0|^13.0
- illuminate/queue: ^12.0|^13.0
- illuminate/support: ^12.0|^13.0
Requires (Dev)
- aws/aws-sdk-php: ^3.300
- orchestra/testbench: ^10.0|^11.0
- pda/pheanstalk: ^5.0
- phpunit/phpunit: ^11.5|^12.0
- predis/predis: ^2.0|^3.0
- symfony/process: ^7.0|^8.0
Suggests
- aws/aws-sdk-php: Required for the sqs driver.
- pda/pheanstalk: Required for the beanstalkd driver.
Provides
None
Conflicts
None
Replaces
None
README
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
pcntlandposix(Linux or macOS) - Laravel 12 or 13
- Queue driver:
database,redis,sqsorbeanstalkd
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:workprocesses on the same queues.
License
MIT