php 队列扩展,Yii2 队列扩展:用于通过队列异步运行任务的扩展

Yii2 Queue Extension

用于通过队列异步运行任务的一个扩展。An extension for running tasks asyncronously via queues.

It supported queues based on DB , Redis , RabbitMQ , Beanstalk and Gearman .

Documentation is at docs/guide/README.md .

Installation

The preferred way to install this extension is through composer .

Either run

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

or add

"zhuravljov/yii2-queue": "*"

to the require section of your composer.json file.

Basic Usage

Each task which is sent to queue should be defined as a separate class. For example, if you need to download and save a file the class may look like the following:

class DownloadJob extends Object implements \zhuravljov\yii\queue\Job

{

public $url;

public $file;

public function execute($queue)

{

file_put_contents($this->file, file_get_contents($this->url));

}

}

Here's how to send a task into queue:

Yii::$app->queue->push(new DownloadJob([

'url' => 'http://example.com/image.jpg',

'file' => '/tmp/image.jpg',

]));

Pushes job into queue that run after 5 min:

Yii::$app->queue->later(new DownloadJob([

'url' => 'http://example.com/image.jpg',

'file' => '/tmp/image.jpg',

]), 5 * 60);

The exact way task is executed depends on the driver used. The most part of drivers can be run using console commands, which the component registers in your application. For more details see documentation of a driver.

The component has ability to track status of a job which was pushed into queue.

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

For more details see the guide .

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值