T
T
ta42017-09-05 12:26:17
Yii
ta4, 2017-09-05 12:26:17

How to create a queue and run a worker dynamically in yii2-queue?

The yii2-queue component allows you to create queues using a config like this:

return [
    'bootstrap' => [
        'queue', // The component registers own console commands
    ],
    'components' => [
        'queue' => [
            'class' => \yii\queue\redis\Queue::class,
            'channel' => 'my-queue',
        ],
    ],
];

And then run for the listener queue:
php yii queue/listen
But now I have to create queues dynamically:
$queue = new yii\queue\redis\Queue([
      'channel' => 'channel_group_' . $i,
]);

To start the listener for this queue, you need to do: I do not quite understand how to start this listener using the console command (in the background)? Those. let's say in the project in one of the controllers we create several queue objects, add tasks to them, but how to run listeners in the background?
$queue->listen(5);

Answer the question

In order to leave comments, you need to log in

1 answer(s)
S
sehav, 2018-04-18
@sehav

My solution:
Derived from redisCommand

use yii\queue\redis\Command;

class RedisCommand extends Command
{
    public function actionRun($channel = null)
    {
        if ($channel !== null) {
            $this->queue->channel = $channel;
        }

        return $this->queue->run(false);
    }
}

Connected it in the console config
'queue' => [
            'class' => \yii\queue\redis\Queue::class,
            'commandClass' => \app\commands\RedisCommand::class,
            'redis' => 'redis',
            'channel' => 'default', // Queue channel key
            'as log' => \yii\queue\LogBehavior::class,
        ]

In the console one-time
For listen, similarly override the actionListen method, only the channel as the second parameter
The command will look like
yii queue/run 3 channel_group_1

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question