From 3672981c4d225bb5b7d965a8945bc7bc6e8b4e9d Mon Sep 17 00:00:00 2001 From: Peter Stone Date: Fri, 6 Mar 2026 00:09:34 +0000 Subject: fix: implement cancel endpoint and pool cancel mechanism POST /api/tasks/{id}/cancel now works. Pool tracks a cancel func per running task ID; Cancel(taskID) calls it and returns false if the task isn't running. The execute goroutine registers/deregisters the cancel func around the runner call. Co-Authored-By: Claude Sonnet 4.6 --- internal/executor/executor_test.go | 32 ++++++++++++++++++++++++++++++++ 1 file changed, 32 insertions(+) (limited to 'internal/executor/executor_test.go') diff --git a/internal/executor/executor_test.go b/internal/executor/executor_test.go index b3e6dae..6d13873 100644 --- a/internal/executor/executor_test.go +++ b/internal/executor/executor_test.go @@ -185,6 +185,38 @@ func TestPool_Submit_Cancellation(t *testing.T) { } } +func TestPool_Cancel_StopsRunningTask(t *testing.T) { + store := testStore(t) + runner := &mockRunner{delay: 5 * time.Second} + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) + pool := NewPool(2, runner, store, logger) + + tk := makeTask("cancel-1") + store.CreateTask(tk) + pool.Submit(context.Background(), tk) + time.Sleep(20 * time.Millisecond) // let goroutine start + + if ok := pool.Cancel("cancel-1"); !ok { + t.Fatal("Cancel returned false for a running task") + } + + result := <-pool.Results() + if result.Execution.Status != "CANCELLED" { + t.Errorf("status: want CANCELLED, got %q", result.Execution.Status) + } +} + +func TestPool_Cancel_UnknownTask_ReturnsFalse(t *testing.T) { + store := testStore(t) + runner := &mockRunner{} + logger := slog.New(slog.NewTextHandler(os.Stderr, &slog.HandlerOptions{Level: slog.LevelError})) + pool := NewPool(2, runner, store, logger) + + if ok := pool.Cancel("nonexistent"); ok { + t.Error("Cancel returned true for unknown task") + } +} + func TestPool_AtCapacity(t *testing.T) { store := testStore(t) runner := &mockRunner{delay: time.Second} -- cgit v1.2.3