Yii2 Queue Extension


An extension for running tasks asynchronously via queues.

It supports queues based on DB, Redis, RabbitMQ, AMQP, Beanstalk, ActiveMQ 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:

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

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 \yii\queue\JobInterface
{
    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 the queue:

Yii::$app->queue->push(new DownloadJob([
    'url' => 'http://example.com/image.jpg',
    'file' => '/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([
    'url' => 'http://example.com/image.jpg',
    'file' => '/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.

Class Reference

Class Description
yii\queue\ErrorEvent Error Event.
yii\queue\ExecEvent Exec Event.
yii\queue\InvalidJobException Invalid Job Exception.
yii\queue\Job Job Interface.
yii\queue\JobEvent Job Event.
yii\queue\JobInterface Job Interface.
yii\queue\LogBehavior Log Behavior.
yii\queue\PushEvent Push Event.
yii\queue\Queue Base Queue.
yii\queue\RetryableJob Retryable Job Interface.
yii\queue\RetryableJobInterface Retryable Job Interface.
yii\queue\amqp\Command Manages application amqp-queue.
yii\queue\amqp\Queue Amqp Queue.
yii\queue\amqp_interop\Command Manages application amqp-queue.
yii\queue\amqp_interop\Queue Amqp Queue.
yii\queue\beanstalk\Command Manages application beanstalk-queue.
yii\queue\beanstalk\InfoAction Info about queue status.
yii\queue\beanstalk\Queue Beanstalk Queue.
yii\queue\cli\Action Base Command Action.
yii\queue\cli\Command Base Command.
yii\queue\cli\LoopInterface Loop Interface.
yii\queue\cli\Queue Queue with CLI.
yii\queue\cli\Signal Process Signal Helper.
yii\queue\cli\SignalLoop Signal Loop.
yii\queue\cli\Verbose Verbose Behavior.
yii\queue\cli\VerboseBehavior Verbose Behavior.
yii\queue\cli\WorkerEvent Worker Event.
yii\queue\closure\Behavior Closure Behavior.
yii\queue\closure\Job Closure Job.
yii\queue\db\Command Manages application db-queue.
yii\queue\db\InfoAction Info about queue status.
yii\queue\db\Queue Db Queue.
yii\queue\db\migrations\M161119140200Queue Example of migration for queue message storage.
yii\queue\db\migrations\M170307170300Later Example of migration for queue message storage.
yii\queue\db\migrations\M170509001400Retry Example of migration for queue message storage.
yii\queue\db\migrations\M170601155600Priority Example of migration for queue message storage.
yii\queue\db\migrations\M211218163000JobQueueSize Example of migration for queue message storage.
yii\queue\debug\Panel Debug Panel.
yii\queue\file\Command Manages application file-queue.
yii\queue\file\InfoAction Info about queue status.
yii\queue\file\Queue File Queue.
yii\queue\gearman\Command Manages application gearman-queue.
yii\queue\gearman\Queue Gearman Queue.
yii\queue\gii\Generator This generator will generate a job.
yii\queue\redis\Command Manages application redis-queue.
yii\queue\redis\InfoAction Info about queue status.
yii\queue\redis\Queue Redis Queue.
yii\queue\serializers\IgbinarySerializer Igbinary Serializer.
yii\queue\serializers\JsonSerializer Json Serializer.
yii\queue\serializers\PhpSerializer Php Serializer.
yii\queue\serializers\Serializer Interface Serializer.
yii\queue\serializers\SerializerInterface Serializer Interface.
yii\queue\sqs\Command Manages application aws sqs-queue.
yii\queue\sqs\Queue SQS Queue.
yii\queue\stomp\Command Manages application stomp-queue.
yii\queue\stomp\Queue Stomp Queue.
yii\queue\sync\Queue Sync Queue.