Skip to content

Commit

Permalink
Daily Maintenance: Limit number of logs deleted in a single transacti…
Browse files Browse the repository at this point in the history
…on (#2533)

* Daily Maintenance: Limit number of logs deleted in a single transaction (#3347)

relates to xibosignage/xibo#3347

- Implemented Batch Deletion with a short pause in between batches.
- Each batch deletes up to a maximum of 10k rows.
- Added a 2-second sleep time in between batches.
  • Loading branch information
nadzpogi authored May 24, 2024
1 parent 74554c5 commit 7488a77
Showing 1 changed file with 18 additions and 4 deletions.
22 changes: 18 additions & 4 deletions lib/Helper/DatabaseLogHandler.php
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
<?php
/*
* Copyright (C) 2023 Xibo Signage Ltd
* Copyright (C) 2024 Xibo Signage Ltd
*
* Xibo - Digital Signage - https://xibosignage.com
*
Expand Down Expand Up @@ -141,9 +141,23 @@ public static function tidyLogs($cutOff)
if (self::$pdo === null) {
self::$pdo = PdoStorageService::newConnection('log');
}
$statement = self::$pdo->prepare('DELETE FROM `log` WHERE logdate < :maxage');
$statement->execute(['maxage' => $cutOff]);
PdoStorageService::incrementStat('log', 'delete');

$statement = self::$pdo->prepare('DELETE FROM `log` WHERE logdate < :maxage LIMIT 10000');

do {
// Execute statement
$statement->execute(['maxage' => $cutOff]);

// initialize number of rows deleted
$rowsDeleted = $statement->rowCount();

PdoStorageService::incrementStat('log', 'delete');

// pause for a second
sleep(2);

} while ($rowsDeleted > 0);

} catch (\PDOException $ignored) {}
}
}

0 comments on commit 7488a77

Please sign in to comment.