xyqweb/yii2-queue

支持 RabbitMQ 的 Yii2 队列扩展

安装: 271

依赖: 0

建议者: 0

安全: 0

星标: 1

关注者: 1

分支: 1

类型:yii2-extension

1.9.5 2022-12-02 01:27 UTC

This package is auto-updated.

Last update: 2024-09-23 09:47:14 UTC


README

Yii2 队列扩展


一个用于通过队列异步执行任务的扩展。

它支持基于 DBRedisRabbitMQAMQPBeanstalkGearman 的队列。

文档位于 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));
    }
}

要推送一个将在 5 分钟后运行的作业到队列中

Yii::$app->queue->push(new DownloadJob([
    'url' => 'http://example.com/image.jpg',
    'file' => '/tmp/image.jpg',
]));

任务的执行方式取决于所使用的驱动。大多数驱动可以使用控制台命令运行,这些命令组件会自动注册到您的应用程序中。

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

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

组件还具有跟踪推送到队列的作业状态的能力。

// 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);

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