shipsaas/laravel-priority-queue
Composer 安装命令:
composer require shipsaas/laravel-priority-queue
包简介
Priority Queue implementation for your Laravel Applications
README 文档
README
A simple Priority Queue Driver for your Laravel Applications to serve your priority messages and makes users happy 🔋.
With the famous Repository Pattern of Laravel, Priority Queue Driver is easily get injected into Laravel's Lifecycle without any hassle/hurdle.
We can use built-in artisan command php artisan queue:work 😎.
Supports
- Laravel 11 (supports by default)
- Laravel 10 (supports until Laravel drops the bug fixes at August 6th, 2024)
- PHP 8.2 & 8.3
- Any database that Laravel supported.
Architecture Diagram
Why Priority Queue Driver use Database?
- Everybody knows Database (MySQL, PgSQL, etc) 👀.
- Easy and simple to implement ❤️.
- Utilize the
ORDER BYandINDEXfor fast queue msgs pop process. Faster than any other stuff 🔥. - Highest visibility (you can view the jobs and their data in DB) ⭐️.
- Highest flexibility (you can change the weight directly in DB to unblock important msgs) 💰.
- No extra tool involved. Just Laravel 🥰.
Install Laravel Priority Queue
composer require shipsaas/laravel-priority-queue
One-Time Setup
Export and run the migration (one-time). We don't load migration by default just in case you want to customize the migration schema 😎.
php artisan vendor:publish --tag=priority-queue-migrations php artisan migrate
Open config/queue.php and add this to the connections array:
'connections' => [ // ... a lot of connections above // then our lovely guy here 'database-priority' => [ 'driver' => 'database-priority', 'connection' => 'mysql', 'table' => 'priority_jobs', 'queue' => 'default', 'retry_after' => 90, 'after_commit' => false, // or true, depends on your need ], ],
Scale/Reliability Consideration
It is recommended to use a different database connection (eg mysql_secondary) to avoid the worker processes ramming your
primary database.
Usage
The Job Weight
The default job weight is 500.
You can define a hardcoded weight for your job by using the $jobWeight property.
class SendEmail implements ShouldQueue { public int $jobWeight = 500; }
Or if you want to calculate the job weight on runtime, you can use the UseJobPrioritization trait:
use ShipSaasPriorityQueue\Traits\UseJobPrioritization; class SendEmail implements ShouldQueue { use UseJobPrioritization; public function getJobWeight() : int { return $this->user->isUsingProPlan() ? 1000 : 500; } }
Dispatch the Queue
You can use the normal Dispatcher or Queue Facade,... to dispatch the Queue Msgs
As primary queue
QUEUE_CONNECTION=database-priority
And you're ready to roll.
As secondary queue
Specify the database-priority connection when dispatching a queue msg.
// use Dispatcher SendEmail::dispatch($user, $emailContent) ->onConnection('database-priority'); // use Queue Facade use Illuminate\Support\Facades\Queue; Queue::connection('database-priority') ->push(new SendEmail($user, $emailContent));
I get that you guys might don't want to explicitly put the connection name. Alternatively, you can do this:
class SendEmail implements ShouldQueue { // first option public $connection = 'database-priority'; public function __construct() { // second option $this->onConnection('database-priority'); } }
Run The Queue Worker
Nothing different from the Laravel Documentation 😎. Just need to include the database-priority driver.
php artisan queue:work database-priority
# Extra win, priority on topic
php artisan queue:work database-priority --queue=custom
Testing
Run composer test 😆
Available Tests:
- Unit Testing
- Integration Testing against MySQL and
queue:workcommand
Contributors
- Seth Phat
Contributions & Support the Project
Feel free to submit any PR, please follow PSR-1/PSR-12 coding conventions and unit test is a must.
If this package is helpful, please give it a ⭐️⭐️⭐️. Thank you!
License
MIT License
shipsaas/laravel-priority-queue 适用场景与选型建议
shipsaas/laravel-priority-queue 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 2.84k 次下载、GitHub Stars 达 49, 最近一次更新时间为 2023 年 04 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「laravel」 「laravel queue」 「laravel library」 「laravel priority queue」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 shipsaas/laravel-priority-queue 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 shipsaas/laravel-priority-queue 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 shipsaas/laravel-priority-queue 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
PHP AMQP Binding Library
A Laravel package to monitor queue jobs.
Inbox pattern process implementation for your Laravel Applications
Core library that defines common interfaces used by the rest of the intahwebz..
Amqp classes
Provides command to manage a web library directory
统计信息
- 总下载量: 2.84k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 49
- 点击次数: 13
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2023-04-02