Add API endpoint to cancel active task

This commit is contained in:
Ken Hibino 2020-12-05 06:47:35 -08:00
parent 96eec9ce11
commit 47d8c73ab7
2 changed files with 13 additions and 0 deletions

View File

@ -79,6 +79,8 @@ func main() {
newResumeQueueHandlerFunc(inspector)).Methods("POST") newResumeQueueHandlerFunc(inspector)).Methods("POST")
api.HandleFunc("/queues/{qname}/active_tasks", api.HandleFunc("/queues/{qname}/active_tasks",
newListActiveTasksHandlerFunc(inspector)).Methods("GET") newListActiveTasksHandlerFunc(inspector)).Methods("GET")
api.HandleFunc("/queues/{qname}/active_tasks/{task_id}/cancel",
newCancelActiveTaskHandlerFunc(inspector)).Methods("POST")
api.HandleFunc("/queues/{qname}/pending_tasks", api.HandleFunc("/queues/{qname}/pending_tasks",
newListPendingTasksHandlerFunc(inspector)).Methods("GET") newListPendingTasksHandlerFunc(inspector)).Methods("GET")
api.HandleFunc("/queues/{qname}/scheduled_tasks", api.HandleFunc("/queues/{qname}/scheduled_tasks",

View File

@ -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 { func newListPendingTasksHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
return func(w http.ResponseWriter, r *http.Request) { return func(w http.ResponseWriter, r *http.Request) {
vars := mux.Vars(r) vars := mux.Vars(r)