Skip to content

Commit 662c8e4

Browse files
giosh94mhzfabpot
authored andcommitted
Messenger: avoid DELETE statement flood when using Doctrine+MySQL backend
Cleanup for acknoledged and rejected messages is made only after a job update to avoid recording a DELETE statement every second.
1 parent e6c875e commit 662c8e4

File tree

1 file changed

+14
-3
lines changed
  • src/Symfony/Component/Messenger/Bridge/Doctrine/Transport

1 file changed

+14
-3
lines changed

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

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ class Connection implements ResetInterface
5252
protected ?float $queueEmptiedAt = null;
5353

5454
private bool $autoSetup;
55+
private bool $doMysqlCleanup = false;
5556

5657
/**
5758
* Constructor.
@@ -75,6 +76,7 @@ public function __construct(
7576
public function reset(): void
7677
{
7778
$this->queueEmptiedAt = null;
79+
$this->doMysqlCleanup = false;
7880
}
7981

8082
public function getConfiguration(): array
@@ -152,9 +154,10 @@ public function send(string $body, array $headers, int $delay = 0): string
152154

153155
public function get(): ?array
154156
{
155-
if ($this->driverConnection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
157+
if ($this->doMysqlCleanup && $this->driverConnection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
156158
try {
157159
$this->driverConnection->delete($this->configuration['table_name'], ['delivered_at' => '9999-12-31 23:59:59']);
160+
$this->doMysqlCleanup = false;
158161
} catch (DriverException $e) {
159162
// Ignore the exception
160163
} catch (TableNotFoundException $e) {
@@ -252,7 +255,11 @@ public function ack(string $id): bool
252255
{
253256
try {
254257
if ($this->driverConnection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
255-
return $this->driverConnection->update($this->configuration['table_name'], ['delivered_at' => '9999-12-31 23:59:59'], ['id' => $id]) > 0;
258+
if ($updated = $this->driverConnection->update($this->configuration['table_name'], ['delivered_at' => '9999-12-31 23:59:59'], ['id' => $id]) > 0) {
259+
$this->doMysqlCleanup = true;
260+
}
261+
262+
return $updated;
256263
}
257264

258265
return $this->driverConnection->delete($this->configuration['table_name'], ['id' => $id]) > 0;
@@ -265,7 +272,11 @@ public function reject(string $id): bool
265272
{
266273
try {
267274
if ($this->driverConnection->getDatabasePlatform() instanceof AbstractMySQLPlatform) {
268-
return $this->driverConnection->update($this->configuration['table_name'], ['delivered_at' => '9999-12-31 23:59:59'], ['id' => $id]) > 0;
275+
if ($updated = $this->driverConnection->update($this->configuration['table_name'], ['delivered_at' => '9999-12-31 23:59:59'], ['id' => $id]) > 0) {
276+
$this->doMysqlCleanup = true;
277+
}
278+
279+
return $updated;
269280
}
270281

271282
return $this->driverConnection->delete($this->configuration['table_name'], ['id' => $id]) > 0;

0 commit comments

Comments
 (0)