Skip to content
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

Improve cleanup scripts #12906

Closed
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
28 changes: 1 addition & 27 deletions daily.php
Expand Up @@ -57,33 +57,7 @@
}

if ($options['f'] === 'syslog') {
$lock = Cache::lock('syslog_purge', 86000);
if ($lock->get()) {
$syslog_purge = Config::get('syslog_purge');

if (is_numeric($syslog_purge)) {
$rows = (int) dbFetchCell('SELECT MIN(seq) FROM syslog');
$initial_rows = $rows;
while (true) {
$limit = dbFetchCell('SELECT seq FROM syslog WHERE seq >= ? ORDER BY seq LIMIT 1000,1', [$rows]);
if (empty($limit)) {
break;
}

// Deletes are done in blocks of 1000 to avoid a single very large operation.
if (dbDelete('syslog', 'seq >= ? AND seq < ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)', [$rows, $limit, $syslog_purge]) > 0) {
$rows = $limit;
} else {
break;
}
}

dbDelete('syslog', 'seq >= ? AND timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)', [$rows, $syslog_purge]);
$final_rows = $rows - $initial_rows;
echo "Syslog cleared for entries over $syslog_purge days (about $final_rows rows)\n";
}
$lock->release();
}
lock_and_purge('syslog', 'timestamp < DATE_SUB(NOW(), INTERVAL ? DAY)');
}

if ($options['f'] === 'ports_fdb') {
Expand Down
14 changes: 11 additions & 3 deletions includes/functions.php
Expand Up @@ -2045,9 +2045,10 @@ function get_device_oid_limit($device)
*
* @param string $table
* @param string $sql
* @param int $count
* @return int exit code
*/
function lock_and_purge($table, $sql)
function lock_and_purge($table, $sql, int $count = 1000)
{
$purge_name = $table . '_purge';
$lock = Cache::lock($purge_name, 86000);
Expand All @@ -2056,9 +2057,16 @@ function lock_and_purge($table, $sql)

$name = str_replace('_', ' ', ucfirst($table));
if (is_numeric($purge_days)) {
if (dbDelete($table, $sql, [$purge_days])) {
echo "$name cleared for entries over $purge_days days\n";
if (is_numeric($count)) {
Copy link
Member

Choose a reason for hiding this comment

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

FYI $count is always numeric, you said it must be an int in the parameters.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

good catch, removed the check

$sql = $sql . " LIMIT $count";
}
while (true) {
// Deletes are done in blocks to avoid a single very large operation.
if (! dbDelete($table, $sql, [$purge_days]) > 0) {
break;
}
}
echo "$name cleared for entries over $purge_days days\n";
}
$lock->release();

Expand Down