Add pause resume and abort controls
This commit is contained in:
+20
-1
@@ -37,7 +37,7 @@ func (q *Queue) RestoreHistory(runs []model.Run) {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
for _, run := range runs {
|
||||
if run.Status == "queued" || run.Status == "running" {
|
||||
if run.Status == "queued" || run.Status == "running" || run.Status == "paused" {
|
||||
run.Status = "failed"
|
||||
run.ErrorCode = "environment"
|
||||
run.Message = "daemon restarted while task was active"
|
||||
@@ -125,6 +125,25 @@ func (q *Queue) Cancel(id string) bool {
|
||||
return false
|
||||
}
|
||||
|
||||
func (q *Queue) SetPaused(id string, paused bool) bool {
|
||||
q.mu.Lock()
|
||||
defer q.mu.Unlock()
|
||||
if q.active == nil || q.active.ID != id {
|
||||
return false
|
||||
}
|
||||
expected := "running"
|
||||
next := "paused"
|
||||
if !paused {
|
||||
expected, next = "paused", "running"
|
||||
}
|
||||
if q.active.Status != expected {
|
||||
return false
|
||||
}
|
||||
q.active.Status = next
|
||||
q.emitLocked()
|
||||
return true
|
||||
}
|
||||
|
||||
func (q *Queue) Stop() {
|
||||
q.mu.Lock()
|
||||
q.stopping = true
|
||||
|
||||
@@ -59,3 +59,34 @@ func TestClearHistoryKeepsActiveAndPendingRuns(t *testing.T) {
|
||||
q.Stop()
|
||||
close(release)
|
||||
}
|
||||
|
||||
func TestPauseAndResumeActiveRun(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)
|
||||
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
|
||||
if !q.SetPaused("run", true) {
|
||||
t.Fatal("active run could not be paused")
|
||||
}
|
||||
if runs := q.Snapshot(); len(runs) != 1 || runs[0].Status != "paused" {
|
||||
t.Fatalf("paused runs = %#v", runs)
|
||||
}
|
||||
if !q.SetPaused("run", false) {
|
||||
t.Fatal("paused run could not be resumed")
|
||||
}
|
||||
if runs := q.Snapshot(); len(runs) != 1 || runs[0].Status != "running" {
|
||||
t.Fatalf("resumed runs = %#v", runs)
|
||||
}
|
||||
close(release)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user