Make sync status non-blocking

This commit is contained in:
Mikhail Chusavitin
2026-03-17 18:34:28 +03:00
parent a054fc7564
commit b0a106415f
4 changed files with 78 additions and 78 deletions

View File

@@ -248,35 +248,20 @@ func (s *Service) GetStatus() (*SyncStatus, error) {
lastAttempt := s.localDB.GetLastPricelistSyncAttemptAt()
lastSyncStatus := s.localDB.GetLastPricelistSyncStatus()
lastSyncError := s.localDB.GetLastPricelistSyncError()
// Count server pricelists (only if already connected, don't reconnect)
serverCount := 0
connStatus := s.getConnectionStatus()
if connStatus.IsConnected {
if mariaDB, err := s.getDB(); err == nil && mariaDB != nil {
pricelistRepo := repository.NewPricelistRepository(mariaDB)
activeCount, err := pricelistRepo.CountActive()
if err == nil {
serverCount = int(activeCount)
}
}
}
// Count local pricelists
localCount := s.localDB.CountLocalPricelists()
needsSync, _ := s.NeedSync()
hasFailedSync := strings.EqualFold(lastSyncStatus, "failed")
needsSync := lastSync == nil || hasFailedSync
return &SyncStatus{
LastSyncAt: lastSync,
LastAttemptAt: lastAttempt,
LastSyncStatus: lastSyncStatus,
LastSyncError: lastSyncError,
ServerPricelists: serverCount,
ServerPricelists: 0,
LocalPricelists: int(localCount),
NeedsSync: needsSync,
IncompleteServerSync: needsSync && strings.EqualFold(lastSyncStatus, "failed"),
KnownServerChangesMiss: needsSync,
IncompleteServerSync: hasFailedSync,
KnownServerChangesMiss: hasFailedSync,
}, nil
}
@@ -447,11 +432,29 @@ func (s *Service) recordPricelistSyncFailure(syncErr error) {
if s.localDB == nil || syncErr == nil {
return
}
s.markConnectionBroken(syncErr)
if err := s.localDB.SetPricelistSyncResult("failed", syncErr.Error(), time.Now()); err != nil {
slog.Warn("failed to persist pricelist sync failure state", "error", err)
}
}
func (s *Service) markConnectionBroken(err error) {
if err == nil || s.connMgr == nil {
return
}
msg := strings.ToLower(err.Error())
switch {
case strings.Contains(msg, "i/o timeout"),
strings.Contains(msg, "invalid connection"),
strings.Contains(msg, "bad connection"),
strings.Contains(msg, "connection reset"),
strings.Contains(msg, "broken pipe"),
strings.Contains(msg, "unexpected eof"):
s.connMgr.MarkOffline(err)
}
}
func (s *Service) syncNewPricelistSnapshot(localPL *localdb.LocalPricelist) (int, error) {
if localPL == nil {
return 0, fmt.Errorf("local pricelist is nil")
@@ -990,6 +993,7 @@ func (s *Service) PushPendingChanges() (int, error) {
for _, change := range sortedChanges {
err := s.pushSingleChange(&change)
if err != nil {
s.markConnectionBroken(err)
slog.Warn("failed to push change", "id", change.ID, "type", change.EntityType, "operation", change.Operation, "error", err)
// Increment attempts
s.localDB.IncrementPendingChangeAttempts(change.ID, err.Error())