111 lines
3.6 KiB
PHP
111 lines
3.6 KiB
PHP
<?php
|
|
|
|
declare(strict_types=1);
|
|
|
|
namespace OCA\MailCalendarSync\BackgroundJob;
|
|
|
|
use OCA\MailCalendarSync\AppInfo\Application;
|
|
use OCA\MailCalendarSync\Db\ConfigMapper;
|
|
use OCA\MailCalendarSync\Db\LogMapper;
|
|
use OCA\MailCalendarSync\Db\ProcessedMessageMapper;
|
|
use OCA\MailCalendarSync\Service\SyncService;
|
|
use OCP\AppFramework\Utility\ITimeFactory;
|
|
use OCP\BackgroundJob\TimedJob;
|
|
use OCP\IConfig;
|
|
use Psr\Log\LoggerInterface;
|
|
|
|
/**
|
|
* Background job that scans for new iMIP calendar responses.
|
|
*
|
|
* Runs every 5 minutes (minimum Nextcloud interval), but respects
|
|
* each user's configured sync_interval via IConfig timestamps.
|
|
*/
|
|
class ProcessImipResponsesJob extends TimedJob {
|
|
|
|
public function __construct(
|
|
ITimeFactory $time,
|
|
private SyncService $syncService,
|
|
private ConfigMapper $configMapper,
|
|
private LogMapper $logMapper,
|
|
private ProcessedMessageMapper $processedMapper,
|
|
private IConfig $appConfig,
|
|
private LoggerInterface $logger,
|
|
) {
|
|
parent::__construct($time);
|
|
|
|
// Job wakes up every 5 minutes; per-user interval enforced in run()
|
|
$this->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,
|
|
]);
|
|
}
|
|
}
|
|
}
|