Skip to content

[dbal] handle gracefully concurrency issues or 3rd party interruptions. #640

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Nov 16, 2018
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
18 changes: 14 additions & 4 deletions pkg/dbal/DbalConsumerHelperTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
namespace Enqueue\Dbal;

use Doctrine\DBAL\Connection;
use Doctrine\DBAL\Exception\DeadlockException;
use Doctrine\DBAL\ParameterType;
use Doctrine\DBAL\Types\Type;
use Ramsey\Uuid\Uuid;
Expand Down Expand Up @@ -73,8 +74,9 @@ protected function fetchMessage(array $queues, int $redeliveryDelay): ?DbalMessa
->fetch()
;

// the message has been removed by a 3rd party, such as truncate operation.
if (false == $deliveredMessage) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

=== is probably intended here

throw new \LogicException('There must be a message at all times at this stage but there is no a message.');
continue;
}

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

$update->execute();
try {
$update->execute();

$this->redeliverMessagesLastExecutedAt = microtime(true);
$this->redeliverMessagesLastExecutedAt = microtime(true);
} catch (DeadlockException $e) {
Copy link

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it would be better to use \Doctrine\DBAL\Exception\RetryableException that I created. this would also cover lock timeouts.

// maybe next time we'll get more luck
}
}

protected function removeExpiredMessages(): void
Expand All @@ -127,7 +133,11 @@ protected function removeExpiredMessages(): void
->setParameter(':now', time(), Type::BIGINT)
;

$delete->execute();
try {
$delete->execute();
} catch (DeadlockException $e) {
// maybe next time we'll get more luck
}

$this->removeExpiredMessagesLastExecutedAt = microtime(true);
}
Expand Down