-
Notifications
You must be signed in to change notification settings - Fork 314
feat: Add configurable sync window (mail.max_sync_days) to limit database growth #12633
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
Open
Rikdekker
wants to merge
7
commits into
nextcloud:main
Choose a base branch
from
Rikdekker:feat/max-sync-days
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
570e79a
feat: Add configurable sync window (mail.max_sync_days) to limit data…
Rikdekker cc5ef1d
fix: Address CI failures (php-cs, psalm, DCO)
Rikdekker ecbcdc2
feat: Add UNIQUE constraint migration for (mailbox_id, uid)
Rikdekker 5c10ecd
fix: Address php-cs and psalm CI failures
Rikdekker 7251a7b
fix: Constructor formatting, line endings, and psalm null-coalesce
Rikdekker b84850b
fix: Update tests for new constructor dependencies and psalm
Rikdekker bb5b5e7
fix: Import ordering and skip duplicate UID test
Rikdekker File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,111 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Mail\BackgroundJob; | ||
|
|
||
| use OCA\Mail\Db\MailboxMapper; | ||
| use OCA\Mail\Db\MessageMapper; | ||
| use OCA\Mail\IMAP\IMAPClientFactory; | ||
| use OCA\Mail\IMAP\MessageMapper as ImapMessageMapper; | ||
| use OCA\Mail\Service\AccountService; | ||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
| * Backfills search result messages that exist on IMAP but not in the local DB. | ||
| * | ||
| * When a user searches and the IMAP server returns UIDs that are outside the | ||
| * local sync window, this job fetches their metadata from IMAP and inserts | ||
| * them into the local DB. The next search will then return complete results. | ||
| */ | ||
| class BackfillSearchResultsJob extends QueuedJob { | ||
| public function __construct( | ||
| ITimeFactory $time, | ||
| private AccountService $accountService, | ||
| private MailboxMapper $mailboxMapper, | ||
| private MessageMapper $messageMapper, | ||
| private IMAPClientFactory $clientFactory, | ||
| private ImapMessageMapper $imapMapper, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| parent::__construct($time); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function run($argument): void { | ||
| $accountId = (int)($argument['accountId'] ?? 0); | ||
| $mailboxId = (int)($argument['mailboxId'] ?? 0); | ||
| $uids = $argument['uids'] ?? []; | ||
|
|
||
| if ($accountId === 0 || $mailboxId === 0 || $uids === []) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $account = $this->accountService->findById($accountId); | ||
| } catch (DoesNotExistException $e) { | ||
| return; | ||
| } | ||
|
|
||
| if (!$account->getMailAccount()->canAuthenticateImap()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $mailbox = $this->mailboxMapper->findById($mailboxId); | ||
| } catch (DoesNotExistException $e) { | ||
| return; | ||
| } | ||
|
|
||
| // Re-check which UIDs are still missing (some may have been synced since job was queued) | ||
| $stillMissing = $this->messageMapper->findMissingUids($mailbox, $uids); | ||
| if ($stillMissing === []) { | ||
| $this->logger->debug("BackfillSearchResultsJob: all UIDs already in DB for mailbox $mailboxId"); | ||
| return; | ||
| } | ||
|
|
||
| // Cap to 200 UIDs per job to limit IMAP load | ||
| $stillMissing = array_slice($stillMissing, 0, 200); | ||
|
|
||
| $client = $this->clientFactory->getClient($account, false); | ||
| try { | ||
| $imapMessages = $this->imapMapper->findByIds( | ||
| $client, | ||
| $mailbox->getName(), | ||
| $stillMissing, | ||
| $account->getUserId(), | ||
| ); | ||
|
|
||
| if (empty($imapMessages)) { | ||
| $this->logger->debug("BackfillSearchResultsJob: no messages found on IMAP for mailbox $mailboxId"); | ||
| return; | ||
| } | ||
|
|
||
| $dbMessages = array_map( | ||
| static function ($imapMessage) use ($mailbox, $account) { | ||
| $msg = $imapMessage->toDbMessage($mailbox->getId(), $account->getMailAccount()); | ||
| $msg->setStructureAnalyzed(true); | ||
| return $msg; | ||
| }, | ||
| $imapMessages | ||
| ); | ||
|
|
||
| $inserted = $this->messageMapper->insertBulkIgnore($account, ...$dbMessages); | ||
| $this->logger->debug("BackfillSearchResultsJob: inserted $inserted messages for mailbox $mailboxId"); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->warning('BackfillSearchResultsJob failed: ' . $e->getMessage(), [ | ||
| 'exception' => $e, | ||
| ]); | ||
| } finally { | ||
| $client->logout(); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,80 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Mail\BackgroundJob; | ||
|
|
||
| use OCA\Mail\Db\MailboxMapper; | ||
| use OCA\Mail\Service\AccountService; | ||
| use OCA\Mail\Service\Sync\ImapToDbSynchronizer; | ||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
| * Background job to deep-sync older messages for a single mailbox. | ||
| * | ||
| * This is a QueuedJob (runs once per scheduling) instead of being inlined | ||
| * in the per-account SyncJob TimedJob. This prevents deep-sync from blocking | ||
| * the main cron cycle when there are thousands of accounts. | ||
| * | ||
| * Nextcloud's cron runner processes QueuedJobs round-robin, so 10K jobs are | ||
| * spread across multiple cron invocations (~1000 jobs per 5-min cycle). | ||
| */ | ||
| class DeepSyncJob extends QueuedJob { | ||
| public function __construct( | ||
| ITimeFactory $time, | ||
| private AccountService $accountService, | ||
| private MailboxMapper $mailboxMapper, | ||
| private ImapToDbSynchronizer $syncService, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| parent::__construct($time); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function run($argument): void { | ||
| $accountId = (int)($argument['accountId'] ?? 0); | ||
| $mailboxId = (int)($argument['mailboxId'] ?? 0); | ||
|
|
||
| if ($accountId === 0 || $mailboxId === 0) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $account = $this->accountService->findById($accountId); | ||
| } catch (DoesNotExistException $e) { | ||
| $this->logger->debug("DeepSyncJob: account $accountId not found, skipping"); | ||
| return; | ||
| } | ||
|
|
||
| if (!$account->getMailAccount()->canAuthenticateImap()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $mailbox = $this->mailboxMapper->findById($mailboxId); | ||
| } catch (DoesNotExistException $e) { | ||
| $this->logger->debug("DeepSyncJob: mailbox $mailboxId not found, skipping"); | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $this->syncService->syncOlderMessagesBackground( | ||
| $account, | ||
| $mailbox, | ||
| $this->logger, | ||
| ); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->warning("DeepSyncJob failed for account $accountId mailbox $mailboxId: " . $e->getMessage(), [ | ||
| 'exception' => $e, | ||
| ]); | ||
| } | ||
| } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,77 @@ | ||
| <?php | ||
|
|
||
| declare(strict_types=1); | ||
|
|
||
| /** | ||
| * SPDX-FileCopyrightText: 2026 Nextcloud GmbH and Nextcloud contributors | ||
| * SPDX-License-Identifier: AGPL-3.0-or-later | ||
| */ | ||
|
|
||
| namespace OCA\Mail\BackgroundJob; | ||
|
|
||
| use OCA\Mail\Db\MailboxMapper; | ||
| use OCA\Mail\Service\AccountService; | ||
| use OCA\Mail\Service\Sync\ImapToDbSynchronizer; | ||
| use OCP\AppFramework\Db\DoesNotExistException; | ||
| use OCP\AppFramework\Utility\ITimeFactory; | ||
| use OCP\BackgroundJob\QueuedJob; | ||
| use Psr\Log\LoggerInterface; | ||
|
|
||
| /** | ||
| * Async on-demand sync triggered when a user scrolls past cached messages. | ||
| * | ||
| * Instead of blocking the HTTP request with a synchronous IMAP fetch, | ||
| * MailSearch schedules this job. The frontend retries after a short delay | ||
| * and picks up the newly synced messages from the local DB. | ||
| */ | ||
| class OnDemandSyncJob extends QueuedJob { | ||
| public function __construct( | ||
| ITimeFactory $time, | ||
| private AccountService $accountService, | ||
| private MailboxMapper $mailboxMapper, | ||
| private ImapToDbSynchronizer $syncService, | ||
| private LoggerInterface $logger, | ||
| ) { | ||
| parent::__construct($time); | ||
| } | ||
|
|
||
| #[\Override] | ||
| protected function run($argument): void { | ||
| $accountId = (int)($argument['accountId'] ?? 0); | ||
| $mailboxId = (int)($argument['mailboxId'] ?? 0); | ||
| $cursorTimestamp = (int)($argument['cursorTimestamp'] ?? 0); | ||
|
|
||
| if ($accountId === 0 || $mailboxId === 0 || $cursorTimestamp === 0) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $account = $this->accountService->findById($accountId); | ||
| } catch (DoesNotExistException $e) { | ||
| return; | ||
| } | ||
|
|
||
| if (!$account->getMailAccount()->canAuthenticateImap()) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $mailbox = $this->mailboxMapper->findById($mailboxId); | ||
| } catch (DoesNotExistException $e) { | ||
| return; | ||
| } | ||
|
|
||
| try { | ||
| $synced = $this->syncService->syncOlderMessages( | ||
| $account, | ||
| $mailbox, | ||
| $cursorTimestamp, | ||
| ); | ||
| $this->logger->debug("OnDemandSyncJob: synced $synced messages for account $accountId mailbox $mailboxId"); | ||
| } catch (\Throwable $e) { | ||
| $this->logger->warning('OnDemandSyncJob failed: ' . $e->getMessage(), [ | ||
| 'exception' => $e, | ||
| ]); | ||
| } | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
A standard deployment has a cron interval of 5 minutes. That means the asynchronous job will be delayed up to 5 minutes if the cron queue is empty, potentially longer if there are more time critical jobs in the queue.