queue icon indicating copy to clipboard operation
queue copied to clipboard

Queue is created with the wrong channel

Open romkatsu opened this issue 4 years ago • 1 comments

What steps will reproduce the problem?

Configure queue


    'yiisoft/yii-queue' => [
        'handlers' => [
            'yii-queue-1' => [new \App\Job\HandlerOne(), 'handle'],
        ],
        'channel-definitions' => [
            'yii-queue-1' => \Yiisoft\Yii\Queue\AMQP\Adapter::class,
        ],
    ],

Push message:

$message = new \Yiisoft\Yii\Queue\Message\Message('yii-queue-1', ['message']);
$this->queueFactory->get('yii-queue-1')->push($message);

What is the expected result?

A queue will be created 'yii-queue-1'

What do you get instead?

A queue has been created with the default name 'yii-queue'

изображение

Additional info

There will be no problem if configured like this:

'channel-definitions' => [
    'yii-queue-1' =>  static fn(\Yiisoft\Yii\Queue\AMQP\Adapter $adapter) => $adapter->withChannel('yii-queue-1'),
],

The problem is in the factory, here: https://github.com/yiisoft/yii-queue/blob/master/src/QueueFactory.php#L89

romkatsu avatar Nov 21 '21 17:11 romkatsu

Hi, @romkatsu. I remember I've talked to you in PM about that. And we didn't fully understand each other that time. Now I'm ready to clarify the rest. In my opinion the only bug is in docs.

  1. Message name
'handlers' => [
    'yii-queue-1' => [new \App\Job\HandlerOne(), 'handle'],
],

In this example you are ready to handle messages with name yii-queue-1, and it doesn't matter, from which channel/queue.

  1. Channel name
'channel-definitions' => [
    'yii-queue-1' => \Yiisoft\Yii\Queue\AMQP\Adapter::class,
],

Here the key yii-queue-1 is just an application-defined channel name. And its value, \Yiisoft\Yii\Queue\AMQP\Adapter::class, is a definition of the channel. You can use any definition supported by yiisoft/definitions. In this particular case you just say "Give me the default instance of Adapter::class from the container". To set a different queue name for the amqp adapter, you can use the $adapter->withChannel() method. You can even use a different adapter here (i.e. for redis, or the synchronous one).

viktorprogger avatar Feb 14 '22 07:02 viktorprogger