Skip to content

Commit 815083c

Browse files
authored
Merge pull request #640 from php-enqueue/dbal-do-not-fail-on-consumption
[dbal] handle gracefully concurrency issues or 3rd party interruptions.
2 parents 9a182c1 + db7c6bd commit 815083c

File tree

1 file changed

+14
-4
lines changed

1 file changed

+14
-4
lines changed

Diff for: pkg/dbal/DbalConsumerHelperTrait.php

+14-4
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
namespace Enqueue\Dbal;
66

77
use Doctrine\DBAL\Connection;
8+
use Doctrine\DBAL\Exception\DeadlockException;
89
use Doctrine\DBAL\ParameterType;
910
use Doctrine\DBAL\Types\Type;
1011
use Ramsey\Uuid\Uuid;
@@ -73,8 +74,9 @@ protected function fetchMessage(array $queues, int $redeliveryDelay): ?DbalMessa
7374
->fetch()
7475
;
7576

77+
// the message has been removed by a 3rd party, such as truncate operation.
7678
if (false == $deliveredMessage) {
77-
throw new \LogicException('There must be a message at all times at this stage but there is no a message.');
79+
continue;
7880
}
7981

8082
if ($deliveredMessage['redelivered'] || empty($deliveredMessage['time_to_live']) || $deliveredMessage['time_to_live'] > time()) {
@@ -105,9 +107,13 @@ protected function redeliverMessages(): void
105107
->setParameter('redelivered', true, Type::BOOLEAN)
106108
;
107109

108-
$update->execute();
110+
try {
111+
$update->execute();
109112

110-
$this->redeliverMessagesLastExecutedAt = microtime(true);
113+
$this->redeliverMessagesLastExecutedAt = microtime(true);
114+
} catch (DeadlockException $e) {
115+
// maybe next time we'll get more luck
116+
}
111117
}
112118

113119
protected function removeExpiredMessages(): void
@@ -127,7 +133,11 @@ protected function removeExpiredMessages(): void
127133
->setParameter(':now', time(), Type::BIGINT)
128134
;
129135

130-
$delete->execute();
136+
try {
137+
$delete->execute();
138+
} catch (DeadlockException $e) {
139+
// maybe next time we'll get more luck
140+
}
131141

132142
$this->removeExpiredMessagesLastExecutedAt = microtime(true);
133143
}

0 commit comments

Comments
 (0)