2021-09-18 20:25:59 +08:00
|
|
|
package asynqmon
|
2020-11-24 22:54:00 +08:00
|
|
|
|
|
|
|
import (
|
2021-10-01 02:26:01 +08:00
|
|
|
"github.com/go-redis/redis/v8"
|
2020-11-24 22:54:00 +08:00
|
|
|
"github.com/gorilla/mux"
|
2021-10-01 02:23:01 +08:00
|
|
|
"net/http"
|
2021-09-18 22:23:42 +08:00
|
|
|
|
2020-11-24 22:54:00 +08:00
|
|
|
"github.com/hibiken/asynq"
|
2021-01-08 23:16:48 +08:00
|
|
|
)
|
|
|
|
|
2021-10-01 02:23:01 +08:00
|
|
|
type HandlerOptions struct {
|
|
|
|
RedisClient redis.UniversalClient
|
|
|
|
Inspector *asynq.Inspector
|
|
|
|
Middlewares []mux.MiddlewareFunc
|
|
|
|
BytesStringer BytesStringer
|
|
|
|
StaticContentHandler http.Handler
|
2021-04-24 10:58:32 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:23:01 +08:00
|
|
|
func NewHandler(opts HandlerOptions) http.Handler {
|
2021-09-18 20:25:59 +08:00
|
|
|
router := mux.NewRouter()
|
|
|
|
inspector := opts.Inspector
|
2021-10-01 02:09:41 +08:00
|
|
|
t := &transformer{bs: defaultBytesStringer}
|
|
|
|
if opts.BytesStringer != nil {
|
|
|
|
t = &transformer{bs: opts.BytesStringer}
|
2021-09-18 20:26:00 +08:00
|
|
|
}
|
2020-11-24 22:54:00 +08:00
|
|
|
|
2021-09-18 20:25:59 +08:00
|
|
|
for _, mf := range opts.Middlewares {
|
|
|
|
router.Use(mf)
|
2021-09-06 21:32:23 +08:00
|
|
|
}
|
2020-11-24 22:54:00 +08:00
|
|
|
|
|
|
|
api := router.PathPrefix("/api").Subrouter()
|
2020-12-07 00:46:14 +08:00
|
|
|
// Queue endpoints.
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queues", newListQueuesHandlerFunc(inspector, t)).Methods("GET")
|
|
|
|
api.HandleFunc("/queues/{qname}", newGetQueueHandlerFunc(inspector, t)).Methods("GET")
|
|
|
|
api.HandleFunc("/queues/{qname}", newDeleteQueueHandlerFunc(inspector, t)).Methods("DELETE")
|
2020-12-07 22:41:16 +08:00
|
|
|
api.HandleFunc("/queues/{qname}:pause", newPauseQueueHandlerFunc(inspector)).Methods("POST")
|
|
|
|
api.HandleFunc("/queues/{qname}:resume", newResumeQueueHandlerFunc(inspector)).Methods("POST")
|
2020-12-07 00:46:14 +08:00
|
|
|
|
2020-12-28 07:45:54 +08:00
|
|
|
// Queue Historical Stats endpoint.
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queue_stats", newListQueueStatsHandlerFunc(inspector, t)).Methods("GET")
|
2020-12-28 07:45:54 +08:00
|
|
|
|
2020-12-07 00:46:14 +08:00
|
|
|
// Task endpoints.
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/active_tasks", newListActiveTasksHandlerFunc(inspector, t)).Methods("GET")
|
2020-12-07 22:41:16 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/active_tasks/{task_id}:cancel", newCancelActiveTaskHandlerFunc(inspector)).Methods("POST")
|
2020-12-23 22:59:44 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/active_tasks:cancel_all", newCancelAllActiveTasksHandlerFunc(inspector)).Methods("POST")
|
2020-12-23 22:23:15 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/active_tasks:batch_cancel", newBatchCancelActiveTasksHandlerFunc(inspector)).Methods("POST")
|
2021-01-21 13:30:27 +08:00
|
|
|
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/pending_tasks", newListPendingTasksHandlerFunc(inspector, t)).Methods("GET")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/pending_tasks/{task_id}", newDeleteTaskHandlerFunc(inspector)).Methods("DELETE")
|
2021-01-21 13:30:27 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/pending_tasks:delete_all", newDeleteAllPendingTasksHandlerFunc(inspector)).Methods("DELETE")
|
|
|
|
api.HandleFunc("/queues/{qname}/pending_tasks:batch_delete", newBatchDeleteTasksHandlerFunc(inspector)).Methods("POST")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/pending_tasks/{task_id}:archive", newArchiveTaskHandlerFunc(inspector)).Methods("POST")
|
2021-01-21 13:30:27 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/pending_tasks:archive_all", newArchiveAllPendingTasksHandlerFunc(inspector)).Methods("POST")
|
|
|
|
api.HandleFunc("/queues/{qname}/pending_tasks:batch_archive", newBatchArchiveTasksHandlerFunc(inspector)).Methods("POST")
|
|
|
|
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks", newListScheduledTasksHandlerFunc(inspector, t)).Methods("GET")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks/{task_id}", newDeleteTaskHandlerFunc(inspector)).Methods("DELETE")
|
2020-12-07 23:22:04 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks:delete_all", newDeleteAllScheduledTasksHandlerFunc(inspector)).Methods("DELETE")
|
2020-12-18 08:05:22 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks:batch_delete", newBatchDeleteTasksHandlerFunc(inspector)).Methods("POST")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks/{task_id}:run", newRunTaskHandlerFunc(inspector)).Methods("POST")
|
2020-12-19 22:07:23 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks:run_all", newRunAllScheduledTasksHandlerFunc(inspector)).Methods("POST")
|
2020-12-15 22:16:58 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks:batch_run", newBatchRunTasksHandlerFunc(inspector)).Methods("POST")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks/{task_id}:archive", newArchiveTaskHandlerFunc(inspector)).Methods("POST")
|
2021-01-13 03:59:44 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks:archive_all", newArchiveAllScheduledTasksHandlerFunc(inspector)).Methods("POST")
|
|
|
|
api.HandleFunc("/queues/{qname}/scheduled_tasks:batch_archive", newBatchArchiveTasksHandlerFunc(inspector)).Methods("POST")
|
2021-01-21 13:30:27 +08:00
|
|
|
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks", newListRetryTasksHandlerFunc(inspector, t)).Methods("GET")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks/{task_id}", newDeleteTaskHandlerFunc(inspector)).Methods("DELETE")
|
2020-12-07 23:22:04 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks:delete_all", newDeleteAllRetryTasksHandlerFunc(inspector)).Methods("DELETE")
|
2020-12-18 08:05:22 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks:batch_delete", newBatchDeleteTasksHandlerFunc(inspector)).Methods("POST")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks/{task_id}:run", newRunTaskHandlerFunc(inspector)).Methods("POST")
|
2020-12-19 22:07:23 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks:run_all", newRunAllRetryTasksHandlerFunc(inspector)).Methods("POST")
|
2020-12-15 22:16:58 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks:batch_run", newBatchRunTasksHandlerFunc(inspector)).Methods("POST")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks/{task_id}:archive", newArchiveTaskHandlerFunc(inspector)).Methods("POST")
|
2021-01-13 03:59:44 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks:archive_all", newArchiveAllRetryTasksHandlerFunc(inspector)).Methods("POST")
|
|
|
|
api.HandleFunc("/queues/{qname}/retry_tasks:batch_archive", newBatchArchiveTasksHandlerFunc(inspector)).Methods("POST")
|
2021-01-21 13:30:27 +08:00
|
|
|
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/archived_tasks", newListArchivedTasksHandlerFunc(inspector, t)).Methods("GET")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/archived_tasks/{task_id}", newDeleteTaskHandlerFunc(inspector)).Methods("DELETE")
|
2021-01-13 03:59:44 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/archived_tasks:delete_all", newDeleteAllArchivedTasksHandlerFunc(inspector)).Methods("DELETE")
|
|
|
|
api.HandleFunc("/queues/{qname}/archived_tasks:batch_delete", newBatchDeleteTasksHandlerFunc(inspector)).Methods("POST")
|
2021-05-29 05:40:09 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/archived_tasks/{task_id}:run", newRunTaskHandlerFunc(inspector)).Methods("POST")
|
2021-01-13 03:59:44 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/archived_tasks:run_all", newRunAllArchivedTasksHandlerFunc(inspector)).Methods("POST")
|
|
|
|
api.HandleFunc("/queues/{qname}/archived_tasks:batch_run", newBatchRunTasksHandlerFunc(inspector)).Methods("POST")
|
2020-12-07 00:46:14 +08:00
|
|
|
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/queues/{qname}/tasks/{task_id}", newGetTaskHandlerFunc(inspector, t)).Methods("GET")
|
2021-07-30 20:53:14 +08:00
|
|
|
|
2020-12-31 00:58:50 +08:00
|
|
|
// Servers endpoints.
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/servers", newListServersHandlerFunc(inspector, t)).Methods("GET")
|
2020-12-31 00:58:50 +08:00
|
|
|
|
2020-12-07 00:46:14 +08:00
|
|
|
// Scheduler Entry endpoints.
|
2021-09-18 20:26:00 +08:00
|
|
|
api.HandleFunc("/scheduler_entries", newListSchedulerEntriesHandlerFunc(inspector, t)).Methods("GET")
|
|
|
|
api.HandleFunc("/scheduler_entries/{entry_id}/enqueue_events", newListSchedulerEnqueueEventsHandlerFunc(inspector, t)).Methods("GET")
|
2020-11-24 22:54:00 +08:00
|
|
|
|
2021-01-03 22:56:53 +08:00
|
|
|
// Redis info endpoint.
|
2021-09-18 20:25:59 +08:00
|
|
|
switch c := opts.RedisClient.(type) {
|
|
|
|
case *redis.ClusterClient:
|
|
|
|
api.HandleFunc("/redis_info", newRedisClusterInfoHandlerFunc(c, inspector)).Methods("GET")
|
|
|
|
case *redis.Client:
|
|
|
|
api.HandleFunc("/redis_info", newRedisInfoHandlerFunc(c)).Methods("GET")
|
2020-11-24 22:54:00 +08:00
|
|
|
}
|
|
|
|
|
2021-10-01 02:23:01 +08:00
|
|
|
api.Handle("/", opts.StaticContentHandler)
|
|
|
|
|
2021-09-18 20:25:59 +08:00
|
|
|
return router
|
2020-11-24 22:54:00 +08:00
|
|
|
}
|