|
| 1 | +<?php |
| 2 | + |
| 3 | +namespace Utopia\Queue\Broker; |
| 4 | + |
| 5 | +use Utopia\Queue\Consumer; |
| 6 | +use Utopia\Queue\Publisher; |
| 7 | +use Utopia\Queue\Queue; |
| 8 | +use Utopia\Pools\Pool as UtopiaPool; |
| 9 | + |
| 10 | +readonly class Pool implements Publisher, Consumer |
| 11 | +{ |
| 12 | + public function __construct( |
| 13 | + private ?UtopiaPool $publisher = null, |
| 14 | + private ?UtopiaPool $consumer = null, |
| 15 | + ) { |
| 16 | + } |
| 17 | + |
| 18 | + public function enqueue(Queue $queue, array $payload): bool |
| 19 | + { |
| 20 | + return $this->delegatePublish(__FUNCTION__, \func_get_args()); |
| 21 | + } |
| 22 | + |
| 23 | + public function retry(Queue $queue, ?int $limit = null): void |
| 24 | + { |
| 25 | + $this->delegatePublish(__FUNCTION__, \func_get_args()); |
| 26 | + } |
| 27 | + |
| 28 | + public function getQueueSize(Queue $queue, bool $failedJobs = false): int |
| 29 | + { |
| 30 | + return $this->delegatePublish(__FUNCTION__, \func_get_args()); |
| 31 | + } |
| 32 | + |
| 33 | + public function consume(Queue $queue, callable $messageCallback, callable $successCallback, callable $errorCallback): void |
| 34 | + { |
| 35 | + $this->delegateConsumer(__FUNCTION__, \func_get_args()); |
| 36 | + } |
| 37 | + |
| 38 | + public function close(): void |
| 39 | + { |
| 40 | + $this->delegateConsumer(__FUNCTION__, \func_get_args()); |
| 41 | + } |
| 42 | + |
| 43 | + protected function delegatePublish(string $method, array $args): mixed |
| 44 | + { |
| 45 | + return $this->publisher?->use(function (Publisher $adapter) use ($method, $args) { |
| 46 | + return $adapter->$method(...$args); |
| 47 | + }); |
| 48 | + } |
| 49 | + |
| 50 | + protected function delegateConsumer(string $method, array $args): mixed |
| 51 | + { |
| 52 | + return $this->consumer?->use(function (Consumer $adapter) use ($method, $args) { |
| 53 | + return $adapter->$method(...$args); |
| 54 | + }); |
| 55 | + } |
| 56 | +} |
0 commit comments