PHPackages                             urbanindo/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. urbanindo/yii2-queue

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

urbanindo/yii2-queue
====================

Queue component for Yii2

3.0.0(7y ago)11449.9k—5.6%47[2 PRs](https://github.com/urbanindo/yii2-queue/pulls)MITPHPPHP ^7.2

Since Apr 8Pushed 7y ago15 watchersCompare

[ Source](https://github.com/urbanindo/yii2-queue)[ Packagist](https://packagist.org/packages/urbanindo/yii2-queue)[ RSS](/packages/urbanindo-yii2-queue/feed)WikiDiscussions master Synced 1mo ago

READMEChangelog (10)Dependencies (13)Versions (20)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)[![codecov](https://camo.githubusercontent.com/00c625c5bc97dbd8f75be5cd579981df5e9f632b383c5eba1fb0ee35fcf781f5/68747470733a2f2f636f6465636f762e696f2f67682f757262616e696e646f2f796969322d71756575652f6272616e63682f6d61737465722f67726170682f62616467652e737667)](https://codecov.io/gh/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' => [
                'credentials' => [
                    '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',
        // sleep for 10 seconds if there's no item in the queue (to save CPU)
        'waitSecondsIfNoQueue' => 10,
    ]
]
```

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

43

—

FairBetter than 90% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity45

Moderate usage in the ecosystem

Community29

Small or concentrated contributor base

Maturity67

Established project with proven stability

 Bus Factor1

Top contributor holds 75.5% 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 ~103 days

Recently: every ~118 days

Total

14

Last Release

2699d ago

Major Versions

1.3.1 → 2.0.02017-09-24

2.1.0 → 3.0.02018-12-18

PHP version history (2 changes)1.0.0PHP &gt;=5.5.0

3.0.0PHP ^7.2

### Community

Maintainers

![](https://www.gravatar.com/avatar/1b7f55dc6d59ee54e7f73ffc96ab51bac7e1e328894ad74998dc55df0df8433c?d=identicon)[urbanindo](/maintainers/urbanindo)

![](https://www.gravatar.com/avatar/c1261f29f95a5ca228a04c8415431baf0fb6c397ec83e93e3454fb0b55718c5e?d=identicon)[petrabarus](/maintainers/petrabarus)

---

Top Contributors

[![petrabarus](https://avatars.githubusercontent.com/u/523289?v=4)](https://github.com/petrabarus "petrabarus (83 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)")[![TonisOrmisson](https://avatars.githubusercontent.com/u/6357451?v=4)](https://github.com/TonisOrmisson "TonisOrmisson (2 commits)")[![jafaripur](https://avatars.githubusercontent.com/u/5066430?v=4)](https://github.com/jafaripur "jafaripur (1 commits)")[![ahmadpriatama](https://avatars.githubusercontent.com/u/303305?v=4)](https://github.com/ahmadpriatama "ahmadpriatama (1 commits)")[![madeadi](https://avatars.githubusercontent.com/u/478268?v=4)](https://github.com/madeadi "madeadi (1 commits)")[![marekpetras](https://avatars.githubusercontent.com/u/8991813?v=4)](https://github.com/marekpetras "marekpetras (1 commits)")[![odannyc](https://avatars.githubusercontent.com/u/12864641?v=4)](https://github.com/odannyc "odannyc (1 commits)")[![padlyuck](https://avatars.githubusercontent.com/u/630610?v=4)](https://github.com/padlyuck "padlyuck (1 commits)")[![restyler](https://avatars.githubusercontent.com/u/775507?v=4)](https://github.com/restyler "restyler (1 commits)")[![kyle-mccarthy](https://avatars.githubusercontent.com/u/7758522?v=4)](https://github.com/kyle-mccarthy "kyle-mccarthy (1 commits)")[![armengabrielyan](https://avatars.githubusercontent.com/u/5264868?v=4)](https://github.com/armengabrielyan "armengabrielyan (1 commits)")[![deniskoronets](https://avatars.githubusercontent.com/u/5627329?v=4)](https://github.com/deniskoronets "deniskoronets (1 commits)")[![desenvolvedorindie](https://avatars.githubusercontent.com/u/1456279?v=4)](https://github.com/desenvolvedorindie "desenvolvedorindie (1 commits)")[![fschindler](https://avatars.githubusercontent.com/u/5937990?v=4)](https://github.com/fschindler "fschindler (1 commits)")

###  Code Quality

TestsPHPUnit

Code StylePHP\_CodeSniffer

### Embed Badge

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

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

###  Alternatives

[illuminate/queue

The Illuminate Queue package.

20331.4M1.2k](/packages/illuminate-queue)[jolicode/castor

A lightweight and modern task runner. Automate everything. In PHP.

53541.0k3](/packages/jolicode-castor)[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.

44643.1k1](/packages/pressbooks-pressbooks)[palpalani/laravel-sqs-queue-json-reader

Custom SQS queue reader for Laravel

26109.8k](/packages/palpalani-laravel-sqs-queue-json-reader)[hyperf/xxl-job-incubator

php hyperf xxljob

4632.2k5](/packages/hyperf-xxl-job-incubator)[yiicod/yii2-jobqueue

Yii Job Queue based on Illuminate Queue

107.3k](/packages/yiicod-yii2-jobqueue)

PHPackages © 2026

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