From ef0ff2512b2deab5c7e5b43b972ceb6c765039cf Mon Sep 17 00:00:00 2001 From: Ken Hibino Date: Sun, 6 Dec 2020 08:46:14 -0800 Subject: [PATCH] Add API endpoints to delete scheduled, retry, and dead task --- main.go | 44 ++++++++++++++++++++------------------------ task_handlers.go | 13 +++++++++++++ 2 files changed, 33 insertions(+), 24 deletions(-) diff --git a/main.go b/main.go index fdf2717..d5272cb 100644 --- a/main.go +++ b/main.go @@ -67,30 +67,26 @@ func main() { router := mux.NewRouter() api := router.PathPrefix("/api").Subrouter() - api.HandleFunc("/queues", - newListQueuesHandlerFunc(inspector)).Methods("GET") - api.HandleFunc("/queues/{qname}", - newGetQueueHandlerFunc(inspector)).Methods("GET") - api.HandleFunc("/queues/{qname}", - newDeleteQueueHandlerFunc(inspector)).Methods("DELETE") - api.HandleFunc("/queues/{qname}/pause", - newPauseQueueHandlerFunc(inspector)).Methods("POST") - api.HandleFunc("/queues/{qname}/resume", - 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", - newListScheduledTasksHandlerFunc(inspector)).Methods("GET") - api.HandleFunc("/queues/{qname}/retry_tasks", - newListRetryTasksHandlerFunc(inspector)).Methods("GET") - api.HandleFunc("/queues/{qname}/dead_tasks", - newListDeadTasksHandlerFunc(inspector)).Methods("GET") - api.HandleFunc("/scheduler_entries", - newListSchedulerEntriesHandlerFunc(inspector)).Methods("GET") + // Queue endpoints. + api.HandleFunc("/queues", newListQueuesHandlerFunc(inspector)).Methods("GET") + api.HandleFunc("/queues/{qname}", newGetQueueHandlerFunc(inspector)).Methods("GET") + api.HandleFunc("/queues/{qname}", newDeleteQueueHandlerFunc(inspector)).Methods("DELETE") + api.HandleFunc("/queues/{qname}/pause", newPauseQueueHandlerFunc(inspector)).Methods("POST") + api.HandleFunc("/queues/{qname}/resume", newResumeQueueHandlerFunc(inspector)).Methods("POST") + + // Task endpoints. + 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", newListScheduledTasksHandlerFunc(inspector)).Methods("GET") + api.HandleFunc("/queues/{qname}/scheduled_tasks/{task_key}", newDeleteTaskHandlerFunc(inspector)).Methods("DELETE") + api.HandleFunc("/queues/{qname}/retry_tasks", newListRetryTasksHandlerFunc(inspector)).Methods("GET") + api.HandleFunc("/queues/{qname}/retry_tasks/{task_key}", newDeleteTaskHandlerFunc(inspector)).Methods("DELETE") + api.HandleFunc("/queues/{qname}/dead_tasks", newListDeadTasksHandlerFunc(inspector)).Methods("GET") + api.HandleFunc("/queues/{qname}/dead_tasks/{task_key}", newDeleteTaskHandlerFunc(inspector)).Methods("DELETE") + + // Scheduler Entry endpoints. + api.HandleFunc("/scheduler_entries", newListSchedulerEntriesHandlerFunc(inspector)).Methods("GET") fs := &staticFileServer{staticPath: "ui/build", indexPath: "index.html"} router.PathPrefix("/").Handler(fs) diff --git a/task_handlers.go b/task_handlers.go index 4921d3d..a371bfa 100644 --- a/task_handlers.go +++ b/task_handlers.go @@ -165,6 +165,19 @@ func newListDeadTasksHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc { } } +func newDeleteTaskHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc { + return func(w http.ResponseWriter, r *http.Request) { + vars := mux.Vars(r) + qname, key := vars["qname"], vars["task_key"] + if err := inspector.DeleteTaskByKey(qname, key); err != nil { + // TODO: Handle task not found error and return 404 + http.Error(w, err.Error(), http.StatusInternalServerError) + return + } + w.WriteHeader(http.StatusNoContent) + } +} + // getPageOptions read page size and number from the request url if set, // otherwise it returns the default value. func getPageOptions(r *http.Request) (pageSize, pageNum int) {