manoj-malviya/yii-2-queue

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

安装: 4

依赖: 0

建议者: 0

安全: 0

星标: 0

关注者: 1

分支: 296

类型:yii2-extension

2.3.0 2019-06-04 18:58 UTC

README

Yii2 队列扩展


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

它支持基于数据库RedisRabbitMQAMQPBeanstalkActiveMQGearman的队列。

文档位于docs/guide/README.md

Latest Stable Version Total Downloads Build Status

安装

安装此扩展的首选方式是通过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

有关特定驱动程序控制台命令及其选项的更多详细信息,请参阅文档。

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

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

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