]> source.dussan.org Git - nextcloud-server.git/commitdiff
fix(caldav): automatically delete outdated scheduling objects 45641/head
authorAnna Larch <anna@nextcloud.com>
Wed, 8 May 2024 18:38:51 +0000 (20:38 +0200)
committerbackportbot[bot] <backportbot[bot]@users.noreply.github.com>
Mon, 3 Jun 2024 11:10:03 +0000 (11:10 +0000)
Signed-off-by: Anna Larch <anna@nextcloud.com>
12 files changed:
apps/dav/composer/composer/autoload_classmap.php
apps/dav/composer/composer/autoload_static.php
apps/dav/lib/BackgroundJob/DeleteOutdatedSchedulingObjects.php [new file with mode: 0644]
apps/dav/lib/CalDAV/CalDavBackend.php
apps/dav/lib/Migration/DeleteSchedulingObjects.php [new file with mode: 0644]
apps/dav/lib/Migration/Version1004Date20170825134824.php
apps/settings/composer/composer/autoload_classmap.php
apps/settings/composer/composer/autoload_static.php
apps/settings/lib/AppInfo/Application.php
apps/settings/lib/SetupChecks/SchedulingTableSize.php [new file with mode: 0644]
core/Application.php
lib/private/Repair.php

index 1aff4d758c79ab56e7a244c62459a78cb07af900..176f5d32ed0d6362c1e8db9e64cf9cab1a25cbbc 100644 (file)
@@ -16,6 +16,7 @@ return array(
     'OCA\\DAV\\BackgroundJob\\CalendarRetentionJob' => $baseDir . '/../lib/BackgroundJob/CalendarRetentionJob.php',
     'OCA\\DAV\\BackgroundJob\\CleanupDirectLinksJob' => $baseDir . '/../lib/BackgroundJob/CleanupDirectLinksJob.php',
     'OCA\\DAV\\BackgroundJob\\CleanupInvitationTokenJob' => $baseDir . '/../lib/BackgroundJob/CleanupInvitationTokenJob.php',
+    'OCA\\DAV\\BackgroundJob\\DeleteOutdatedSchedulingObjects' => $baseDir . '/../lib/BackgroundJob/DeleteOutdatedSchedulingObjects.php',
     'OCA\\DAV\\BackgroundJob\\EventReminderJob' => $baseDir . '/../lib/BackgroundJob/EventReminderJob.php',
     'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => $baseDir . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php',
     'OCA\\DAV\\BackgroundJob\\OutOfOfficeEventDispatcherJob' => $baseDir . '/../lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php',
@@ -288,6 +289,7 @@ return array(
     'OCA\\DAV\\Migration\\BuildSocialSearchIndexBackgroundJob' => $baseDir . '/../lib/Migration/BuildSocialSearchIndexBackgroundJob.php',
     'OCA\\DAV\\Migration\\CalDAVRemoveEmptyValue' => $baseDir . '/../lib/Migration/CalDAVRemoveEmptyValue.php',
     'OCA\\DAV\\Migration\\ChunkCleanup' => $baseDir . '/../lib/Migration/ChunkCleanup.php',
+    'OCA\\DAV\\Migration\\DeleteSchedulingObjects' => $baseDir . '/../lib/Migration/DeleteSchedulingObjects.php',
     'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => $baseDir . '/../lib/Migration/FixBirthdayCalendarComponent.php',
     'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => $baseDir . '/../lib/Migration/RefreshWebcalJobRegistrar.php',
     'OCA\\DAV\\Migration\\RegenerateBirthdayCalendars' => $baseDir . '/../lib/Migration/RegenerateBirthdayCalendars.php',
index 9850414b8affd651c6fe19b18e91245b8fae649c..b8888ed425e81030edd64ed1f0f80d3f522b6cba 100644 (file)
@@ -31,6 +31,7 @@ class ComposerStaticInitDAV
         'OCA\\DAV\\BackgroundJob\\CalendarRetentionJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/CalendarRetentionJob.php',
         'OCA\\DAV\\BackgroundJob\\CleanupDirectLinksJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanupDirectLinksJob.php',
         'OCA\\DAV\\BackgroundJob\\CleanupInvitationTokenJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/CleanupInvitationTokenJob.php',
+        'OCA\\DAV\\BackgroundJob\\DeleteOutdatedSchedulingObjects' => __DIR__ . '/..' . '/../lib/BackgroundJob/DeleteOutdatedSchedulingObjects.php',
         'OCA\\DAV\\BackgroundJob\\EventReminderJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/EventReminderJob.php',
         'OCA\\DAV\\BackgroundJob\\GenerateBirthdayCalendarBackgroundJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/GenerateBirthdayCalendarBackgroundJob.php',
         'OCA\\DAV\\BackgroundJob\\OutOfOfficeEventDispatcherJob' => __DIR__ . '/..' . '/../lib/BackgroundJob/OutOfOfficeEventDispatcherJob.php',
@@ -303,6 +304,7 @@ class ComposerStaticInitDAV
         'OCA\\DAV\\Migration\\BuildSocialSearchIndexBackgroundJob' => __DIR__ . '/..' . '/../lib/Migration/BuildSocialSearchIndexBackgroundJob.php',
         'OCA\\DAV\\Migration\\CalDAVRemoveEmptyValue' => __DIR__ . '/..' . '/../lib/Migration/CalDAVRemoveEmptyValue.php',
         'OCA\\DAV\\Migration\\ChunkCleanup' => __DIR__ . '/..' . '/../lib/Migration/ChunkCleanup.php',
+        'OCA\\DAV\\Migration\\DeleteSchedulingObjects' => __DIR__ . '/..' . '/../lib/Migration/DeleteSchedulingObjects.php',
         'OCA\\DAV\\Migration\\FixBirthdayCalendarComponent' => __DIR__ . '/..' . '/../lib/Migration/FixBirthdayCalendarComponent.php',
         'OCA\\DAV\\Migration\\RefreshWebcalJobRegistrar' => __DIR__ . '/..' . '/../lib/Migration/RefreshWebcalJobRegistrar.php',
         'OCA\\DAV\\Migration\\RegenerateBirthdayCalendars' => __DIR__ . '/..' . '/../lib/Migration/RegenerateBirthdayCalendars.php',
diff --git a/apps/dav/lib/BackgroundJob/DeleteOutdatedSchedulingObjects.php b/apps/dav/lib/BackgroundJob/DeleteOutdatedSchedulingObjects.php
new file mode 100644 (file)
index 0000000..fa53a8b
--- /dev/null
@@ -0,0 +1,35 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\DAV\BackgroundJob;
+
+use OCA\DAV\CalDAV\CalDavBackend;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\TimedJob;
+use Psr\Log\LoggerInterface;
+
+class DeleteOutdatedSchedulingObjects extends TimedJob {
+       public function __construct(
+               private CalDavBackend $calDavBackend,
+               private LoggerInterface $logger,
+               ITimeFactory $timeFactory,
+       ) {
+               parent::__construct($timeFactory);
+               $this->setInterval(23 * 60 * 60);
+               $this->setTimeSensitivity(self::TIME_INSENSITIVE);
+       }
+
+       /**
+        * @param array $argument
+        */
+       protected function run($argument): void {
+               $time = $this->time->getTime() - (60 * 60);
+               $this->calDavBackend->deleteOutdatedSchedulingObjects($time, 50000);
+               $this->logger->info("Removed outdated scheduling objects");
+       }
+}
index 0aa4426c786a44fa7938d2dcacef7b58dc6bc1c8..af03d57018c8754d653f2852327ff970e107fd68 100644 (file)
@@ -2738,6 +2738,44 @@ class CalDavBackend extends AbstractBackend implements SyncSupport, Subscription
                                ->executeStatement();
        }
 
+       /**
+        * Deletes all scheduling objects last modified before $modifiedBefore from the inbox collection.
+        *
+        * @param int $modifiedBefore
+        * @param int $limit
+        * @return void
+        */
+       public function deleteOutdatedSchedulingObjects(int $modifiedBefore, int $limit): void {
+               $query = $this->db->getQueryBuilder();
+               $query->select('id')
+                       ->from('schedulingobjects')
+                       ->where($query->expr()->lt('lastmodified', $query->createNamedParameter($modifiedBefore)))
+                       ->setMaxResults($limit);
+               $result = $query->executeQuery();
+               $count = $result->rowCount();
+               if($count === 0) {
+                       return;
+               }
+               $ids = array_map(static function (array $id) {
+                       return (int)$id[0];
+               }, $result->fetchAll(\PDO::FETCH_NUM));
+               $result->closeCursor();
+
+               $numDeleted = 0;
+               $deleteQuery = $this->db->getQueryBuilder();
+               $deleteQuery->delete('schedulingobjects')
+                       ->where($deleteQuery->expr()->in('id', $deleteQuery->createParameter('ids'), IQueryBuilder::PARAM_INT_ARRAY));
+               foreach(array_chunk($ids, 1000) as $chunk) {
+                       $deleteQuery->setParameter('ids', $chunk, IQueryBuilder::PARAM_INT_ARRAY);
+                       $numDeleted += $deleteQuery->executeStatement();
+               }
+
+               if($numDeleted === $limit) {
+                       $this->logger->info("Deleted $limit scheduling objects, continuing with next batch");
+                       $this->deleteOutdatedSchedulingObjects($modifiedBefore, $limit);
+               }
+       }
+
        /**
         * Creates a new scheduling object. This should land in a users' inbox.
         *
diff --git a/apps/dav/lib/Migration/DeleteSchedulingObjects.php b/apps/dav/lib/Migration/DeleteSchedulingObjects.php
new file mode 100644 (file)
index 0000000..3919236
--- /dev/null
@@ -0,0 +1,38 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\DAV\Migration;
+
+use OCA\DAV\BackgroundJob\DeleteOutdatedSchedulingObjects;
+use OCA\DAV\CalDAV\CalDavBackend;
+use OCP\AppFramework\Utility\ITimeFactory;
+use OCP\BackgroundJob\IJobList;
+use OCP\Migration\IOutput;
+use OCP\Migration\IRepairStep;
+
+class DeleteSchedulingObjects implements IRepairStep {
+       public function __construct(private IJobList $jobList,
+               private ITimeFactory $time,
+               private CalDavBackend $calDavBackend
+       ) {
+       }
+
+       public function getName(): string {
+               return 'Handle outdated scheduling events';
+       }
+
+       public function run(IOutput $output): void {
+               $output->info('Cleaning up old scheduling events');
+               $time = $this->time->getTime() - (60 * 60);
+               $this->calDavBackend->deleteOutdatedSchedulingObjects($time, 50000);
+               if (!$this->jobList->has(DeleteOutdatedSchedulingObjects::class, null)) {
+                       $output->info('Adding background job to delete old scheduling objects');
+                       $this->jobList->add(DeleteOutdatedSchedulingObjects::class, null);
+               }
+       }
+}
index a7cbaa78ef23e52619ceb1e0059dd00149787d96..7321bba62ff9260cf36cdf4f280307bf05598702 100644 (file)
@@ -383,6 +383,7 @@ class Version1004Date20170825134824 extends SimpleMigrationStep {
                        ]);
                        $table->setPrimaryKey(['id']);
                        $table->addIndex(['principaluri'], 'schedulobj_principuri_index');
+                       $table->addIndex(['lastmodified'], 'schedulobj_lastmodified_idx');
                }
 
                if (!$schema->hasTable('cards_properties')) {
index 17e47f62a7d8b36e6be0c78b378ca15b338f9d5f..6bbb2978a875eb766982c9cd1a6be68cd6d53731 100644 (file)
@@ -117,6 +117,7 @@ return array(
     'OCA\\Settings\\SetupChecks\\PushService' => $baseDir . '/../lib/SetupChecks/PushService.php',
     'OCA\\Settings\\SetupChecks\\RandomnessSecure' => $baseDir . '/../lib/SetupChecks/RandomnessSecure.php',
     'OCA\\Settings\\SetupChecks\\ReadOnlyConfig' => $baseDir . '/../lib/SetupChecks/ReadOnlyConfig.php',
+    'OCA\\Settings\\SetupChecks\\SchedulingTableSize' => $baseDir . '/../lib/SetupChecks/SchedulingTableSize.php',
     'OCA\\Settings\\SetupChecks\\SecurityHeaders' => $baseDir . '/../lib/SetupChecks/SecurityHeaders.php',
     'OCA\\Settings\\SetupChecks\\SupportedDatabase' => $baseDir . '/../lib/SetupChecks/SupportedDatabase.php',
     'OCA\\Settings\\SetupChecks\\SystemIs64bit' => $baseDir . '/../lib/SetupChecks/SystemIs64bit.php',
index 1dccc69b923a57c5d6fd50fb98a278679b3cbc11..df8f985d7ab604de0619ee0989d13d4f2e3a2ac1 100644 (file)
@@ -132,6 +132,7 @@ class ComposerStaticInitSettings
         'OCA\\Settings\\SetupChecks\\PushService' => __DIR__ . '/..' . '/../lib/SetupChecks/PushService.php',
         'OCA\\Settings\\SetupChecks\\RandomnessSecure' => __DIR__ . '/..' . '/../lib/SetupChecks/RandomnessSecure.php',
         'OCA\\Settings\\SetupChecks\\ReadOnlyConfig' => __DIR__ . '/..' . '/../lib/SetupChecks/ReadOnlyConfig.php',
+        'OCA\\Settings\\SetupChecks\\SchedulingTableSize' => __DIR__ . '/..' . '/../lib/SetupChecks/SchedulingTableSize.php',
         'OCA\\Settings\\SetupChecks\\SecurityHeaders' => __DIR__ . '/..' . '/../lib/SetupChecks/SecurityHeaders.php',
         'OCA\\Settings\\SetupChecks\\SupportedDatabase' => __DIR__ . '/..' . '/../lib/SetupChecks/SupportedDatabase.php',
         'OCA\\Settings\\SetupChecks\\SystemIs64bit' => __DIR__ . '/..' . '/../lib/SetupChecks/SystemIs64bit.php',
index 9f7ec3036f43f4f9256981f78ce1f015ae37205f..e64dbfe4e84342739ca39dfede168a9cc1ba8be7 100644 (file)
@@ -86,6 +86,7 @@ use OCA\Settings\SetupChecks\PhpOutputBuffering;
 use OCA\Settings\SetupChecks\PushService;
 use OCA\Settings\SetupChecks\RandomnessSecure;
 use OCA\Settings\SetupChecks\ReadOnlyConfig;
+use OCA\Settings\SetupChecks\SchedulingTableSize;
 use OCA\Settings\SetupChecks\SecurityHeaders;
 use OCA\Settings\SetupChecks\SupportedDatabase;
 use OCA\Settings\SetupChecks\SystemIs64bit;
@@ -216,6 +217,7 @@ class Application extends App implements IBootstrap {
                $context->registerSetupCheck(RandomnessSecure::class);
                $context->registerSetupCheck(ReadOnlyConfig::class);
                $context->registerSetupCheck(SecurityHeaders::class);
+               $context->registerSetupCheck(SchedulingTableSize::class);
                $context->registerSetupCheck(SupportedDatabase::class);
                $context->registerSetupCheck(SystemIs64bit::class);
                $context->registerSetupCheck(TempSpaceAvailable::class);
diff --git a/apps/settings/lib/SetupChecks/SchedulingTableSize.php b/apps/settings/lib/SetupChecks/SchedulingTableSize.php
new file mode 100644 (file)
index 0000000..cf629f5
--- /dev/null
@@ -0,0 +1,48 @@
+<?php
+
+declare(strict_types=1);
+
+/**
+ * SPDX-FileCopyrightText: 2024 Nextcloud GmbH and Nextcloud contributors
+ * SPDX-License-Identifier: AGPL-3.0-or-later
+ */
+namespace OCA\Settings\SetupChecks;
+
+use OCP\IDBConnection;
+use OCP\IL10N;
+use OCP\SetupCheck\ISetupCheck;
+use OCP\SetupCheck\SetupResult;
+
+class SchedulingTableSize implements ISetupCheck {
+       public function __construct(
+               private IL10N $l10n,
+               private IDBConnection $connection,
+       ) {
+       }
+
+       public function getName(): string {
+               return $this->l10n->t('Scheduling objects table size');
+       }
+
+       public function getCategory(): string {
+               return 'database';
+       }
+
+       public function run(): SetupResult {
+               $qb = $this->connection->getQueryBuilder();
+               $qb->select($qb->func()->count('id'))
+                       ->from('schedulingobjects');
+               $query = $qb->executeQuery();
+               $count = $query->fetchOne();
+               $query->closeCursor();
+
+               if ($count > 500000) {
+                       return SetupResult::warning(
+                               $this->l10n->t('You have more than 500 000 rows in the scheduling objects table. Please run the expensive repair jobs via occ maintenance:repair --include-expensive')
+                       );
+               }
+               return SetupResult::success(
+                       $this->l10n->t('Scheduling objects table size is within acceptable range.')
+               );
+       }
+}
index a4e915caec2cbcde61b76f6d8d43273c204998a3..c25fb4fb6f4574ef9d39553b66d0876756c76ead 100644 (file)
@@ -191,6 +191,12 @@ class Application extends App {
                                ['principaluri']
                        );
 
+                       $event->addMissingIndex(
+                               'schedulingobjects',
+                               'schedulobj_lastmodified_idx',
+                               ['lastmodified']
+                       );
+
                        $event->addMissingIndex(
                                'properties',
                                'properties_path_index',
index b1800d6087d39f98575e46cbc94de7af12b5ad28..4a9cac9ff7d15e3187e4d2823ef4af71dc95d956 100644 (file)
@@ -81,6 +81,7 @@ use OC\Repair\RepairDavShares;
 use OC\Repair\RepairInvalidShares;
 use OC\Repair\RepairMimeTypes;
 use OC\Template\JSCombiner;
+use OCA\DAV\Migration\DeleteSchedulingObjects;
 use OCP\AppFramework\QueryException;
 use OCP\AppFramework\Utility\ITimeFactory;
 use OCP\Collaboration\Resources\IManager;
@@ -224,6 +225,7 @@ class Repair implements IOutput {
                return [
                        new OldGroupMembershipShares(\OC::$server->getDatabaseConnection(), \OC::$server->getGroupManager()),
                        \OC::$server->get(ValidatePhoneNumber::class),
+                       \OC::$server->get(DeleteSchedulingObjects::class),
                ];
        }