laravel-batch-dispatcher maintained by timmylindh
laravel-batch-dispatcher
Batch queued jobs and queued event listeners into a single queued job. This reduces the number of queue requests by capturing multiple dispatches and sending them as one job that processes all items.
Installation
Requires:
- Laravel >= 10
- PHP >= 8.1
You can install the package via composer:
composer require timmylindh/laravel-batch-dispatcher
You can publish the config file with:
php artisan vendor:publish --tag="laravel-batch-dispatcher-config"
Usage
Behavior
- All calls to
dispatch(),SomeJob::dispatch(), andEvent::dispatch()will be buffered during the request. - On terminate, the package queues a single wrapper job which in turn dispatches all buffered jobs and queued listeners.
Configuration
You can publish the config file with:
php artisan vendor:publish --tag="laravel-batch-dispatcher-config"
The batching behavior is controlled by config/batch-dispatcher.php:
return [
"enabled" => env("BATCH_DISPATCHER_ENABLED", true),
/**
* In testing, avoid serializing jobs and run the wrapper immediately for assertions
*/
"synchronous_testing" => env(
"BATCH_DISPATCHER_SYNC_TESTING",
env("APP_ENV") === "testing"
),
/**
* Maximum number of buffered items (jobs + queued listeners)
* per wrapper job. When exceeded, multiple wrapper jobs will be dispatched.
*/
"max_batch_size" => env("BATCH_DISPATCHER_MAX_SIZE", 10),
/**
* Enable the middleware to batch the requests.
* Otherwise you will have to manually wrap the routes in the middleware.
*/
"enable_middleware" => env("BATCH_DISPATCHER_ENABLE_MIDDLEWARE", true),
];
Middleware
Setting enable_middleware = true will automatically apply the batching to the api and web middleware groups. You can apply the batching to specific routes or groups by adding the BatchRequests middleware.
Notes
- Only instances of jobs implementing
ShouldQueueand queued event listeners are batched. - Per-job queue options (connection/queue/delay) are respected when listeners are enqueued by the wrapper. Jobs are dispatched as usual by the wrapper.
How it works
During the request, we intercept:
- Bus dispatches of
ShouldQueuejobs and store the job instances in memory - Event dispatches with queued listeners and capture their queued calls
On terminate, a single ProcessBatch job is queued. It then dispatches the buffered jobs and enqueues/invokes listeners.
Testing
composer test
Credits
License
The MIT License (MIT). Please see License File for more information.