定制 timmylindh/laravel-batch-dispatcher 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

timmylindh/laravel-batch-dispatcher

最新稳定版本:v0.0.2

Composer 安装命令:

composer require timmylindh/laravel-batch-dispatcher

包简介

Batch Laravel queued jobs and queued event listeners into a single queued job dispatched at the end of the request.

README 文档

README

Latest Version on Packagist GitHub Tests Action Status GitHub Code Style Action Status Total Downloads

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(), and Event::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 ShouldQueue and 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 ShouldQueue jobs 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.

统计信息

  • 总下载量: 20
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 1
  • 点击次数: 1
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 1
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2025-08-30