|
| 1 | +<?php |
| 2 | + |
| 3 | +declare(strict_types=1); |
| 4 | + |
| 5 | +namespace Enqueue\Dbal; |
| 6 | + |
| 7 | +use Doctrine\DBAL\Types\Type; |
| 8 | +use Interop\Queue\Consumer; |
| 9 | +use Interop\Queue\SubscriptionConsumer; |
| 10 | + |
| 11 | +class DbalSubscriptionConsumer implements SubscriptionConsumer |
| 12 | +{ |
| 13 | + /** |
| 14 | + * @var DbalContext |
| 15 | + */ |
| 16 | + private $context; |
| 17 | + |
| 18 | + /** |
| 19 | + * an item contains an array: [DbalConsumer $consumer, callable $callback];. |
| 20 | + * |
| 21 | + * @var array |
| 22 | + */ |
| 23 | + private $subscribers; |
| 24 | + |
| 25 | + /** |
| 26 | + * @var \Doctrine\DBAL\Connection |
| 27 | + */ |
| 28 | + private $dbal; |
| 29 | + |
| 30 | + /** |
| 31 | + * @param DbalContext $context |
| 32 | + */ |
| 33 | + public function __construct(DbalContext $context) |
| 34 | + { |
| 35 | + $this->context = $context; |
| 36 | + $this->dbal = $this->context->getDbalConnection(); |
| 37 | + $this->subscribers = []; |
| 38 | + } |
| 39 | + |
| 40 | + public function consume(int $timeout = 0): void |
| 41 | + { |
| 42 | + if (empty($this->subscribers)) { |
| 43 | + throw new \LogicException('No subscribers'); |
| 44 | + } |
| 45 | + |
| 46 | + $timeout = (int) ceil($timeout / 1000); |
| 47 | + $endAt = time() + $timeout; |
| 48 | + |
| 49 | + $queueNames = []; |
| 50 | + foreach (array_keys($this->subscribers) as $queueName) { |
| 51 | + $queueNames[$queueName] = $queueName; |
| 52 | + } |
| 53 | + |
| 54 | + $currentQueueNames = []; |
| 55 | + while (true) { |
| 56 | + if (empty($currentQueueNames)) { |
| 57 | + $currentQueueNames = $queueNames; |
| 58 | + } |
| 59 | + |
| 60 | + $message = $this->fetchPrioritizedMessage($currentQueueNames) ?: $this->fetchMessage($currentQueueNames); |
| 61 | + |
| 62 | + if ($message) { |
| 63 | + $this->dbal->delete($this->context->getTableName(), ['id' => $message['id']], ['id' => Type::GUID]); |
| 64 | + |
| 65 | + $dbalMessage = $this->context->convertMessage($message); |
| 66 | + |
| 67 | + /** |
| 68 | + * @var DbalConsumer |
| 69 | + * @var callable $callback |
| 70 | + */ |
| 71 | + list($consumer, $callback) = $this->subscribers[$message['queue']]; |
| 72 | + |
| 73 | + if (false === call_user_func($callback, $dbalMessage, $consumer)) { |
| 74 | + return; |
| 75 | + } |
| 76 | + |
| 77 | + unset($currentQueueNames[$message['queue']]); |
| 78 | + } else { |
| 79 | + $currentQueueNames = []; |
| 80 | + |
| 81 | + usleep(200000); // 200ms |
| 82 | + } |
| 83 | + |
| 84 | + if ($timeout && microtime(true) >= $endAt) { |
| 85 | + return; |
| 86 | + } |
| 87 | + } |
| 88 | + } |
| 89 | + |
| 90 | + /** |
| 91 | + * @param DbalConsumer $consumer |
| 92 | + */ |
| 93 | + public function subscribe(Consumer $consumer, callable $callback): void |
| 94 | + { |
| 95 | + if (false == $consumer instanceof DbalConsumer) { |
| 96 | + throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', DbalConsumer::class, get_class($consumer))); |
| 97 | + } |
| 98 | + |
| 99 | + $queueName = $consumer->getQueue()->getQueueName(); |
| 100 | + if (array_key_exists($queueName, $this->subscribers)) { |
| 101 | + if ($this->subscribers[$queueName][0] === $consumer && $this->subscribers[$queueName][1] === $callback) { |
| 102 | + return; |
| 103 | + } |
| 104 | + |
| 105 | + throw new \InvalidArgumentException(sprintf('There is a consumer subscribed to queue: "%s"', $queueName)); |
| 106 | + } |
| 107 | + |
| 108 | + $this->subscribers[$queueName] = [$consumer, $callback]; |
| 109 | + } |
| 110 | + |
| 111 | + /** |
| 112 | + * @param DbalConsumer $consumer |
| 113 | + */ |
| 114 | + public function unsubscribe(Consumer $consumer): void |
| 115 | + { |
| 116 | + if (false == $consumer instanceof DbalConsumer) { |
| 117 | + throw new \InvalidArgumentException(sprintf('The consumer must be instance of "%s" got "%s"', DbalConsumer::class, get_class($consumer))); |
| 118 | + } |
| 119 | + |
| 120 | + $queueName = $consumer->getQueue()->getQueueName(); |
| 121 | + |
| 122 | + if (false == array_key_exists($queueName, $this->subscribers)) { |
| 123 | + return; |
| 124 | + } |
| 125 | + |
| 126 | + if ($this->subscribers[$queueName][0] !== $consumer) { |
| 127 | + return; |
| 128 | + } |
| 129 | + |
| 130 | + unset($this->subscribers[$queueName]); |
| 131 | + } |
| 132 | + |
| 133 | + public function unsubscribeAll(): void |
| 134 | + { |
| 135 | + $this->subscribers = []; |
| 136 | + } |
| 137 | + |
| 138 | + private function fetchMessage(array $queues): ?array |
| 139 | + { |
| 140 | + $query = $this->dbal->createQueryBuilder(); |
| 141 | + $query |
| 142 | + ->select('*') |
| 143 | + ->from($this->context->getTableName()) |
| 144 | + ->andWhere('queue IN (:queues)') |
| 145 | + ->andWhere('priority IS NULL') |
| 146 | + ->andWhere('(delayed_until IS NULL OR delayed_until <= :delayedUntil)') |
| 147 | + ->addOrderBy('published_at', 'asc') |
| 148 | + ->setMaxResults(1) |
| 149 | + ; |
| 150 | + |
| 151 | + $sql = $query->getSQL().' '.$this->dbal->getDatabasePlatform()->getWriteLockSQL(); |
| 152 | + |
| 153 | + $result = $this->dbal->executeQuery( |
| 154 | + $sql, |
| 155 | + [ |
| 156 | + 'queues' => array_keys($queues), |
| 157 | + 'delayedUntil' => time(), |
| 158 | + ], |
| 159 | + [ |
| 160 | + 'queues' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, |
| 161 | + 'delayedUntil' => \Doctrine\DBAL\ParameterType::INTEGER, |
| 162 | + ] |
| 163 | + )->fetch(); |
| 164 | + |
| 165 | + return $result ?: null; |
| 166 | + } |
| 167 | + |
| 168 | + private function fetchPrioritizedMessage(array $queues): ?array |
| 169 | + { |
| 170 | + $query = $this->dbal->createQueryBuilder(); |
| 171 | + $query |
| 172 | + ->select('*') |
| 173 | + ->from($this->context->getTableName()) |
| 174 | + ->andWhere('queue IN (:queues)') |
| 175 | + ->andWhere('priority IS NOT NULL') |
| 176 | + ->andWhere('(delayed_until IS NULL OR delayed_until <= :delayedUntil)') |
| 177 | + ->addOrderBy('published_at', 'asc') |
| 178 | + ->addOrderBy('priority', 'desc') |
| 179 | + ->setMaxResults(1) |
| 180 | + ; |
| 181 | + |
| 182 | + $sql = $query->getSQL().' '.$this->dbal->getDatabasePlatform()->getWriteLockSQL(); |
| 183 | + |
| 184 | + $result = $this->dbal->executeQuery( |
| 185 | + $sql, |
| 186 | + [ |
| 187 | + 'queues' => array_keys($queues), |
| 188 | + 'delayedUntil' => time(), |
| 189 | + ], |
| 190 | + [ |
| 191 | + 'queues' => \Doctrine\DBAL\Connection::PARAM_STR_ARRAY, |
| 192 | + 'delayedUntil' => \Doctrine\DBAL\ParameterType::INTEGER, |
| 193 | + ] |
| 194 | + )->fetch(); |
| 195 | + |
| 196 | + return $result ?: null; |
| 197 | + } |
| 198 | +} |
0 commit comments