joshuakevin/yii2-queue

支持数据库、Redis、RabbitMQ、Beanstalk和Gearman的Yii2队列扩展

安装量8,269

依赖关系: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

类型:yii2-extension

1.0.1 2017-12-18 09:11 UTC

This package is not auto-updated.

Last update: 2024-09-29 05:55:14 UTC


README

Yii2队列扩展


通过队列异步执行任务的扩展。

它支持基于 DBRedisRabbitMQBeanstalkGearman 的队列。

文档位于 docs/guide/README.md

Latest Stable Version Total Downloads Build Status

安装

安装此扩展的首选方法是使用 composer

运行以下命令之一

php composer.phar require --prefer-dist yiisoft/yii2-queue

或者在您的 composer.json 文件的 require 部分添加以下内容:

"yiisoft/yii2-queue": "~2.0.0"

基本用法

发送到队列的每个任务都应该定义为单独的类。例如,如果您需要下载并保存文件,该类的形式可能如下所示

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 queue and get message ID.
$id = Yii::$app->queue->push(new SomeJob());

// The job is waiting for execute.
Yii::$app->queue->isWaiting($id);

// Worker gets the job from queue, and executing it.
Yii::$app->queue->isReserved($id);

// Worker has executed the job.
Yii::$app->queue->isDone($id);

有关更多详细信息,请参阅 指南