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; foreach ($configs as $config) { $userId = $config->getUserId(); // Check if enough time has passed since last sync for this user $syncInterval = $config->getSyncInterval(); $lastSync = $config->getUpdatedAt(); if ($lastSync !== null && $syncInterval > 300) { $lastSyncTime = strtotime($lastSync); if ($lastSyncTime !== false && (time() - $lastSyncTime) < $syncInterval) { continue; // Not time yet for this user } } try { $stats = $this->syncService->syncForUser($userId); $totalProcessed += $stats['processed']; $totalUpdated += $stats['updated']; $totalErrors += $stats['errors']; } catch (\Throwable $e) { $totalErrors++; $this->logger->error('Sync failed for user', [ 'userId' => $userId, 'exception' => $e, ]); } } $this->logger->info('Mail-calendar sync completed', [ 'users' => count($configs), 'processed' => $totalProcessed, 'updated' => $totalUpdated, 'errors' => $totalErrors, ]); // Periodic cleanup of old records $this->logMapper->cleanupOld(30); $this->processedMapper->cleanupOld(90); } catch (\Throwable $e) { $this->logger->error('Mail-calendar sync background job failed', [ 'exception' => $e, ]); } } }