Add live activity progress and logs

This commit is contained in:
Mikei386
2026-06-14 14:26:13 +02:00
parent 5a1b9588e1
commit 3e3c63070e
14 changed files with 254 additions and 38 deletions
+47
View File
@@ -0,0 +1,47 @@
package queue
import (
"context"
"testing"
"time"
"github.com/backupper-unraid/backupper/internal/model"
)
func TestUpdateActiveIsVisibleWithoutPersistingIntermediateState(t *testing.T) {
started := make(chan struct{})
release := make(chan struct{})
persistCalls := 0
q := New(func(_ context.Context, run model.Run) model.Run {
close(started)
<-release
run.Status = "success"
return run
}, func([]model.Run) { persistCalls++ })
ctx, cancel := context.WithCancel(context.Background())
defer cancel()
go q.Run(ctx)
if err := q.Enqueue(model.Run{ID: "run", JobID: "job"}); err != nil {
t.Fatal(err)
}
<-started
before := persistCalls
if !q.UpdateActive("run", func(run *model.Run) { run.ProgressPercent = 42 }) {
t.Fatal("active run was not updated")
}
if runs := q.Snapshot(); len(runs) != 1 || runs[0].ProgressPercent != 42 {
t.Fatalf("runs = %#v", runs)
}
if persistCalls != before {
t.Fatal("intermediate progress was persisted")
}
close(release)
deadline := time.Now().Add(time.Second)
for time.Now().Before(deadline) {
if runs := q.Snapshot(); len(runs) == 1 && runs[0].Status == "success" {
return
}
time.Sleep(time.Millisecond)
}
t.Fatal("run did not finish")
}
+10
View File
@@ -141,6 +141,16 @@ func (q *Queue) Snapshot() []model.Run {
return q.snapshotLocked()
}
func (q *Queue) UpdateActive(id string, update func(*model.Run)) bool {
q.mu.Lock()
defer q.mu.Unlock()
if q.active == nil || q.active.ID != id {
return false
}
update(q.active)
return true
}
func (q *Queue) snapshotLocked() []model.Run {
result := append([]model.Run{}, q.history...)
if q.active != nil {