2021-12-06 12:24:22 +00:00
|
|
|
package api
|
|
|
|
|
|
|
|
import (
|
2021-12-24 03:50:21 +00:00
|
|
|
"net/http"
|
|
|
|
|
2021-12-06 12:24:22 +00:00
|
|
|
"github.com/cuigh/auxo/data"
|
|
|
|
"github.com/cuigh/auxo/net/web"
|
|
|
|
"github.com/cuigh/swirl/biz"
|
2022-01-06 08:54:14 +00:00
|
|
|
"github.com/cuigh/swirl/misc"
|
2021-12-06 12:24:22 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// TaskHandler encapsulates node related handlers.
|
|
|
|
type TaskHandler struct {
|
|
|
|
Search web.HandlerFunc `path:"/search" auth:"task.view" desc:"search tasks"`
|
|
|
|
Find web.HandlerFunc `path:"/find" auth:"task.view" desc:"find task by id"`
|
|
|
|
FetchLogs web.HandlerFunc `path:"/fetch-logs" auth:"task.logs" desc:"fetch logs of task"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewTask creates an instance of TaskHandler
|
|
|
|
func NewTask(b biz.TaskBiz) *TaskHandler {
|
|
|
|
return &TaskHandler{
|
|
|
|
Search: taskSearch(b),
|
|
|
|
Find: taskFind(b),
|
|
|
|
FetchLogs: taskFetchLogs(b),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskSearch(b biz.TaskBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
Service string `json:"service" bind:"service"`
|
|
|
|
State string `json:"state" bind:"state"`
|
|
|
|
PageIndex int `json:"pageIndex" bind:"pageIndex"`
|
|
|
|
PageSize int `json:"pageSize" bind:"pageSize"`
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
var (
|
|
|
|
args = &Args{}
|
|
|
|
tasks []*biz.Task
|
|
|
|
total int
|
|
|
|
)
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
tasks, total, err = b.Search(ctx, "", args.Service, args.State, args.PageIndex, args.PageSize)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{
|
2021-12-06 12:24:22 +00:00
|
|
|
"items": tasks,
|
|
|
|
"total": total,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskFind(b biz.TaskBiz) web.HandlerFunc {
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) error {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
id := c.Query("id")
|
|
|
|
task, raw, err := b.Find(ctx, id)
|
2021-12-06 12:24:22 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
2021-12-24 11:52:29 +00:00
|
|
|
} else if task == nil {
|
|
|
|
return web.NewError(http.StatusNotFound)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{"task": task, "raw": raw})
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func taskFetchLogs(b biz.TaskBiz) web.HandlerFunc {
|
|
|
|
type Args struct {
|
|
|
|
ID string `json:"id" bind:"id"`
|
|
|
|
Lines int `json:"lines" bind:"lines"`
|
|
|
|
Timestamps bool `json:"timestamps" bind:"timestamps"`
|
|
|
|
}
|
|
|
|
|
2022-01-06 08:54:14 +00:00
|
|
|
return func(c web.Context) (err error) {
|
2021-12-06 12:24:22 +00:00
|
|
|
var (
|
|
|
|
args = &Args{}
|
|
|
|
stdout, stderr string
|
|
|
|
)
|
2022-01-06 08:54:14 +00:00
|
|
|
if err = c.Bind(args); err == nil {
|
|
|
|
ctx, cancel := misc.Context(defaultTimeout)
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
stdout, stderr, err = b.FetchLogs(ctx, args.ID, args.Lines, args.Timestamps)
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2022-01-06 08:54:14 +00:00
|
|
|
return success(c, data.Map{"stdout": stdout, "stderr": stderr})
|
2021-12-06 12:24:22 +00:00
|
|
|
}
|
|
|
|
}
|