package sync import ( "context" "log/slog" "math/rand/v2" "time" "git.mchus.pro/mchus/quoteforge/internal/db" ) // WorkerJitter spreads background sync cycles by ±30% of the interval so that // clients started at the same time do not hit MariaDB in lockstep. const WorkerJitter = 0.3 // jitteredInterval returns base scaled by a random factor in [1-WorkerJitter, 1+WorkerJitter]. func jitteredInterval(base time.Duration) time.Duration { factor := 1 - WorkerJitter + rand.Float64()*2*WorkerJitter return time.Duration(float64(base) * factor) } // nextCycleDelay returns how long to wait so the next cycle starts a jittered // interval after started; zero if the cycle itself took longer than that. func nextCycleDelay(started time.Time, base time.Duration) time.Duration { return max(jitteredInterval(base)-time.Since(started), 0) } // Worker performs background synchronization at jittered intervals type Worker struct { service *Service connMgr *db.ConnectionManager interval time.Duration logger *slog.Logger stopCh chan struct{} } // NewWorker creates a new background sync worker func NewWorker(service *Service, connMgr *db.ConnectionManager, interval time.Duration) *Worker { return &Worker{ service: service, connMgr: connMgr, interval: interval, logger: slog.Default(), stopCh: make(chan struct{}), } } // isOnline checks if the database connection is available func (w *Worker) isOnline() bool { return w.connMgr.IsOnline() } // Start begins the background sync loop in a goroutine func (w *Worker) Start(ctx context.Context) { w.logger.Info("starting background sync worker", "interval", w.interval) // Run once immediately. The next cycle is scheduled from the start of the // previous one, so heartbeats are at most (1+WorkerJitter)*interval apart. started := time.Now() w.runSync() timer := time.NewTimer(nextCycleDelay(started, w.interval)) defer timer.Stop() for { select { case <-ctx.Done(): w.logger.Info("background sync worker stopped by context") return case <-w.stopCh: w.logger.Info("background sync worker stopped") return case <-timer.C: started = time.Now() w.runSync() timer.Reset(nextCycleDelay(started, w.interval)) } } } // Stop gracefully stops the worker func (w *Worker) Stop() { w.logger.Info("stopping background sync worker") close(w.stopCh) } // runSync performs a single sync iteration func (w *Worker) runSync() { // Check if online if !w.isOnline() { w.logger.Debug("offline, skipping background sync") return } // Each stream is independent: a failure in one must not stop the others. // Readiness only gates pushing local changes to the server. if readiness, err := w.service.EnsureReadinessForSync(); err != nil { w.logger.Warn("background sync: push blocked by readiness guard", "error", err, "reason_code", readiness.ReasonCode, "reason_text", readiness.ReasonText, ) } else if pushed, err := w.service.pushPendingChanges(); err != nil { w.logger.Warn("background sync: failed to push pending changes", "error", err) } else if pushed > 0 { w.logger.Info("background sync: pushed pending changes", "count", pushed) } if err := w.service.SyncPricelistsIfNeeded(); err != nil { w.logger.Warn("background sync: failed to sync pricelists", "error", err) } // Pull partnumber books together with pricelists if _, err := w.service.PullPartnumberBooks(); err != nil { w.logger.Warn("background sync: failed to pull partnumber books", "error", err) } // Pull configurator settings (tab_config, categories, etc.) if err := w.service.SyncQtSettings(); err != nil { w.logger.Warn("background sync: failed to sync qt_settings", "error", err) } w.logger.Info("background sync cycle completed") }