126 lines
2.8 KiB
Go
126 lines
2.8 KiB
Go
package handler
|
|
|
|
import (
|
|
"fmt"
|
|
"github.com/loveuer/nf"
|
|
"github.com/loveuer/nf/nft/resp"
|
|
"github.com/loveuer/nfflow/internal/database"
|
|
"github.com/loveuer/nfflow/internal/model"
|
|
"github.com/loveuer/nfflow/internal/opt"
|
|
"github.com/loveuer/nfflow/internal/sqlType"
|
|
"github.com/loveuer/nfflow/internal/util"
|
|
"time"
|
|
)
|
|
|
|
func TaskList(c *nf.Ctx) error {
|
|
type Req struct {
|
|
Keyword string `query:"keyword,omitempty"`
|
|
Page int `query:"page,omitempty"`
|
|
Size int `query:"size,omitempty"`
|
|
Targets sqlType.NumSlice[uint64] `query:"targets,omitempty"`
|
|
}
|
|
|
|
var (
|
|
err error
|
|
req = new(Req)
|
|
total int
|
|
list = make([]*model.Task, 0)
|
|
)
|
|
|
|
if err = c.QueryParser(req); err != nil {
|
|
return resp.Resp400(c, err.Error())
|
|
}
|
|
|
|
if req.Size <= 0 {
|
|
req.Size = 20
|
|
}
|
|
|
|
txGet := database.DB.Session(util.Timeout(10)).
|
|
Model(&model.Task{})
|
|
txCount := database.DB.Session(util.Timeout(5)).
|
|
Model(&model.Task{}).
|
|
Select("COUNT(id)")
|
|
|
|
if req.Keyword != "" {
|
|
key := fmt.Sprintf("%%%s%%", req.Keyword)
|
|
txGet = txGet.Where("task_name", key)
|
|
txCount = txCount.Where("task_name", key)
|
|
}
|
|
|
|
if err = txCount.Find(&total).Error; err != nil {
|
|
return resp.Resp500(c, err.Error())
|
|
}
|
|
|
|
if err = txGet.
|
|
Order("updated_at DESC").
|
|
Offset(req.Page * req.Size).
|
|
Limit(req.Size).
|
|
Find(&list).
|
|
Error; err != nil {
|
|
return resp.Resp500(c, err.Error())
|
|
}
|
|
|
|
return resp.Resp200(c, nf.Map{"list": list, "total": total})
|
|
}
|
|
|
|
func TaskCreate(c *nf.Ctx) error {
|
|
type Req struct {
|
|
TaskName string `json:"task_name"`
|
|
TaskType string `json:"task_type"`
|
|
Timeout int `json:"timeout"`
|
|
Cron string `json:"cron"`
|
|
RunAt int64 `json:"run_at"`
|
|
}
|
|
|
|
var (
|
|
err error
|
|
req = new(Req)
|
|
now = time.Now()
|
|
)
|
|
|
|
if err = c.BodyParser(req); err != nil {
|
|
return resp.Resp400(c, err.Error())
|
|
}
|
|
|
|
if req.TaskName != "" {
|
|
return resp.Resp400(c, req)
|
|
}
|
|
|
|
task := &model.Task{TaskName: req.TaskName}
|
|
|
|
if req.Timeout < opt.TaskMinTimeout || req.Timeout > opt.TaskMaxTimeout {
|
|
return resp.Resp400(c, req, fmt.Sprintf("timeout 时长过短(%d - %d)", opt.TaskMinTimeout, opt.TaskMaxTimeout))
|
|
}
|
|
|
|
task.TimeoutSecond = req.Timeout
|
|
|
|
switch req.TaskType {
|
|
case "once":
|
|
case "timing":
|
|
rt := time.UnixMilli(req.RunAt)
|
|
if rt.Sub(now).Seconds() > opt.TaskFetchInterval {
|
|
return resp.Resp400(c, req, "任务执行时间距离当前时间太短")
|
|
}
|
|
task.TaskRunType = fmt.Sprintf("T-%d", req.RunAt)
|
|
case "cron":
|
|
task.TaskRunType = fmt.Sprintf("C-%s", req.TaskType)
|
|
default:
|
|
return resp.Resp400(c, req, "任务执行类型: once/timing/cron")
|
|
}
|
|
|
|
if err = database.DB.Session(util.Timeout(5)).
|
|
Create(task).
|
|
Error; err != nil {
|
|
return resp.Resp500(c, err.Error())
|
|
}
|
|
|
|
return resp.Resp200(c, task)
|
|
}
|
|
|
|
func TaskInputCreate(c *nf.Ctx) error {
|
|
panic("impl")
|
|
}
|
|
func TaskInputUpdate(c *nf.Ctx) error {
|
|
panic("impl")
|
|
}
|