1
0
mirror of https://github.com/mailcow/mailcow-dockerized.git synced 2026-07-05 20:45:51 +00:00

[PHP-FPM] Use transactions for batch deletion of sasl_log data

This commit is contained in:
FreddleSpl0it
2024-09-05 10:36:15 +02:00
parent b54a9c7bb3
commit d22b9510fc
+23 -15
View File
@@ -17,22 +17,23 @@ try {
} }
catch (PDOException $e) { catch (PDOException $e) {
echo($e->getMessage() . PHP_EOL); echo($e->getMessage() . PHP_EOL);
exit; exit(1);
} }
$dateThreshold = new DateTime();
$dateThreshold->modify('-31 days');
$dateThresholdFormatted = $dateThreshold->format('Y-m-d H:i:s');
$batchSize = 1000;
$lastProcessedDatetime = null;
$lastProcessedUsername = "";
$lastProcessedService = "";
$loopCounter = 0;
$rowCounter = 0;
$clearedRowCounter = 0;
try { try {
$dateThreshold = new DateTime();
$dateThreshold->modify('-31 days');
$dateThresholdFormatted = $dateThreshold->format('Y-m-d H:i:s');
$batchSize = 1000;
$lastProcessedDatetime = null;
$lastFetchedRows = 0;
$loopCounter = 0;
$rowCounter = 0;
$clearedRowCounter = 0;
do { do {
$loopCounter++; $loopCounter++;
echo("Processing batch $loopCounter\n"); echo("Processing batch $loopCounter\n");
@@ -58,6 +59,7 @@ try {
echo("Fetched $rowCount rows (total of $rowCounter)\n"); echo("Fetched $rowCount rows (total of $rowCounter)\n");
$pdo->beginTransaction();
foreach ($rows as $row) { foreach ($rows as $row) {
$stmt = $pdo->prepare(" $stmt = $pdo->prepare("
SELECT MAX(datetime) as max_date SELECT MAX(datetime) as max_date
@@ -85,22 +87,28 @@ try {
$clearedRowCounter++; $clearedRowCounter++;
} }
} }
$pdo->commit();
if ($lastFetchedRows == $rowCount && $rowCount != $batchSize) { if ($lastProcessedDatetime == $rows[$rowCount - 1]['datetime'] &&
$lastProcessedUsername == $rows[$rowCount - 1]['username'] &&
$lastProcessedService == $rows[$rowCount - 1]['service'] ||
$rowCount != $batchSize) {
$rowCount = 0; $rowCount = 0;
} }
// Update last processed datetime // Update last processed datetime
if ($rowCount > 0) { if ($rowCount > 0) {
$lastProcessedDatetime = $rows[$rowCount - 1]['datetime']; $lastProcessedDatetime = $rows[$rowCount - 1]['datetime'];
$lastFetchedRows = $rowCount; $lastProcessedUsername = $rows[$rowCount - 1]['username'];
$lastProcessedService = $rows[$rowCount - 1]['service'];
} }
} while ($rowCount > 0); } while ($rowCount > 0);
} }
catch (PDOException $e) { catch (PDOException $e) {
echo($e->getMessage() . PHP_EOL); echo($e->getMessage() . PHP_EOL);
exit; exit(1);
} }
echo("Succesfully cleared $clearedRowCounter rows of $rowCounter rows"); echo("Succesfully cleared $clearedRowCounter rows of $rowCounter rows");
exit(0);