fix: requeue orphaned pending configurations; independent background sync streams
- Configurations stuck in sync_status=pending without a pending_changes row are re-enqueued as an update with the current snapshot on each push cycle. - Background cycle no longer aborts pricelist/partbook/settings sync when push or readiness fails; each stream logs its own error. Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
This commit is contained in:
1 parent
310b296045
commit
cccb8890a4
5 files changed
+143
-11
No files matched your search
@@ -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 {
|
||||
|
||||
@@ -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)
|
||||
}
|
||||
}
|
||||
@@ -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
|
||||
|
||||
Reference in new issue
Block a user