Add RabbitMQConsumerTotalProxy to lazy RabbitMQ services for messages

This is just a simple proxy because we can't lazy load RabbitMQ service just to count number of messages in the queue.
As they are automatically injected in the controller now, we can't lazy load them.

Also forgot to use `AbstractController` in previous PR about _controller as a service_.
This commit is contained in:
Jeremy Benoist
2022-12-19 13:23:56 +01:00
parent c9edd15bc5
commit 0a6e6abdc4
12 changed files with 152 additions and 61 deletions

View File

@ -0,0 +1,92 @@
<?php
namespace Wallabag\ImportBundle\Consumer;
use OldSound\RabbitMqBundle\RabbitMq\Consumer;
/**
* A proxy class only used to count messages in a queue while lazy loading RabbitMQ services.
* Only used in ImportController.
*/
class RabbitMQConsumerTotalProxy
{
private Consumer $pocketConsumer;
private Consumer $readabilityConsumer;
private Consumer $wallabagV1Consumer;
private Consumer $wallabagV2Consumer;
private Consumer $firefoxConsumer;
private Consumer $chromeConsumer;
private Consumer $instapaperConsumer;
private Consumer $pinboardConsumer;
private Consumer $deliciousConsumer;
private Consumer $elcuratorConsumer;
public function __construct(Consumer $pocketConsumer, Consumer $readabilityConsumer, Consumer $wallabagV1Consumer, Consumer $wallabagV2Consumer, Consumer $firefoxConsumer, Consumer $chromeConsumer, Consumer $instapaperConsumer, Consumer $pinboardConsumer, Consumer $deliciousConsumer, Consumer $elcuratorConsumer)
{
$this->pocketConsumer = $pocketConsumer;
$this->readabilityConsumer = $readabilityConsumer;
$this->wallabagV1Consumer = $wallabagV1Consumer;
$this->wallabagV2Consumer = $wallabagV2Consumer;
$this->firefoxConsumer = $firefoxConsumer;
$this->chromeConsumer = $chromeConsumer;
$this->instapaperConsumer = $instapaperConsumer;
$this->pinboardConsumer = $pinboardConsumer;
$this->deliciousConsumer = $deliciousConsumer;
$this->elcuratorConsumer = $elcuratorConsumer;
}
/**
* Count message in RabbitMQ queue.
*
* It get one message without acking it (so it'll stay in the queue)
* which will include the total of *other* messages in the queue.
* Adding one to that messages will result in the full total message.
*
* @param string $importService The import service related: pocket, readability, wallabag_v1 or wallabag_v2
*/
public function getTotalMessage(string $importService): int
{
switch ($importService) {
case 'pocket':
$consumer = $this->pocketConsumer;
break;
case 'readability':
$consumer = $this->readabilityConsumer;
break;
case 'wallabag_v1':
$consumer = $this->wallabagV1Consumer;
break;
case 'wallabag_v2':
$consumer = $this->wallabagV2Consumer;
break;
case 'firefox':
$consumer = $this->firefoxConsumer;
break;
case 'chrome':
$consumer = $this->chromeConsumer;
break;
case 'instapaper':
$consumer = $this->instapaperConsumer;
break;
case 'pinboard':
$consumer = $this->pinboardConsumer;
break;
case 'delicious':
$consumer = $this->deliciousConsumer;
break;
case 'elcurator':
$consumer = $this->elcuratorConsumer;
break;
default:
return 0;
}
$message = $consumer->getChannel()->basic_get('wallabag.import.' . $importService);
if (null === $message) {
return 0;
}
return $message->delivery_info['message_count'] + 1;
}
}