mice-tm/yii2-queue

基于yii2-queue的包,包含一些改进

安装: 5,065

依赖: 0

建议: 0

安全: 0

星标: 0

关注者: 0

分支: 2

开放问题: 0

类型:yii2-extension

0.1.0 2017-12-04 07:48 UTC

This package is not auto-updated.

Last update: 2024-09-19 13:55:59 UTC


README

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

它支持基于DBRedisRabbitMQBeanstalkGearman的队列。

为AMQP添加了延迟队列支持,并为队列添加了健康检查

https://github.com/yiisoft/yii2-queue的分支

安装

安装此扩展的首选方式是通过composer

更新composer.json

  1. 将以下内容添加到你的composer.json文件的require部分:
"mice-tm/yii2-queue": "dev-master"
  1. 然后添加一个repositories部分到你的composer.json文件。
{
  "type": "vcs",
  "url": "https://github.com/mice-tm/yii2-queue"
}

基本用法

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

class DownloadJob extends Object implements \yii\queue\Job
{
    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 massage 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);