adamczykpiotr/laravel-balanced-queues
Composer 安装命令:
composer require adamczykpiotr/laravel-balanced-queues
包简介
Laravel package designed to optimize the performance and efficiency of your application's queue usage
README 文档
README
Stop guessing how many queue workers you need. This package automatically optimizes worker counts based on job type (CPU-intensive, API calls, file downloads) and your server's resources.
Why You Need This
Running 10 workers for everything? You're wasting resources or creating bottlenecks.
- Image downloads saturate your network with too many workers
- API calls queue up with too few concurrent workers
- CPU jobs leave cores idle or cause context-switching overhead
Are you running your application on different machines? Different CPU core configurations, changing network bandwidth? With this package you can optimize it automatically and fine tune it for best results.
Quick Start
composer require adamczykpiotr/laravel-balanced-queues
php artisan vendor:publish --tag="balanced-queues-config"
Tag your jobs with the right workload type using traits:
use AdamczykPiotr\LaravelBalancedQueues\Traits\HasBalancedQueues; class ScrapeVideosJob implements ShouldQueue { public function __construct() { $this->onHighNetworkBandwidthUsageQueu(); } } class CallApiJob implements ShouldQueue { public function __construct() { $this->onHighNetworkRequestUsageQueue(); } } class ProcessBigDataJob implements ShouldQueue { public function __construct() { $this->onHighCpuUsageQueue(); } } class ProcessSmallerDataJob implements ShouldQueue { public function __construct() { $this->onMediumCpuUsageQueue(); } }
Run your queues:
php artisan queue:run-balanced
Done. The package spawns all the queues with adjusted worker counts for each job type.
Available helpers
| Helper method | Workload Type | Use Case |
|---|---|---|
onHighCpuUsageQueue |
CPU_HIGH | Large file processing ~MB/GB |
onMediumCpuUsageQueue |
CPU_MEDIUM | PDF generation, smaller file parsing (~KB/MB) |
onLowCpuUsageQueue |
CPU_LOW | Quick background queries |
onHighNetworkRequestUsageQueue |
NETWORK_HIGH_BANDWIDTH | Large file downloads (~ >10MB) |
onHighNetworkBandwidthUsageQueue |
NETWORK_HIGH_REQUESTS | API calls, webhooks |
onFilesystemQueue |
FILESYSTEM | Filesystem operations (duh!) |
How It Works
By default, CPU_HIGH usage assumes 100-50% CPU utilisation for a single job, ~50-25% for CPU_MEDIUM and ~10-20% for CPU_LOW.
Jobs on NETWORK_HIGH_BANDWIDTH are best suited for downloading large files that completely saturate network bandwidth
whereas NETWORK_HIGH_REQUESTS are in most cases are bottleneck by neither network nor cpu and are offloaded on a queue
simply to improve UX. FILESYSTEM jobs are assumed to be bottlenecked by disk I/O.
If the default configuration doesn't fully utilize your machine, adjust the following default configuration in
config/balanced-queues.php:
use AdamczykPiotr\LaravelBalancedQueues\Enums\JobWorkloadType; $CPU_CORES = CpuCoreConfigurationResolver::CPU_CORES; return [ 'queues' => [ JobWorkloadType::DEFAULT->value => 1, JobWorkloadType::CPU_HIGH->value => $CPU_CORES, JobWorkloadType::CPU_MEDIUM->value => 2 * $CPU_CORES, JobWorkloadType::CPU_LOW->value => 4 * $CPU_CORES, JobWorkloadType::NETWORK_HIGH_BANDWIDTH->value => 5, JobWorkloadType::NETWORK_HIGH_REQUESTS->value => 50, JobWorkloadType::FILESYSTEM->value => 2, ], ];
The default configuration aims to ensure best results across wide range of cases. In order to get full benefits of this approach, it's highly recommended to fine-tune the configuration to your specific use-case. The clue to optimisation is to ensure a single queue can fully saturate the machine:
- Dispatching
CPU_CORESjobs toCPU_HIGHqueue should result in ~100% CPU usage - Dispatching
2*CPU_CORESjobs toCPU_MEDIUMqueue should result in ~100% CPU usage - Dispatching
4*CPU_CORESjobs toCPU_LOWqueue should result in ~100% CPU usage - Dispatching
5jobs toNETWORK_HIGH_BANDWIDTHqueue should result in full network saturation - Dispatching
50jobs toNETWORK_HIGH_REQUESTSshould result in jobs being finished as soon as possible (no real bottleneck) - Dispatching
2jobs toFILESYSTEMqueue should result in full disk I/O saturation
In case more than one queue is fully saturated, OS scheduler will balance processes CPU time accordingly.
Advanced Configuration
Custom Queue Names
'queues' => [ 'ml-training' => 2 * $CPU_CORES, 'notifications' => 10, 'web-scraping' => 25, ],
VM / Docker CPU Limits
Override auto-detected CPU core count:
'package' => [ 'cpu_core_count' => 4, // Override auto-detected core count ],
Worker Options
Add additional options to artisan queue:work command:
'worker_options' => [ '--timeout' => 60, '--tries' => 3, '--sleep' => 3, ],
Supervisor Configuration
Customize the generated Supervisor configuration:
'supervisor' => [ 'header' => [ 'user' => 'www-data', 'numprocs' => 1, ], ],
Commands
# Run queue workers php artisan queue:run-balanced # Run in background php artisan queue:run-balanced --background # Get path to generated config and run supervisor manually php artisan queue:run-balanced --path-only
Docker container entrypoint example
#!/bin/sh
set -e
SUPERVISORD_CONFIG_PATH=$(php artisan queue:run-balanced --path-only)
exec /usr/bin/supervisord -n -c "$SUPERVISORD_CONFIG_PATH"
Requirements
- PHP 8.3+
- Laravel 11.x or 12.x
- Supervisor
Testing
composer test
Changelog
Please see CHANGELOG for more information on what has changed recently.
Contributing
Please see CONTRIBUTING for details.
Security Vulnerabilities
Please review our security policy on how to report security vulnerabilities.
Credits
License
The MIT License (MIT). Please see License File for more information.
adamczykpiotr/laravel-balanced-queues 适用场景与选型建议
adamczykpiotr/laravel-balanced-queues 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 268 次下载、GitHub Stars 达 1, 最近一次更新时间为 2025 年 10 月 12 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「adamczykpiotr」 「laravel-balanced-queues」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 adamczykpiotr/laravel-balanced-queues 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 adamczykpiotr/laravel-balanced-queues 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 adamczykpiotr/laravel-balanced-queues 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
This is my package dag-workflows
Alfabank REST API integration
Laravel package for Accurate Online API integration.
Shared RCX Laravel DataTables UI and configuration helpers.
Boot a Laravel project on any machine with one command: app:serve installs missing tools (PHP, Node, Composer, Herd, Docker), creates .env, sets up the database, runs migrations, builds assets, starts a queue worker and serves via Herd, Sail or artisan serve; app:down cleanly stops everything it sta
Branded, diagnostic error pages (500, 403, 404, 419, 503) for Filament — native Filament UI, dark mode and translations out of the box.
统计信息
- 总下载量: 268
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 1
- 点击次数: 18
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2025-10-12