round4-client: surface last sync error per mount in status
Track lastErr per mount (mirroring lastSync pattern), cleared on
success so a recovered mount shows clean. Errors from LoadIgnoreWith,
Pull, and Sync all land in the map and surface through StateSnapshot
into 'devbox status' (human ⚠️ warning) and --json (last_err field).
This commit is contained in:
parent
b8b9bd1f22
commit
3ea8df85aa
4 changed files with 65 additions and 3 deletions
|
|
@ -775,6 +775,7 @@ type statusMount struct {
|
||||||
Pinned bool `json:"pinned"`
|
Pinned bool `json:"pinned"`
|
||||||
BaseSnapshot string `json:"base_snapshot,omitempty"`
|
BaseSnapshot string `json:"base_snapshot,omitempty"`
|
||||||
LastSyncUnix int64 `json:"last_sync_unix,omitempty"`
|
LastSyncUnix int64 `json:"last_sync_unix,omitempty"`
|
||||||
|
LastErr string `json:"last_err,omitempty"`
|
||||||
}
|
}
|
||||||
|
|
||||||
type statusJSON struct {
|
type statusJSON struct {
|
||||||
|
|
@ -818,7 +819,7 @@ func statusCmd() *cobra.Command {
|
||||||
s.Mounts = append(s.Mounts, statusMount{
|
s.Mounts = append(s.Mounts, statusMount{
|
||||||
Share: m.Share, Subpath: m.Subpath, Local: m.Local,
|
Share: m.Share, Subpath: m.Subpath, Local: m.Local,
|
||||||
ReadOnly: m.ReadOnly, Pinned: m.Pinned, BaseSnapshot: m.BaseSnapshot,
|
ReadOnly: m.ReadOnly, Pinned: m.Pinned, BaseSnapshot: m.BaseSnapshot,
|
||||||
LastSyncUnix: m.LastSyncUnix,
|
LastSyncUnix: m.LastSyncUnix, LastErr: m.LastErr,
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
|
|
@ -856,6 +857,9 @@ func statusCmd() *cobra.Command {
|
||||||
}
|
}
|
||||||
fmt.Fprintf(out, " - %s/%s -> %s [%s] @%s (%s)\n",
|
fmt.Fprintf(out, " - %s/%s -> %s [%s] @%s (%s)\n",
|
||||||
m.Share, m.Subpath, m.Local, mode, short(orNone(m.BaseSnapshot)), syncAge(m.LastSyncUnix))
|
m.Share, m.Subpath, m.Local, mode, short(orNone(m.BaseSnapshot)), syncAge(m.LastSyncUnix))
|
||||||
|
if m.LastErr != "" {
|
||||||
|
fmt.Fprintf(out, " ⚠️ last sync failed: %s\n", m.LastErr)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -41,6 +41,7 @@ type MountState struct {
|
||||||
Pinned bool `json:"pinned"`
|
Pinned bool `json:"pinned"`
|
||||||
BaseSnapshot string `json:"base_snapshot"`
|
BaseSnapshot string `json:"base_snapshot"`
|
||||||
LastSyncUnix int64 `json:"last_sync_unix"` // 0 = not synced since daemon start
|
LastSyncUnix int64 `json:"last_sync_unix"` // 0 = not synced since daemon start
|
||||||
|
LastErr string `json:"last_err,omitempty"` // last sync error message; empty = healthy
|
||||||
}
|
}
|
||||||
|
|
||||||
// Daemon is the slice of the running daemon the control server steers. The
|
// Daemon is the slice of the running daemon the control server steers. The
|
||||||
|
|
|
||||||
|
|
@ -50,6 +50,7 @@ type Daemon struct {
|
||||||
nudges map[string]func() // mountKey -> trigger a catch-up sync (for Resume)
|
nudges map[string]func() // mountKey -> trigger a catch-up sync (for Resume)
|
||||||
mounts map[string]config.Mount // mountKey -> mount config (for StateSnapshot)
|
mounts map[string]config.Mount // mountKey -> mount config (for StateSnapshot)
|
||||||
lastSync map[string]int64 // mountKey -> unix secs of last successful sync (0 = never)
|
lastSync map[string]int64 // mountKey -> unix secs of last successful sync (0 = never)
|
||||||
|
lastErr map[string]string // mountKey -> last sync error message (empty = healthy)
|
||||||
resumeTimer *time.Timer // pending auto-resume from PauseFor; nil if none
|
resumeTimer *time.Timer // pending auto-resume from PauseFor; nil if none
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -84,13 +85,23 @@ func New(dir string, cfg config.Daemon, host string, logf func(string, ...any))
|
||||||
nudges: map[string]func(){},
|
nudges: map[string]func(){},
|
||||||
mounts: map[string]config.Mount{},
|
mounts: map[string]config.Mount{},
|
||||||
lastSync: map[string]int64{},
|
lastSync: map[string]int64{},
|
||||||
|
lastErr: map[string]string{},
|
||||||
}, nil
|
}, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// recordSync stamps a mount's last successful sync time (for StateSnapshot / status).
|
// recordSync stamps a mount's last successful sync time and clears any prior error.
|
||||||
func (d *Daemon) recordSync(m config.Mount) {
|
func (d *Daemon) recordSync(m config.Mount) {
|
||||||
d.mu.Lock()
|
d.mu.Lock()
|
||||||
d.lastSync[mountKey(m)] = time.Now().Unix()
|
k := mountKey(m)
|
||||||
|
d.lastSync[k] = time.Now().Unix()
|
||||||
|
delete(d.lastErr, k)
|
||||||
|
d.mu.Unlock()
|
||||||
|
}
|
||||||
|
|
||||||
|
// recordErr stores the last sync error for a mount (surfaced via StateSnapshot / status).
|
||||||
|
func (d *Daemon) recordErr(m config.Mount, err error) {
|
||||||
|
d.mu.Lock()
|
||||||
|
d.lastErr[mountKey(m)] = err.Error()
|
||||||
d.mu.Unlock()
|
d.mu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -328,6 +339,7 @@ func (d *Daemon) StateSnapshot() control.State {
|
||||||
Pinned: m.Pinned,
|
Pinned: m.Pinned,
|
||||||
BaseSnapshot: d.state[k],
|
BaseSnapshot: d.state[k],
|
||||||
LastSyncUnix: d.lastSync[k],
|
LastSyncUnix: d.lastSync[k],
|
||||||
|
LastErr: d.lastErr[k],
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
return st
|
return st
|
||||||
|
|
@ -341,6 +353,7 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
|
||||||
ig, err := syncer.LoadIgnoreWith(m.Local, excludes)
|
ig, err := syncer.LoadIgnoreWith(m.Local, excludes)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logf("ignore %s: %v", m.Local, err)
|
d.logf("ignore %s: %v", m.Local, err)
|
||||||
|
d.recordErr(m, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
base := d.getBase(m)
|
base := d.getBase(m)
|
||||||
|
|
@ -351,6 +364,7 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
|
||||||
pr, err := syncer.Pull(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
|
pr, err := syncer.Pull(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logf("pull %s: %v", m.Share, err)
|
d.logf("pull %s: %v", m.Share, err)
|
||||||
|
d.recordErr(m, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d.setBase(m, pr.Base)
|
d.setBase(m, pr.Base)
|
||||||
|
|
@ -362,6 +376,7 @@ func (d *Daemon) syncMount(c *transport.Client, m config.Mount) {
|
||||||
newBase, pr, err := syncer.Sync(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
|
newBase, pr, err := syncer.Sync(c, m.Local, m.Share, m.Subpath, base, d.host, now, ig, d.guard, hk)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
d.logf("sync %s: %v", m.Share, err)
|
d.logf("sync %s: %v", m.Share, err)
|
||||||
|
d.recordErr(m, err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
d.setBase(m, newBase)
|
d.setBase(m, newBase)
|
||||||
|
|
|
||||||
|
|
@ -4,6 +4,7 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"crypto/ed25519"
|
"crypto/ed25519"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
|
"errors"
|
||||||
"net/http/httptest"
|
"net/http/httptest"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
|
@ -56,6 +57,47 @@ func waitFor(path, want string, d time.Duration) bool {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestRecordErrSurfacedAndCleared verifies that recordErr surfaces the error in
|
||||||
|
// StateSnapshot and that a subsequent recordSync clears it.
|
||||||
|
func TestRecordErrSurfacedAndCleared(t *testing.T) {
|
||||||
|
d, err := New(t.TempDir(), config.Daemon{}, "h", t.Logf)
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
m := config.Mount{Share: "s", Local: t.TempDir(), Hub: "http://hub"}
|
||||||
|
k := mountKey(m)
|
||||||
|
// Register so StateSnapshot emits this mount.
|
||||||
|
d.mu.Lock()
|
||||||
|
d.mounts[k] = m
|
||||||
|
d.mu.Unlock()
|
||||||
|
|
||||||
|
d.recordErr(m, errors.New("boom"))
|
||||||
|
|
||||||
|
snap := d.StateSnapshot()
|
||||||
|
var got string
|
||||||
|
for _, ms := range snap.Mounts {
|
||||||
|
if mountKey(config.Mount{Share: ms.Share, Subpath: ms.Subpath, Local: ms.Local}) == k {
|
||||||
|
got = ms.LastErr
|
||||||
|
}
|
||||||
|
}
|
||||||
|
if got != "boom" {
|
||||||
|
t.Fatalf("expected LastErr=boom, got %q", got)
|
||||||
|
}
|
||||||
|
|
||||||
|
d.recordSync(m)
|
||||||
|
snap = d.StateSnapshot()
|
||||||
|
for _, ms := range snap.Mounts {
|
||||||
|
if mountKey(config.Mount{Share: ms.Share, Subpath: ms.Subpath, Local: ms.Local}) == k {
|
||||||
|
if ms.LastErr != "" {
|
||||||
|
t.Fatalf("expected LastErr cleared after recordSync, got %q", ms.LastErr)
|
||||||
|
}
|
||||||
|
if ms.LastSyncUnix == 0 {
|
||||||
|
t.Fatal("expected LastSyncUnix != 0 after recordSync")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
// TestLiveTwoWaySyncViaSSE runs two daemons against one hub and proves a change
|
// TestLiveTwoWaySyncViaSSE runs two daemons against one hub and proves a change
|
||||||
// on one machine reaches the other automatically (push -> SSE event -> pull).
|
// on one machine reaches the other automatically (push -> SSE event -> pull).
|
||||||
func TestLiveTwoWaySyncViaSSE(t *testing.T) {
|
func TestLiveTwoWaySyncViaSSE(t *testing.T) {
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue