Add activity history clearing
This commit is contained in:
@@ -151,6 +151,15 @@ func (q *Queue) UpdateActive(id string, update func(*model.Run)) bool {
|
||||
return true
|
||||
}
|
||||
|
||||
func (q *Queue) ClearHistory() int {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
count := len(q.history)
|
||||
q.history = nil
|
||||
q.emitLocked()
|
||||
return count
|
||||
}
|
||||
|
||||
func (q *Queue) snapshotLocked() []model.Run {
|
||||
result := append([]model.Run{}, q.history...)
|
||||
if q.active != nil {
|
||||
|
||||
@@ -28,3 +28,34 @@ func TestQueueDeduplicatesJobAndRunsTask(t *testing.T) {
|
||||
}
|
||||
q.Stop()
|
||||
}
|
||||
|
||||
func TestClearHistoryKeepsActiveAndPendingRuns(t *testing.T) {
|
||||
started := make(chan struct{})
|
||||
release := make(chan struct{})
|
||||
q := New(func(_ context.Context, run model.Run) model.Run {
|
||||
close(started)
|
||||
<-release
|
||||
run.Status = "success"
|
||||
return run
|
||||
}, nil)
|
||||
q.RestoreHistory([]model.Run{{ID: "old", Status: "success"}})
|
||||
ctx, cancel := context.WithCancel(context.Background())
|
||||
defer cancel()
|
||||
go q.Run(ctx)
|
||||
if err := q.Enqueue(model.Run{ID: "active", JobID: "active-job"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
<-started
|
||||
if err := q.Enqueue(model.Run{ID: "pending", JobID: "pending-job"}); err != nil {
|
||||
t.Fatal(err)
|
||||
}
|
||||
if cleared := q.ClearHistory(); cleared != 1 {
|
||||
t.Fatalf("cleared = %d", cleared)
|
||||
}
|
||||
runs := q.Snapshot()
|
||||
if len(runs) != 2 || runs[0].ID != "active" || runs[1].ID != "pending" {
|
||||
t.Fatalf("runs = %#v", runs)
|
||||
}
|
||||
q.Stop()
|
||||
close(release)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user