PHPackages                             luoxiaojun1992/yii2-queue - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Queues &amp; Workers](/categories/queues)
4. /
5. luoxiaojun1992/yii2-queue

ActiveLibrary[Queues &amp; Workers](/categories/queues)

luoxiaojun1992/yii2-queue
=========================

Queue component for Yii2

2.0.6(9y ago)114[1 issues](https://github.com/luoxiaojun1992/yii2-queue/issues)MITPHPPHP &gt;=5.5.0

Since Apr 8Pushed 9y ago2 watchersCompare

[ Source](https://github.com/luoxiaojun1992/yii2-queue)[ Packagist](https://packagist.org/packages/luoxiaojun1992/yii2-queue)[ RSS](/packages/luoxiaojun1992-yii2-queue/feed)WikiDiscussions master Synced today

READMEChangelog (4)Dependencies (15)Versions (24)Used By (0)

Queue Component for Yii2
========================

[](#queue-component-for-yii2)

This provides queue component for Yii2.

[![Latest Stable Version](https://camo.githubusercontent.com/eb7da2e76a18bc3a93c3a349a28bb11daa42756bb0c2ef8a37a090df72b9c978/68747470733a2f2f706f7365722e707567782e6f72672f757262616e696e646f2f796969322d71756575652f762f737461626c652e737667)](https://packagist.org/packages/urbanindo/yii2-queue)[![Total Downloads](https://camo.githubusercontent.com/cd89c468108abc5bb604ec2c555e265396f1313310ed08abe872ad25de487ae7/68747470733a2f2f706f7365722e707567782e6f72672f757262616e696e646f2f796969322d71756575652f646f776e6c6f6164732e737667)](https://packagist.org/packages/urbanindo/yii2-queue)[![Latest Unstable Version](https://camo.githubusercontent.com/78b30a06df45675f16097268e2c7b8c7c348ad3843cf4d37f520cb0af0cb8e46/68747470733a2f2f706f7365722e707567782e6f72672f757262616e696e646f2f796969322d71756575652f762f756e737461626c652e737667)](https://packagist.org/packages/urbanindo/yii2-queue)[![Build Status](https://camo.githubusercontent.com/b73f7b1449d27d0848b657d025ecfc247a719e1bf74707fdefe54338d8459703/68747470733a2f2f7472617669732d63692e6f72672f757262616e696e646f2f796969322d71756575652e737667)](https://travis-ci.org/urbanindo/yii2-queue)

Requirements
------------

[](#requirements)

You need [PCNT extension](http://php.net/manual/en/book.pcntl.php) enabled to run listener

Installation
------------

[](#installation)

The preferred way to install this extension is through [composer](http://getcomposer.org/download/).

Either run

```
php composer.phar require --prefer-dist urbanindo/yii2-queue "*"

```

or add

```
"urbanindo/yii2-queue": "*"

```

to the require section of your `composer.json` file.

To use Redis queue or RabbitMQ, you have to add `yiisoft/yii2-redis:*` or `videlalvaro/php-amqplib: 2.5.*` respectively.

Setting Up
----------

[](#setting-up)

After the installation, first step is to set the console controller.

```
return [
    // ...
    'controllerMap' => [
        'queue' => [
            'class' => 'UrbanIndo\Yii2\Queue\Console\Controller',
            //'sleepTimeout' => 1
        ],
    ],
];
```

For the task worker, set a new module, e.g. `task` and declare it in the config.

```
'modules' => [
    'task' => [
        'class' => 'app\modules\task\Module',
    ]
]
```

And then set the queue component. Don't forget to set the module name that runs the task in the component. For example, queue using AWS SQS

```
'components' => [
    'queue' => [
        'class' => 'UrbanIndo\Yii2\Queue\Queues\SqsQueue',
            'module' => 'task',
            'url' => 'https://sqs.ap-southeast-1.amazonaws.com/123456789012/queue',
            'config' => [
                'key' => 'AKIA1234567890123456',
                'secret' => '1234567890123456789012345678901234567890',
                'region' => 'ap-southeast-1',
                'version' => 'latest'
            ]
        ]
    ]
]
```

Or using Database queue

```
'components' => [
    'db' => [
        // the db component
    ],
    'queue' => [
        'class' => 'UrbanIndo\Yii2\Queue\Queues\DbQueue',
        'db' => 'db',
        'tableName' => 'queue',
        'module' => 'task',
    ]
]
```

Usage
-----

[](#usage)

### Creating A Worker

[](#creating-a-worker)

Creating a worker is just the same with creating console or web controller. In the task module create a controller that extends `UrbanIndo\Yii2\Queue\Worker\Controller`

e.g.

```
class FooController extends UrbanIndo\Yii2\Queue\Worker\Controller
{
    public function actionBar($param1, $param2)
    {
        echo $param1;
    }
}
```

To prevent the job got deleted from the queue, for example when the job is not completed, return `false` in the action. The job will be run again the next chance.

e.g.

```
class FooController extends UrbanIndo\Yii2\Queue\Worker\Controller
{
    public function actionBar($param1, $param2)
    {
        try {
            // do some stuff
        } catch (\Exception $ex) {
            \Yii::error('Ouch something just happened');
            return false;
        }
    }
}
```

### Running The Listener

[](#running-the-listener)

To run the listener, run the console that set in the above config. If the controller mapped as `queue` then run.

```
yii queue/listen

```

### Posting A Job

[](#posting-a-job)

To post a job from source code, put something like this.

```
use UrbanIndo\Yii2\Queue\Job;

$route = 'foo/bar';
$data = ['param1' => 'foo', 'param2' => 'bar'];
Yii::$app->queue->post(new Job(['route' => $route, 'data' => $data]));
```

Job can also be posted from the console. The data in the second parameter is in JSON string.

```
yii queue/post 'foo/bar' '{"param1": "foo", "param2": "bar"}'

```

Job can also be posted as anonymous function. Be careful using this.

```
Yii::$app->queue->post(new Job(function() {
    echo 'Hello World!';
}));
```

### Deferred Event

[](#deferred-event)

In this queue, there is a feature called **Deferred Event**. Basically using this feature, we can defer a process executed after a certain event using queue.

To use this, add behavior in a component and implement the defined event handler.

```
    public function behaviors()
    {
        return [
            [
                'class' => \UrbanIndo\Yii2\Queue\Behaviors\DeferredEventBehavior::class,
                'events' => [
                    self::EVENT_AFTER_VALIDATE => 'deferAfterValidate',
                ]
            ]
        ];
    }

    public function deferAfterValidate()
    {
        // do something here
    }
```

**NOTE**Due to reducing the message size, the `$event` object that usually passed when triggered the event will not be passed to the deferred event. Also, the object in which the method invoked is merely a clone object, so it won't have the behavior and the event attached in the original object.

As for `ActiveRecord` class, since the object can not be passed due to limitation of SuperClosure in serializing PDO (I personally think that's bad too), the behavior should use `\UrbanIndo\Yii2\Queue\Behaviors\ActiveRecordDeferredEventBehavior`instead. The difference is in the object in which the deferred event handler invoked.

Since we can not pass the original object, the invoking object will be re-fetched from the table using the primary key. And for the `afterDelete` event, since the respective row is not in the table anymore, the invoking object is a new object whose attributes are assigned from the attributes of the original object.

### Web End Point

[](#web-end-point)

We can use web endpoint to use the queue by adding `\UrbanIndo\Yii2\Queue\Web\Controller`to the controller map.

For example

```
'controllerMap' => [
    'queue' => [
        /* @var $queue UrbanIndo\Yii2\Queue\Web\Controller */
        'class' => 'UrbanIndo\Yii2\Queue\Web\Controller'
    ]
],
```

To post this use

```
curl -XPOST http://example.com/queue/post --data route='test/test' --data data='{"data":"data"}'

```

To limit the access to the controller, we can use `\yii\filters\AccessControl` filter.

For example to filter by IP address, we can use something like this.

```
'controllerMap' => [
    'queue' => [
        /* @var $queue UrbanIndo\Yii2\Queue\Web\Controller */
        'class' => 'UrbanIndo\Yii2\Queue\Web\Controller',
        'as access' => [
            'class' => '\yii\filters\AccessControl',
            'rules' => [
                [
                    'allow' => true,
                    'ips' => [
                        '127.0.0.1'
                    ]
                ]
            ]
        ]
    ]
],
```

Testing
-------

[](#testing)

To run the tests, in the root directory execute below.

```
./vendor/bin/phpunit

```

Road Map
--------

[](#road-map)

- Add more queue provider such as MemCache, IronMQ, RabbitMQ.

###  Health Score

25

—

LowBetter than 35% of packages

Maintenance0

Infrequent updates — may be unmaintained

Popularity7

Limited adoption so far

Community16

Small or concentrated contributor base

Maturity68

Established project with proven stability

 Bus Factor1

Top contributor holds 60.8% of commits — single point of failure

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

###  Release Activity

Cadence

Every ~44 days

Recently: every ~12 days

Total

17

Last Release

3394d ago

Major Versions

1.3.1 → 2.0.02017-01-22

### Community

Maintainers

![](https://www.gravatar.com/avatar/3a9a7ab17addb5818306fe7052bf2fa4c5028284ab1e6c91c26894cf645cbf5f?d=identicon)[luoxiaojun1992](/maintainers/luoxiaojun1992)

---

Top Contributors

[![petrabarus](https://avatars.githubusercontent.com/u/523289?v=4)](https://github.com/petrabarus "petrabarus (79 commits)")[![luoxiaojun1992](https://avatars.githubusercontent.com/u/6905700?v=4)](https://github.com/luoxiaojun1992 "luoxiaojun1992 (32 commits)")[![dieend](https://avatars.githubusercontent.com/u/1404443?v=4)](https://github.com/dieend "dieend (8 commits)")[![deerawan](https://avatars.githubusercontent.com/u/1243921?v=4)](https://github.com/deerawan "deerawan (3 commits)")[![sadovnik](https://avatars.githubusercontent.com/u/5960795?v=4)](https://github.com/sadovnik "sadovnik (2 commits)")[![restyler](https://avatars.githubusercontent.com/u/775507?v=4)](https://github.com/restyler "restyler (1 commits)")[![jafaripur](https://avatars.githubusercontent.com/u/5066430?v=4)](https://github.com/jafaripur "jafaripur (1 commits)")[![armengabrielyan](https://avatars.githubusercontent.com/u/5264868?v=4)](https://github.com/armengabrielyan "armengabrielyan (1 commits)")[![desenvolvedorindie](https://avatars.githubusercontent.com/u/1456279?v=4)](https://github.com/desenvolvedorindie "desenvolvedorindie (1 commits)")[![ahmadpriatama](https://avatars.githubusercontent.com/u/303305?v=4)](https://github.com/ahmadpriatama "ahmadpriatama (1 commits)")[![kyle-mccarthy](https://avatars.githubusercontent.com/u/7758522?v=4)](https://github.com/kyle-mccarthy "kyle-mccarthy (1 commits)")

###  Code Quality

TestsPHPUnit

### Embed Badge

![Health badge](/badges/luoxiaojun1992-yii2-queue/health.svg)

```
[![Health](https://phpackages.com/badges/luoxiaojun1992-yii2-queue/health.svg)](https://phpackages.com/packages/luoxiaojun1992-yii2-queue)
```

###  Alternatives

[magento/community-edition

Magento 2 (Open Source)

12.1k53.0k12](/packages/magento-community-edition)[matomo/matomo

Matomo is the leading Free/Libre open analytics platform

21.6k38.2k](/packages/matomo-matomo)[wheelpros/fitment-platform-api

Magento 2 (Open Source)

12.1k1.2k](/packages/wheelpros-fitment-platform-api)[pressbooks/pressbooks

Pressbooks is an open source book publishing tool built on a WordPress multisite platform. Pressbooks outputs books in multiple formats, including PDF, EPUB, web, and a variety of XML flavours, using a theming/templating system, driven by CSS.

45344.0k1](/packages/pressbooks-pressbooks)[keboola/storage-api-client

Keboola Storage API PHP Client

10397.4k31](/packages/keboola-storage-api-client)[sproutcms/cms

Enterprise content management and framework

242.2k4](/packages/sproutcms-cms)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
