qwenode / yii2-queue
支持DB、Redis、RabbitMQ、Beanstalk、SQS和Gearman的Yii2队列扩展
2.4.0
2022-11-09 07:11 UTC
Requires
- php: >=5.5.0
- symfony/process: ^3.3||^4.0||^5.0
- yiisoft/yii2: ~2.0.14
Requires (Dev)
- aws/aws-sdk-php: >=2.4
- enqueue/amqp-lib: ^0.8||^0.9.10
- enqueue/stomp: ^0.8.39
- jeremeamia/superclosure: *
- pda/pheanstalk: v3.*
- php-amqplib/php-amqplib: *
- phpunit/phpunit: ~4.4
- yiisoft/yii2-debug: *
- yiisoft/yii2-gii: *
- yiisoft/yii2-redis: *
Suggests
- ext-gearman: Need for Gearman queue.
- ext-pcntl: Need for process signals.
- aws/aws-sdk-php: Need for aws SQS.
- enqueue/amqp-lib: Need for AMQP interop queue.
- enqueue/stomp: Need for Stomp queue.
- pda/pheanstalk: Need for Beanstalk queue.
- php-amqplib/php-amqplib: Need for AMQP queue.
- yiisoft/yii2-redis: Need for Redis queue.
This package is not auto-updated.
Last update: 2024-09-26 13:55:52 UTC
README
Yii2 Queue Extension
一个用于通过队列异步运行任务的扩展。
它支持基于 DB、Redis、RabbitMQ、AMQP、Beanstalk、ActiveMQ 和 Gearman 的队列。
文档在 docs/guide/README.md。
安装
安装此扩展的首选方法是使用 composer
php composer.phar require --prefer-dist yiisoft/yii2-queue
基本用法
每个发送到队列的任务都应该定义为单独的类。例如,如果您需要下载并保存文件,该类可能如下所示
class DownloadJob extends BaseObject implements \yii\queue\JobInterface { public $url; public $file; public function execute($queue) { file_put_contents($this->file, file_get_contents($this->url)); } }
这是将任务发送到队列的方法
Yii::$app->queue->push(new DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
将作业推入队列,5分钟后运行
Yii::$app->queue->delay(5 * 60)->push(new DownloadJob([ 'url' => 'http://example.com/image.jpg', 'file' => '/tmp/image.jpg', ]));
任务的执行方式取决于所使用的驱动。大多数驱动程序可以通过控制台命令运行,这些命令组件会自动注册到您的应用程序中。
此命令循环获取并执行任务,直到队列为空
yii queue/run
此命令启动一个守护进程,无限期地查询队列
yii queue/listen
有关特定驱动程序控制台命令及其选项的更多详细信息,请参阅文档。
组件还具有跟踪推入队列的作业状态的 Ability。
// Push a job into the queue and get a message ID. $id = Yii::$app->queue->push(new SomeJob()); // Check whether the job is waiting for execution. Yii::$app->queue->isWaiting($id); // Check whether a worker got the job from the queue and executes it. Yii::$app->queue->isReserved($id); // Check whether a worker has executed the job. Yii::$app->queue->isDone($id);
更多详情请参阅 指南。