From 47d8c73ab761a6c3e73b282ad1d99d74cc0aaad9 Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sat, 5 Dec 2020 06:47:35 -0800 Subject: [PATCH] Add API endpoint to cancel active task --- main.go | 2 ++ task_handlers.go | 11 +++++++++++ 2 files changed, 13 insertions(+) diff --git a/main.go b/main.go index 5c5fea3..fdf2717 100644 --- a/main.go +++ b/main.go @@ -79,6 +79,8 @@ func main() { newResumeQueueHandlerFunc(inspector)).Methods("POST") api.HandleFunc("/queues/{qname}/active_tasks", newListActiveTasksHandlerFunc(inspector)).Methods("GET") + api.HandleFunc("/queues/{qname}/active_tasks/{task_id}/cancel", + newCancelActiveTaskHandlerFunc(inspector)).Methods("POST") api.HandleFunc("/queues/{qname}/pending_tasks", newListPendingTasksHandlerFunc(inspector)).Methods("GET") api.HandleFunc("/queues/{qname}/scheduled_tasks", diff --git a/task_handlers.go b/task_handlers.go index 59e6de3..4921d3d 100644 --- a/task_handlers.go +++ b/task_handlers.go @@ -42,6 +42,17 @@ func newListActiveTasksHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc } } +func newCancelActiveTaskHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + id := mux.Vars(r)["task_id"] + if err := inspector.CancelActiveTask(id); err != nil { + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) + } +} + func newListPendingTasksHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc { return func(w http.ResponseWriter, r *http.Request) { vars := mux.Vars(r)