Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 8947f46

Browse filesBrowse files
[Messenger] Add mysql indexes back and work around deadlocks using soft-delete
1 parent 780efff commit 8947f46
Copy full SHA for 8947f46

File tree

1 file changed

+20
-5
lines changed
Filter options

1 file changed

+20
-5
lines changed

‎src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php

Copy file name to clipboardExpand all lines: src/Symfony/Component/Messenger/Transport/Doctrine/Connection.php
+20-5Lines changed: 20 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -13,6 +13,7 @@
1313

1414
use Doctrine\DBAL\Connection as DBALConnection;
1515
use Doctrine\DBAL\DBALException;
16+
use Doctrine\DBAL\Driver\Exception as DriverException;
1617
use Doctrine\DBAL\Driver\Result as DriverResult;
1718
use Doctrine\DBAL\Exception;
1819
use Doctrine\DBAL\Exception\TableNotFoundException;
@@ -157,6 +158,14 @@ public function send(string $body, array $headers, int $delay = 0): string
157158

158159
public function get(): ?array
159160
{
161+
if ($this->driverConnection->getDatabasePlatform() instanceof MySQLPlatform) {
162+
try {
163+
$this->driverConnection->delete($this->configuration['table_name'], ['delivered_at' => '9999-12-31']);
164+
} catch (DriverException $e) {
165+
// Ignore the exception
166+
}
167+
}
168+
160169
get:
161170
$this->driverConnection->beginTransaction();
162171
try {
@@ -224,6 +233,10 @@ public function get(): ?array
224233
public function ack(string $id): bool
225234
{
226235
try {
236+
if ($this->driverConnection->getDatabasePlatform() instanceof MySQLPlatform) {
237+
return $this->driverConnection->update($this->configuration['table_name'], ['delivered_at' => '9999-12-31'], ['id' => $id]) > 0;
238+
}
239+
227240
return $this->driverConnection->delete($this->configuration['table_name'], ['id' => $id]) > 0;
228241
} catch (DBALException|Exception $exception) {
229242
throw new TransportException($exception->getMessage(), 0, $exception);
@@ -233,6 +246,10 @@ public function ack(string $id): bool
233246
public function reject(string $id): bool
234247
{
235248
try {
249+
if ($this->driverConnection->getDatabasePlatform() instanceof MySQLPlatform) {
250+
return $this->driverConnection->update($this->configuration['table_name'], ['delivered_at' => '9999-12-31'], ['id' => $id]) > 0;
251+
}
252+
236253
return $this->driverConnection->delete($this->configuration['table_name'], ['id' => $id]) > 0;
237254
} catch (DBALException|Exception $exception) {
238255
throw new TransportException($exception->getMessage(), 0, $exception);
@@ -388,6 +405,7 @@ private function getSchema(): Schema
388405
$table->addColumn('headers', self::$useDeprecatedConstants ? Type::TEXT : Types::TEXT)
389406
->setNotnull(true);
390407
$table->addColumn('queue_name', self::$useDeprecatedConstants ? Type::STRING : Types::STRING)
408+
->setLength(190) // MySQL 5.6 only supports 191 characters on an indexed column in utf8mb4 mode
391409
->setNotnull(true);
392410
$table->addColumn('created_at', self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE)
393411
->setNotnull(true);
@@ -396,11 +414,8 @@ private function getSchema(): Schema
396414
$table->addColumn('delivered_at', self::$useDeprecatedConstants ? Type::DATETIME : Types::DATETIME_MUTABLE)
397415
->setNotnull(false);
398416
$table->setPrimaryKey(['id']);
399-
// No indices on queue_name and available_at on MySQL to prevent deadlock issues when running multiple consumers.
400-
if (!$this->driverConnection->getDatabasePlatform() instanceof MySQLPlatform) {
401-
$table->addIndex(['queue_name']);
402-
$table->addIndex(['available_at']);
403-
}
417+
$table->addIndex(['queue_name']);
418+
$table->addIndex(['available_at']);
404419
$table->addIndex(['delivered_at']);
405420

406421
return $schema;

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.