Skip to content
Queue extension for Yii 3.0
Branch: master
Clone or download
Fetching latest commit…
Cannot retrieve the latest commit at this time.
Permalink
Type Name Latest commit message Commit time
Failed to load latest commit information.
.github Create FUNDING.yml May 23, 2019
config Update namespace, remove drivers covered by interop (#14) Apr 30, 2019
docs Update yiisoft/db namespace May 5, 2019
src Fixed `Inflector` usage after it was moved into strings package May 7, 2019
tests Update yiisoft/db namespace May 5, 2019
.dockerignore Test environment Jun 10, 2018
.editorconfig Test environment Jun 10, 2018
.gitattributes Improved test environment Sep 5, 2018
.gitignore Improved test environment Sep 5, 2018
.php_cs CS fix Apr 27, 2018
.styleci.yml Fix some namespaces, add requirements (#12) Apr 17, 2019
.travis.yml Update namespace, remove drivers covered by interop (#14) Apr 30, 2019
CHANGELOG.md Update namespace, remove drivers covered by interop (#14) Apr 30, 2019
LICENSE.md Removed redundant line from license [skip ci] Apr 26, 2018
Makefile Improved test environment Sep 5, 2018
README.md
UPGRADE.md Update namespace, remove drivers covered by interop (#14) Apr 30, 2019
composer.json Fixed `Inflector` usage after it was moved into strings package May 7, 2019
phpunit.xml.dist Update namespace, remove drivers covered by interop (#14) Apr 30, 2019

README.md

Yii Queue Extension


An extension for running tasks asynchronously via queues.

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

Documentation is at docs/guide/README.md.

Latest Stable Version Total Downloads Build Status

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"yiisoft/yii-queue": "~3.0"

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 BaseObject implements \Yiisoft\Yii\Queue\JobInterface
{
    public $url;
    public $file;
    
    public function __construct(string $url, string $file)
    {
        $this->url = $url;
        $this->file = $file;
    }
    
    public function execute($queue)
    {
        file_put_contents($this->file, file_get_contents($this->url));
    }
}

Here's how to send a task into the queue:

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

To push a job into the queue that should run after 5 minutes:

Yii::$app->queue->delay(5 * 60)->push(
    new DownloadJob('http://example.com/image.jpg', '/tmp/image.jpg')
);

The exact way a task is executed depends on the used driver. Most drivers can be run using console commands, which the component automatically registers in your application.

This command obtains and executes tasks in a loop until the queue is empty:

yii queue/run

This command launches a daemon which infinitely queries the queue:

yii queue/listen

See the documentation for more details about driver specific console commands and their options.

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

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

For more details see the guide.

You can’t perform that action at this time.