Skip to content

Commit 50f3f07

Browse files
committed
Fixes.
1 parent 03c40ab commit 50f3f07

4 files changed

Lines changed: 71 additions & 54 deletions

File tree

‎pkg/dbal/DbalConsumer.php‎

Lines changed: 14 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,6 @@
1111
use Interop\Queue\Impl\ConsumerPollingTrait;
1212
use Interop\Queue\Message;
1313
use Interop\Queue\Queue;
14-
use Ramsey\Uuid\Uuid;
1514

1615
class DbalConsumer implements Consumer
1716
{
@@ -49,16 +48,6 @@ public function __construct(DbalContext $context, DbalDestination $queue)
4948
$this->redeliveryDelay = 1200000;
5049
}
5150

52-
public function getContext(): DbalContext
53-
{
54-
return $this->context;
55-
}
56-
57-
public function getConnection(): Connection
58-
{
59-
return $this->dbal;
60-
}
61-
6251
/**
6352
* Get interval between retry failed messages in milliseconds.
6453
*/
@@ -68,7 +57,7 @@ public function getRedeliveryDelay(): int
6857
}
6958

7059
/**
71-
* Interval between retry failed messages in seconds.
60+
* Get interval between retrying failed messages in milliseconds.
7261
*/
7362
public function setRedeliveryDelay(int $redeliveryDelay): self
7463
{
@@ -87,13 +76,12 @@ public function getQueue(): Queue
8776

8877
public function receiveNoWait(): ?Message
8978
{
90-
$deliveryId = (string) Uuid::uuid1();
9179
$redeliveryDelay = $this->getRedeliveryDelay() / 1000; // milliseconds to seconds
9280

9381
$this->redeliverMessages();
9482

9583
// get top message from the queue
96-
if ($message = $this->fetchMessage([$this->queue->getQueueName()], $deliveryId, $redeliveryDelay)) {
84+
if ($message = $this->fetchMessage([$this->queue->getQueueName()], $redeliveryDelay)) {
9785
if ($message['redelivered'] || empty($message['time_to_live']) || $message['time_to_live'] > time()) {
9886
return $this->getContext()->convertMessage($message);
9987
}
@@ -107,6 +95,8 @@ public function receiveNoWait(): ?Message
10795
*/
10896
public function acknowledge(Message $message): void
10997
{
98+
InvalidMessageException::assertMessageInstanceOf($message, DbalMessage::class);
99+
110100
$this->deleteMessage($message->getDeliveryId());
111101
}
112102

@@ -126,6 +116,16 @@ public function reject(Message $message, bool $requeue = false): void
126116
$this->deleteMessage($message->getDeliveryId());
127117
}
128118

119+
protected function getContext(): DbalContext
120+
{
121+
return $this->context;
122+
}
123+
124+
protected function getConnection(): Connection
125+
{
126+
return $this->dbal;
127+
}
128+
129129
private function deleteMessage(string $deliveryId): void
130130
{
131131
if (empty($deliveryId)) {
@@ -138,19 +138,4 @@ private function deleteMessage(string $deliveryId): void
138138
['delivery_id' => Type::STRING]
139139
);
140140
}
141-
142-
private function redeliverMessages(): void
143-
{
144-
$this->getConnection()->createQueryBuilder()
145-
->update($this->getContext()->getTableName())
146-
->set('delivery_id', ':deliveryId')
147-
->set('redelivered', ':redelivered')
148-
->andWhere('delivery_id IS NOT NULL')
149-
->andWhere('redeliver_after < :now')
150-
->setParameter(':now', (int) time(), Type::BIGINT)
151-
->setParameter('deliveryId', null, Type::STRING)
152-
->setParameter('redelivered', true, Type::BOOLEAN)
153-
->execute()
154-
;
155-
}
156141
}

‎pkg/dbal/DbalConsumerHelperTrait.php‎

Lines changed: 23 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,20 +7,22 @@
77
use Doctrine\DBAL\Connection;
88
use Doctrine\DBAL\ParameterType;
99
use Doctrine\DBAL\Types\Type;
10+
use Ramsey\Uuid\Uuid;
1011

1112
trait DbalConsumerHelperTrait
1213
{
13-
abstract public function getContext(): DbalContext;
14+
abstract protected function getContext(): DbalContext;
1415

15-
abstract public function getConnection(): Connection;
16+
abstract protected function getConnection(): Connection;
1617

17-
protected function fetchMessage(array $queues, string $deliveryId, int $redeliveryDelay): ?array
18+
protected function fetchMessage(array $queues, int $redeliveryDelay): ?array
1819
{
19-
try {
20-
$now = time();
20+
$now = time();
21+
$deliveryId = (string) Uuid::uuid1();
2122

22-
$this->getConnection()->beginTransaction();
23+
$this->getConnection()->beginTransaction();
2324

25+
try {
2426
$query = $this->getConnection()->createQueryBuilder()
2527
->select('*')
2628
->from($this->getContext()->getTableName())
@@ -75,4 +77,19 @@ protected function fetchMessage(array $queues, string $deliveryId, int $redelive
7577
throw $e;
7678
}
7779
}
80+
81+
protected function redeliverMessages(): void
82+
{
83+
$this->getConnection()->createQueryBuilder()
84+
->update($this->getContext()->getTableName())
85+
->set('delivery_id', ':deliveryId')
86+
->set('redelivered', ':redelivered')
87+
->andWhere('delivery_id IS NOT NULL')
88+
->andWhere('redeliver_after < :now')
89+
->setParameter(':now', (int) time(), Type::BIGINT)
90+
->setParameter('deliveryId', null, Type::STRING)
91+
->setParameter('redelivered', true, Type::BOOLEAN)
92+
->execute()
93+
;
94+
}
7895
}

‎pkg/dbal/DbalSubscriptionConsumer.php‎

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,6 @@
77
use Doctrine\DBAL\Connection;
88
use Interop\Queue\Consumer;
99
use Interop\Queue\SubscriptionConsumer;
10-
use Ramsey\Uuid\Uuid;
1110

1211
class DbalSubscriptionConsumer implements SubscriptionConsumer
1312
{
@@ -49,27 +48,19 @@ public function __construct(DbalContext $context)
4948
$this->redeliveryDelay = 1200000;
5049
}
5150

52-
public function getContext(): DbalContext
53-
{
54-
return $this->context;
55-
}
56-
57-
public function getConnection(): Connection
58-
{
59-
return $this->dbal;
60-
}
61-
6251
/**
63-
* Get interval between retry failed messages in milliseconds.
52+
* Get interval between retrying failed messages in milliseconds.
6453
*/
6554
public function getRedeliveryDelay(): int
6655
{
6756
return $this->redeliveryDelay;
6857
}
6958

70-
public function setRedeliveryDelay(int $redeliveryDelay): void
59+
public function setRedeliveryDelay(int $redeliveryDelay): self
7160
{
7261
$this->redeliveryDelay = $redeliveryDelay;
62+
63+
return $this;
7364
}
7465

7566
public function consume(int $timeout = 0): void
@@ -78,23 +69,24 @@ public function consume(int $timeout = 0): void
7869
throw new \LogicException('No subscribers');
7970
}
8071

81-
$now = time();
82-
$timeout /= 1000;
83-
$deliveryId = (string) Uuid::uuid1();
84-
$redeliveryDelay = $this->getRedeliveryDelay() / 1000; // milliseconds to seconds
85-
8672
$queueNames = [];
8773
foreach (array_keys($this->subscribers) as $queueName) {
8874
$queueNames[$queueName] = $queueName;
8975
}
9076

77+
$timeout /= 1000;
78+
$redeliveryDelay = $this->getRedeliveryDelay() / 1000; // milliseconds to seconds
79+
9180
$currentQueueNames = [];
9281
while (true) {
9382
if (empty($currentQueueNames)) {
9483
$currentQueueNames = $queueNames;
9584
}
9685

97-
if ($message = $this->fetchMessage($currentQueueNames, $deliveryId, $redeliveryDelay)) {
86+
$now = time();
87+
$this->redeliverMessages();
88+
89+
if ($message = $this->fetchMessage($currentQueueNames, $redeliveryDelay)) {
9890
$dbalMessage = $this->getContext()->convertMessage($message);
9991

10092
/**
@@ -167,4 +159,14 @@ public function unsubscribeAll(): void
167159
{
168160
$this->subscribers = [];
169161
}
162+
163+
protected function getContext(): DbalContext
164+
{
165+
return $this->context;
166+
}
167+
168+
protected function getConnection(): Connection
169+
{
170+
return $this->dbal;
171+
}
170172
}

‎pkg/dbal/Tests/DbalConsumerTest.php‎

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,19 @@ public function testShouldReturnInstanceOfDestination()
4040
$this->assertSame($destination, $consumer->getQueue());
4141
}
4242

43+
public function testAcknowledgeShouldThrowIfInstanceOfMessageIsInvalid()
44+
{
45+
$this->expectException(InvalidMessageException::class);
46+
$this->expectExceptionMessage(
47+
'The message must be an instance of '.
48+
'Enqueue\Dbal\DbalMessage '.
49+
'but it is Enqueue\Dbal\Tests\InvalidMessage.'
50+
);
51+
52+
$consumer = new DbalConsumer($this->createContextMock(), new DbalDestination('queue'));
53+
$consumer->acknowledge(new InvalidMessage());
54+
}
55+
4356
public function testShouldDeleteMessageOnAcknowledge()
4457
{
4558
$queue = new DbalDestination('queue');

0 commit comments

Comments
 (0)