Merge pull request #14426 from annando/expire-limit

The limit for the expiry can now be configured
This commit is contained in:
Tobias Diekershoff 2024-09-09 07:58:37 +02:00 committed by GitHub
commit fd5af4fb82
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -66,13 +66,18 @@ class ExpirePosts
*/ */
private static function deleteExpiredOriginPosts() private static function deleteExpiredOriginPosts()
{ {
$limit = DI::config()->get('system', 'dbclean-expire-limit');
if (empty($limit)) {
return;
}
Logger::notice('Delete expired posts'); Logger::notice('Delete expired posts');
// physically remove anything that has been deleted for more than two months // physically remove anything that has been deleted for more than two months
$condition = ["`gravity` = ? AND `deleted` AND `edited` < ?", Item::GRAVITY_PARENT, DateTimeFormat::utc('now - 60 days')]; $condition = ["`gravity` = ? AND `deleted` AND `edited` < ?", Item::GRAVITY_PARENT, DateTimeFormat::utc('now - 60 days')];
$pass = 0; $pass = 0;
do { do {
++$pass; ++$pass;
$rows = DBA::select('post-user', ['uri-id', 'uid'], $condition, ['limit' => 1000]); $rows = DBA::select('post-user', ['uri-id', 'uid'], $condition, ['limit' => $limit]);
$affected_count = 0; $affected_count = 0;
while ($row = Post::fetch($rows)) { while ($row = Post::fetch($rows)) {
Logger::info('Delete expired item', ['pass' => $pass, 'uri-id' => $row['uri-id']]); Logger::info('Delete expired item', ['pass' => $pass, 'uri-id' => $row['uri-id']]);
@ -178,6 +183,11 @@ class ExpirePosts
*/ */
private static function deleteUnusedItemUri() private static function deleteUnusedItemUri()
{ {
$limit = DI::config()->get('system', 'dbclean-expire-limit');
if (empty($limit)) {
return;
}
// We have to avoid deleting newly created "item-uri" entries. // We have to avoid deleting newly created "item-uri" entries.
// So we fetch a post that had been stored yesterday and only delete older ones. // So we fetch a post that had been stored yesterday and only delete older ones.
$item = Post::selectFirstThread( $item = Post::selectFirstThread(
@ -214,14 +224,15 @@ class ExpirePosts
$pass = 0; $pass = 0;
do { do {
++$pass; ++$pass;
$uris = DBA::select('item-uri', ['id'], $condition, ['limit' => 1000]); $uris = DBA::select('item-uri', ['id'], $condition, ['limit' => $limit]);
$total = DBA::numRows($uris);
Logger::notice('Start deleting orphaned URI-ID', ['pass' => $pass, 'last-id' => $item['uri-id']]); Logger::notice('Start deleting orphaned URI-ID', ['pass' => $pass, 'last-id' => $item['uri-id']]);
$affected_count = 0; $affected_count = 0;
while ($rows = DBA::toArray($uris, false, 100)) { while ($rows = DBA::toArray($uris, false, 100)) {
$ids = array_column($rows, 'id'); $ids = array_column($rows, 'id');
DBA::delete('item-uri', ['id' => $ids]); DBA::delete('item-uri', ['id' => $ids]);
$affected_count += DBA::affectedRows(); $affected_count += DBA::affectedRows();
Logger::info('Deleted', ['pass' => $pass, 'rows' => $affected_count]); Logger::debug('Deleted', ['pass' => $pass, 'affected_count' => $affected_count, 'total' => $total]);
} }
DBA::close($uris); DBA::close($uris);
DBA::commit(); DBA::commit();
@ -234,17 +245,17 @@ class ExpirePosts
*/ */
private static function deleteExpiredExternalPosts() private static function deleteExpiredExternalPosts()
{ {
$limit = DI::config()->get('system', 'dbclean-expire-limit');
if (empty($limit)) {
return;
}
$expire_days = DI::config()->get('system', 'dbclean-expire-days'); $expire_days = DI::config()->get('system', 'dbclean-expire-days');
$expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed'); $expire_days_unclaimed = DI::config()->get('system', 'dbclean-expire-unclaimed');
if (empty($expire_days_unclaimed)) { if (empty($expire_days_unclaimed)) {
$expire_days_unclaimed = $expire_days; $expire_days_unclaimed = $expire_days;
} }
$limit = DI::config()->get('system', 'dbclean-expire-limit');
if (empty($limit)) {
return;
}
if (!empty($expire_days)) { if (!empty($expire_days)) {
Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]); Logger::notice('Start collecting expired threads', ['expiry_days' => $expire_days]);
$condition = [ $condition = [
@ -268,7 +279,7 @@ class ExpirePosts
$pass = 0; $pass = 0;
do { do {
++$pass; ++$pass;
$uris = DBA::select('post-thread', ['uri-id'], $condition, ['limit' => 1000]); $uris = DBA::select('post-thread', ['uri-id'], $condition, ['limit' => $limit]);
Logger::notice('Start deleting expired threads', ['pass' => $pass]); Logger::notice('Start deleting expired threads', ['pass' => $pass]);
$affected_count = 0; $affected_count = 0;
@ -296,14 +307,15 @@ class ExpirePosts
$pass = 0; $pass = 0;
do { do {
++$pass; ++$pass;
$uris = DBA::select('post-user', ['uri-id'], $condition, ['limit' => 1000]); $uris = DBA::select('post-user', ['uri-id'], $condition, ['limit' => $limit]);
$total = DBA::numRows($uris);
Logger::notice('Start deleting unclaimed public items', ['pass' => $pass]); Logger::notice('Start deleting unclaimed public items', ['pass' => $pass]);
$affected_count = 0; $affected_count = 0;
while ($rows = DBA::toArray($uris, false, 100)) { while ($rows = DBA::toArray($uris, false, 100)) {
$ids = array_column($rows, 'uri-id'); $ids = array_column($rows, 'uri-id');
DBA::delete('item-uri', ['id' => $ids]); DBA::delete('item-uri', ['id' => $ids]);
$affected_count += DBA::affectedRows(); $affected_count += DBA::affectedRows();
Logger::debug('Deleted', ['pass' => $pass, 'affected_count' => $affected_count, 'total' => $total]);
} }
DBA::close($uris); DBA::close($uris);
DBA::commit(); DBA::commit();