setInterval(300); $this->setTimeSensitivity(self::TIME_SENSITIVE); } protected function run(mixed $argument): void { $this->logger->info('Starting mail-calendar sync background job'); try { $configs = $this->configMapper->findAllEnabled(); $totalProcessed = 0; $totalUpdated = 0; $totalErrors = 0; $synced = 0; foreach ($configs as $config) { $userId = $config->getUserId(); $syncInterval = $config->getSyncInterval(); // Check per-user last sync time (stored in oc_preferences) $lastSyncStr = $this->appConfig->getUserValue( $userId, Application::APP_ID, 'last_synced_at', '0' ); $lastSync = (int)$lastSyncStr; if ((time() - $lastSync) < $syncInterval) { continue; // Not time yet for this user } try { $stats = $this->syncService->syncForUser($userId); $totalProcessed += $stats['processed']; $totalUpdated += $stats['updated']; $totalErrors += $stats['errors']; $synced++; // Record this sync time $this->appConfig->setUserValue( $userId, Application::APP_ID, 'last_synced_at', (string)time() ); } catch (\Throwable $e) { $totalErrors++; $this->logger->error('Sync failed for user', [ 'userId' => $userId, 'exception' => $e, ]); } } $this->logger->info('Mail-calendar sync completed', [ 'usersChecked' => count($configs), 'usersSynced' => $synced, 'processed' => $totalProcessed, 'updated' => $totalUpdated, 'errors' => $totalErrors, ]); // Periodic cleanup $this->logMapper->cleanupOld(30); $this->processedMapper->cleanupOld(90); } catch (\Throwable $e) { $this->logger->error('Mail-calendar sync background job failed', [ 'exception' => $e, ]); } } }