diff --git a/bible-local/runtime-flows.md b/bible-local/runtime-flows.md index 65cedc4..6104772 100644 --- a/bible-local/runtime-flows.md +++ b/bible-local/runtime-flows.md @@ -75,6 +75,8 @@ Update this file in the same commit as any change to the flows below. - `POST /api/sync/push` drains the queue by writing to MariaDB. - If a push fails, `increment_attempts` and `last_error` are updated; row stays in queue. - `RepairPendingChanges` reconciles orphaned changes (configuration/project deleted locally). +- Background cycle (`sync.Worker.runSync`) runs independent streams: push (gated by readiness guard), then pricelists, partnumber books, qt_settings. A failure in one stream is logged and never stops the others. +- Configurations left `sync_status=pending` with no `pending_changes` row (queue row dropped, e.g. a stale event skipped without marking synced) are re-enqueued as an `update` with the current snapshot at the start of each push (`requeueOrphanedPendingConfigurations`). --- diff --git a/internal/localdb/localdb.go b/internal/localdb/localdb.go index 8077b8b..904fe8f 100644 --- a/internal/localdb/localdb.go +++ b/internal/localdb/localdb.go @@ -1862,6 +1862,17 @@ func (l *LocalDB) MarkChangesSynced(ids []int64) error { return l.db.Where("id IN ?", ids).Delete(&PendingChange{}).Error } +// ListConfigurationsPendingWithoutQueue returns configurations still marked pending +// whose pending_changes rows are gone, so nothing will ever push them. +func (l *LocalDB) ListConfigurationsPendingWithoutQueue() ([]LocalConfiguration, error) { + var configs []LocalConfiguration + err := l.db.Where( + "sync_status = ? AND uuid NOT IN (SELECT entity_uuid FROM pending_changes WHERE entity_type = ?)", + "pending", "configuration", + ).Find(&configs).Error + return configs, err +} + // PurgeOrphanConfigurationPendingChanges removes configuration pending changes // whose entity_uuid no longer exists in local_configurations. func (l *LocalDB) PurgeOrphanConfigurationPendingChanges() (int64, error) { diff --git a/internal/services/sync/service.go b/internal/services/sync/service.go index b8ab179..394300a 100644 --- a/internal/services/sync/service.go +++ b/internal/services/sync/service.go @@ -957,6 +957,8 @@ func (s *Service) PushPendingChanges() (int, error) { slog.Info("auto-repaired errored pending changes", "repaired", repaired) } + s.requeueOrphanedPendingConfigurations() + changes, err := s.localDB.GetPendingChanges() if err != nil { return 0, fmt.Errorf("getting pending changes: %w", err) @@ -1019,6 +1021,63 @@ func (s *Service) PushPendingChanges() (int, error) { return pushed, nil } +// requeueOrphanedPendingConfigurations re-enqueues an update for configurations that are +// still sync_status=pending but have no queue row left (the row was dropped, e.g. a stale +// event was skipped without marking the config synced). The update carries the current +// local snapshot and version, so the server receives the latest state. +func (s *Service) requeueOrphanedPendingConfigurations() int { + orphans, err := s.localDB.ListConfigurationsPendingWithoutQueue() + if err != nil { + slog.Warn("failed to list orphaned pending configurations", "error", err) + return 0 + } + + requeued := 0 + for _, orphan := range orphans { + cfg, versionID, versionNo, err := s.loadCurrentConfigurationState(orphan.UUID) + if err != nil { + slog.Warn("cannot requeue orphaned pending configuration", "uuid", orphan.UUID, "error", err) + continue + } + + now := time.Now().UTC() + payload := ConfigurationChangePayload{ + EventID: uuid.New().String(), + IdempotencyKey: fmt.Sprintf("%s:v%d:requeue", orphan.UUID, versionNo), + ConfigurationUUID: orphan.UUID, + ProjectUUID: cfg.ProjectUUID, + PricelistID: cfg.PricelistID, + Operation: "update", + CurrentVersionID: versionID, + CurrentVersionNo: versionNo, + ConflictPolicy: "last_write_wins", + Snapshot: cfg, + CreatedAt: now, + } + raw, err := json.Marshal(payload) + if err != nil { + slog.Warn("cannot marshal requeued configuration payload", "uuid", orphan.UUID, "error", err) + continue + } + if err := s.localDB.DB().Create(&localdb.PendingChange{ + EntityType: "configuration", + EntityUUID: orphan.UUID, + Operation: "update", + Payload: string(raw), + CreatedAt: now, + }).Error; err != nil { + slog.Warn("cannot enqueue requeued configuration", "uuid", orphan.UUID, "error", err) + continue + } + requeued++ + } + + if requeued > 0 { + slog.Info("requeued orphaned pending configurations", "requeued", requeued) + } + return requeued +} + // pushSingleChange pushes a single pending change to the server func (s *Service) pushSingleChange(change *localdb.PendingChange) error { switch change.EntityType { diff --git a/internal/services/sync/service_orphaned_pending_test.go b/internal/services/sync/service_orphaned_pending_test.go new file mode 100644 index 0000000..b5cb898 --- /dev/null +++ b/internal/services/sync/service_orphaned_pending_test.go @@ -0,0 +1,66 @@ +package sync_test + +import ( + "testing" + + "git.mchus.pro/mchus/quoteforge/internal/localdb" + "git.mchus.pro/mchus/quoteforge/internal/models" + "git.mchus.pro/mchus/quoteforge/internal/services" + syncsvc "git.mchus.pro/mchus/quoteforge/internal/services/sync" +) + +// Reproduces the state seen in the field bundle: configuration stays sync_status=pending +// while its pending_changes rows are gone. Push must not leave it pending forever. +func TestPushPendingChangesRequeuesOrphanedPendingConfiguration(t *testing.T) { + local := newLocalDBForSyncTest(t) + serverDB := newServerDBForSyncTest(t) + + localSync := syncsvc.NewService(nil, local) + configService := services.NewLocalConfigurationService(local, localSync, &services.QuoteService{}, func() bool { return false }) + pushService := syncsvc.NewServiceWithDB(serverDB, local) + + created, err := configService.Create("tester", &services.CreateConfigRequest{ + Name: "Orphan v1", + Items: models.ConfigItems{{LotName: "CPU_O", Quantity: 1, UnitPrice: 500}}, + ServerCount: 1, + }) + if err != nil { + t.Fatalf("create config: %v", err) + } + if _, err := configService.UpdateNoAuth(created.UUID, &services.CreateConfigRequest{ + Name: "Orphan v2", + Items: models.ConfigItems{{LotName: "CPU_O", Quantity: 2, UnitPrice: 500}}, + ServerCount: 1, + ProjectUUID: created.ProjectUUID, + }); err != nil { + t.Fatalf("update config: %v", err) + } + + // Simulate the dropped queue: rows are gone, local config still marked pending. + if err := local.DB().Where("1 = 1").Delete(&localdb.PendingChange{}).Error; err != nil { + t.Fatalf("clear pending changes: %v", err) + } + + if _, err := pushService.PushPendingChanges(); err != nil { + t.Fatalf("push: %v", err) + } + + var serverCfg models.Configuration + if err := serverDB.Where("uuid = ?", created.UUID).First(&serverCfg).Error; err != nil { + t.Fatalf("configuration not on server: %v", err) + } + if serverCfg.Name != "Orphan v2" { + t.Fatalf("expected latest version on server, got %q", serverCfg.Name) + } + + localCfg, err := local.GetConfigurationByUUID(created.UUID) + if err != nil { + t.Fatalf("get local config: %v", err) + } + if localCfg.SyncStatus != "synced" { + t.Fatalf("expected sync_status synced, got %q", localCfg.SyncStatus) + } + if got := local.CountPendingChanges(); got != 0 { + t.Fatalf("expected empty pending queue, got %d", got) + } +} diff --git a/internal/services/sync/worker.go b/internal/services/sync/worker.go index c175b6b..2f4f3d8 100644 --- a/internal/services/sync/worker.go +++ b/internal/services/sync/worker.go @@ -71,28 +71,22 @@ func (w *Worker) runSync() { 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: blocked by readiness guard", + w.logger.Warn("background sync: push blocked by readiness guard", "error", err, "reason_code", readiness.ReasonCode, "reason_text", readiness.ReasonText, ) - return - } - - // Push pending changes first - pushed, err := w.service.PushPendingChanges() - if err != nil { + } 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) } - // Then check for new pricelists - err = w.service.SyncPricelistsIfNeeded() - if err != nil { + if err := w.service.SyncPricelistsIfNeeded(); err != nil { w.logger.Warn("background sync: failed to sync pricelists", "error", err) - return } // Pull partnumber books together with pricelists