mirror of
https://github.com/hibiken/asynqmon.git
synced 2025-10-03 02:32:00 +08:00
Add REST API endpoint to get task by ID
This commit is contained in:
@@ -85,6 +85,61 @@ func toDailyStatsList(in []*asynq.DailyStats) []*DailyStats {
|
|||||||
return out
|
return out
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type TaskInfo struct {
|
||||||
|
// ID is the identifier of the task.
|
||||||
|
ID string `json:"id"`
|
||||||
|
// Queue is the name of the queue in which the task belongs.
|
||||||
|
Queue string `json:"queue"`
|
||||||
|
// Type is the type name of the task.
|
||||||
|
Type string `json:"type"`
|
||||||
|
// Payload is the payload data of the task.
|
||||||
|
Payload string `json:"payload"`
|
||||||
|
// State indicates the task state.
|
||||||
|
State string `json:"state"`
|
||||||
|
// MaxRetry is the maximum number of times the task can be retried.
|
||||||
|
MaxRetry int `json:"max_retry"`
|
||||||
|
// Retried is the number of times the task has retried so far.
|
||||||
|
Retried int `json:"retried"`
|
||||||
|
// LastErr is the error message from the last failure.
|
||||||
|
LastErr string `json:"error_message"`
|
||||||
|
// LastFailedAt is the time time of the last failure in RFC3339 format.
|
||||||
|
// If the task has no failures, empty string.
|
||||||
|
LastFailedAt string `json:"last_failed_at"`
|
||||||
|
// Timeout is the number of seconds the task can be processed by Handler before being retried.
|
||||||
|
Timeout int `json:"timeout_seconds"`
|
||||||
|
// Deadline is the deadline for the task in RFC3339 format. If not set, empty string.
|
||||||
|
Deadline string `json:"deadline"`
|
||||||
|
// NextProcessAt is the time the task is scheduled to be processed in RFC3339 format.
|
||||||
|
// If not applicable, empty string.
|
||||||
|
NextProcessAt string `json:"next_process_at"`
|
||||||
|
}
|
||||||
|
|
||||||
|
// formatTimeInRFC3339 formats t in RFC3339 if the value is non-zero.
|
||||||
|
// If t is zero time (i.e. time.Time{}), returns empty string
|
||||||
|
func formatTimeInRFC3339(t time.Time) string {
|
||||||
|
if t.IsZero() {
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
return t.Format(time.RFC3339)
|
||||||
|
}
|
||||||
|
|
||||||
|
func toTaskInfo(info *asynq.TaskInfo) *TaskInfo {
|
||||||
|
return &TaskInfo{
|
||||||
|
ID: info.ID,
|
||||||
|
Queue: info.Queue,
|
||||||
|
Type: info.Type,
|
||||||
|
Payload: toPrintablePayload(info.Payload),
|
||||||
|
State: info.State.String(),
|
||||||
|
MaxRetry: info.MaxRetry,
|
||||||
|
Retried: info.Retried,
|
||||||
|
LastErr: info.LastErr,
|
||||||
|
LastFailedAt: formatTimeInRFC3339(info.LastFailedAt),
|
||||||
|
Timeout: int(info.Timeout.Seconds()),
|
||||||
|
Deadline: formatTimeInRFC3339(info.Deadline),
|
||||||
|
NextProcessAt: formatTimeInRFC3339(info.NextProcessAt),
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
type BaseTask struct {
|
type BaseTask struct {
|
||||||
ID string `json:"id"`
|
ID string `json:"id"`
|
||||||
Type string `json:"type"`
|
Type string `json:"type"`
|
||||||
|
2
main.go
2
main.go
@@ -197,6 +197,8 @@ func main() {
|
|||||||
api.HandleFunc("/queues/{qname}/archived_tasks:run_all", newRunAllArchivedTasksHandlerFunc(inspector)).Methods("POST")
|
api.HandleFunc("/queues/{qname}/archived_tasks:run_all", newRunAllArchivedTasksHandlerFunc(inspector)).Methods("POST")
|
||||||
api.HandleFunc("/queues/{qname}/archived_tasks:batch_run", newBatchRunTasksHandlerFunc(inspector)).Methods("POST")
|
api.HandleFunc("/queues/{qname}/archived_tasks:batch_run", newBatchRunTasksHandlerFunc(inspector)).Methods("POST")
|
||||||
|
|
||||||
|
api.HandleFunc("/queues/{qname}/tasks/{task_id}", newGetTaskHandlerFunc(inspector)).Methods("GET")
|
||||||
|
|
||||||
// Servers endpoints.
|
// Servers endpoints.
|
||||||
api.HandleFunc("/servers", newListServersHandlerFunc(inspector)).Methods("GET")
|
api.HandleFunc("/servers", newListServersHandlerFunc(inspector)).Methods("GET")
|
||||||
|
|
||||||
|
@@ -2,6 +2,7 @@ package main
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
|
"errors"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
"strconv"
|
"strconv"
|
||||||
@@ -623,3 +624,33 @@ func getPageOptions(r *http.Request) (pageSize, pageNum int) {
|
|||||||
}
|
}
|
||||||
return pageSize, pageNum
|
return pageSize, pageNum
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func newGetTaskHandlerFunc(inspector *asynq.Inspector) http.HandlerFunc {
|
||||||
|
return func(w http.ResponseWriter, r *http.Request) {
|
||||||
|
vars := mux.Vars(r)
|
||||||
|
qname, taskid := vars["qname"], vars["task_id"]
|
||||||
|
if qname == "" {
|
||||||
|
http.Error(w, "queue name cannot be empty", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
if taskid == "" {
|
||||||
|
http.Error(w, "task_id cannot be empty", http.StatusBadRequest)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
info, err := inspector.GetTaskInfo(qname, taskid)
|
||||||
|
switch {
|
||||||
|
case errors.Is(err, asynq.ErrQueueNotFound), errors.Is(err, asynq.ErrTaskNotFound):
|
||||||
|
http.Error(w, err.Error(), http.StatusNotFound)
|
||||||
|
return
|
||||||
|
case err != nil:
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := json.NewEncoder(w).Encode(toTaskInfo(info)); err != nil {
|
||||||
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
|
return
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Reference in New Issue
Block a user