import { Injectable, Logger } from '@nestjs/common'; import { ConfigService } from '@nestjs/config'; import { Interval } from '@nestjs/schedule'; import { SyncService } from './sync.service'; /// Periodically pushes/pulls against the configured peer when enabled. Safe /// to fail silently (e.g. no internet at an on-site event) - just retries /// on the next tick. @Injectable() export class SyncSchedulerService { private readonly logger = new Logger(SyncSchedulerService.name); constructor( private readonly sync: SyncService, private readonly config: ConfigService, ) {} @Interval(30_000) async tick() { if (this.config.get('SYNC_ENABLED') !== 'true') return; const peerUrl = this.config.get('SYNC_PEER_URL'); const peerSecret = this.config.get('SYNC_SHARED_SECRET'); if (!peerUrl || !peerSecret) return; try { await this.sync.pushToPeer(peerUrl, peerSecret); await this.sync.pullFromPeer(peerUrl, peerSecret); } catch (err) { this.logger.debug(`Sync with peer skipped: ${(err as Error).message}`); } } }