wayhood/yii2-queue

yii2 queue component

Installs: 21 275

Dependents: 0

Suggesters: 0

Security: 0

Stars: 19

Watchers: 6

Forks: 12

Open Issues: 3

Type:yii2-extension

dev-master 2016-01-03 15:17 UTC

This package is auto-updated.

Last update: 2024-03-07 16:02:36 UTC


README

This component provides simple queue wrapper

I recommend yii2-asynctask.

Requirements

Redis yii2-redis

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

"wayhood/yii2-queue": "*"

to the require section of your composer.json file.

Usage

To use this extension, simply add the following code in your application configuration:

return [
    //....
    'components' => [
        'queue' => [
            'class' => 'wh\queue\RedisQueue',
        ],
        'redis' => [
            'class' => 'yii\redis\Connection',
            'hostname' => 'localhost',
            'port' => 6379,
            'database' => 0
        ],
    ],
];

First create a Job process class

namespace console\jobs;

class MyJob
{
    public function run($job, $data)
    {
        //process $data;
        var_dump($data);
    }
} 

and than just push job to queue

// Push job to the default queue and execute "run" method
Yii::$app->queue->push('\console\jobs\MyJob', ['a', 'b', 'c']);

// or push it and execute any other method
Yii::$app->queue->push('\console\jobs\MyJob@myMethod', ['a', 'b', 'c']);

// or push it to some specific queue
Yii::$app->queue->push('\console\jobs\MyJob', ['a', 'b', 'c'], 'myQueue');

// or both
Yii::$app->queue->push('\console\jobs\MyJob@myMethod', ['a', 'b', 'c'], 'myQueue');

Map console controller in your app config

return [
    ...
    'controllerMap' => [
        'queue' => 'wh\queue\console\controllers\QueueController'
    ],
    ...
];

Examples of using queue controller:

# Process a job from default queue and than exit the process
./yii queue/work

# continuously process jobs from default queue
./yii queue/listen

# process a job from specific queue and than exit the process
./yii queue/work myQueue

# continuously process jobs from specific queue
./yii queue/listen myQueue