Skip to content

Yii2 Queue Extension. Supports DB, Redis, RabbitMQ, Beanstalk and Gearman

License

Notifications You must be signed in to change notification settings

coderltf/yii2-queue

This branch is 1 commit ahead of, 9 commits behind yiisoft/yii2-queue:refs/heads/master.

Folders and files

NameName
Last commit message
Last commit date

Latest commit

0704bae · Apr 24, 2024
Sep 18, 2023
Apr 24, 2024
Feb 1, 2024
Feb 1, 2024
May 8, 2019
Jun 10, 2018
May 8, 2019
May 8, 2019
May 8, 2019
Mar 4, 2023
Feb 1, 2024
Apr 26, 2018
Sep 18, 2023
Feb 21, 2023
Oct 2, 2023
Jan 5, 2024
Jun 10, 2018

Repository files navigation

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.

About

Yii2 Queue Extension. Supports DB, Redis, RabbitMQ, Beanstalk and Gearman

Resources

License

Security policy

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages

  • PHP 95.2%
  • Dockerfile 2.4%
  • Shell 1.9%
  • Makefile 0.5%