定时任务添加http模式
This commit is contained in:
+44
-10
@@ -84,7 +84,7 @@ func TaskTable(r *ghttp.Request) {
|
||||
if t.UpdateTime > 0 {
|
||||
ut = time.Unix(t.UpdateTime, 0).Format("2006-01-02 15:04:05")
|
||||
}
|
||||
rows = append(rows, g.Map{"id": t.Id, "task_name": t.TaskName, "group_id": t.GroupId, "cron_spec": t.CronSpec, "command": t.Command, "status": t.Status, "status_text": st, "prev_time": t.PrevTime, "prev_time_str": pt, "next_time": nt, "execute_times": t.ExecuteTimes, "server_ids": t.ServerIds, "create_time_str": ct, "update_time_str": ut})
|
||||
rows = append(rows, g.Map{"id": t.Id, "task_name": t.TaskName, "group_id": t.GroupId, "cron_spec": t.CronSpec, "task_type": t.TaskType, "command": t.Command, "url": t.Url, "method": t.Method, "headers": t.Headers, "body": t.Body, "status": t.Status, "status_text": st, "prev_time": t.PrevTime, "prev_time_str": pt, "next_time": nt, "execute_times": t.ExecuteTimes, "server_ids": t.ServerIds, "create_time_str": ct, "update_time_str": ut})
|
||||
}
|
||||
ajaxList(r, "成功", 0, total, rows)
|
||||
}
|
||||
@@ -94,7 +94,12 @@ func TaskAjaxSave(r *ghttp.Request) {
|
||||
id := r.Get("id", 0).Int()
|
||||
taskName := r.Get("task_name").String()
|
||||
cronSpec := r.Get("cron_spec").String()
|
||||
taskType := r.Get("task_type", "shell").String()
|
||||
command := r.Get("command").String()
|
||||
url := r.Get("url").String()
|
||||
method := r.Get("method", "GET").String()
|
||||
headers := r.Get("headers").String()
|
||||
body := r.Get("body").String()
|
||||
groupId := r.Get("group_id", 0).Int()
|
||||
serverIds := r.Get("server_ids").String()
|
||||
serverType := r.Get("server_type", 0).Int()
|
||||
@@ -105,20 +110,31 @@ func TaskAjaxSave(r *ghttp.Request) {
|
||||
notifyTplId := r.Get("notify_tpl_id", 0).Int()
|
||||
notifyUserIds := r.Get("notify_user_ids").String()
|
||||
description := r.Get("description").String()
|
||||
if taskName == "" || cronSpec == "" || command == "" {
|
||||
ajaxMsg(r, "任务名称、Cron表达式、命令不能为空", consts.MsgErr)
|
||||
if taskName == "" || cronSpec == "" {
|
||||
ajaxMsg(r, "任务名称、Cron表达式不能为空", consts.MsgErr)
|
||||
return
|
||||
}
|
||||
if taskType == "http" {
|
||||
if url == "" {
|
||||
ajaxMsg(r, "URL不能为空", consts.MsgErr)
|
||||
return
|
||||
}
|
||||
} else {
|
||||
if command == "" {
|
||||
ajaxMsg(r, "命令不能为空", consts.MsgErr)
|
||||
return
|
||||
}
|
||||
if err := scheduler.Scheduler.CheckCommand(ctx, command); err != nil {
|
||||
ajaxMsg(r, err.Error(), consts.MsgErr)
|
||||
return
|
||||
}
|
||||
}
|
||||
// 验证 Cron 表达式(临时注册再移除,仅用于校验格式)
|
||||
if _, err := gcron.AddSingleton(ctx, cronSpec, func(ctx context.Context) {}, "cron_validator"); err != nil {
|
||||
ajaxMsg(r, "Cron表达式格式错误: "+err.Error(), consts.MsgErr)
|
||||
return
|
||||
}
|
||||
gcron.Remove("cron_validator")
|
||||
if err := scheduler.Scheduler.CheckCommand(ctx, command); err != nil {
|
||||
ajaxMsg(r, err.Error(), consts.MsgErr)
|
||||
return
|
||||
}
|
||||
now := time.Now().Unix()
|
||||
userId := r.GetCtxVar("userId", 0).Int()
|
||||
if id > 0 {
|
||||
@@ -134,7 +150,12 @@ func TaskAjaxSave(r *ghttp.Request) {
|
||||
task.ServerType = serverType
|
||||
task.CronSpec = cronSpec
|
||||
task.Concurrent = concurrent
|
||||
task.TaskType = taskType
|
||||
task.Command = command
|
||||
task.Url = url
|
||||
task.Method = method
|
||||
task.Headers = headers
|
||||
task.Body = body
|
||||
task.Timeout = timeout
|
||||
task.IsNotify = isNotify
|
||||
task.NotifyType = notifyType
|
||||
@@ -158,7 +179,9 @@ func TaskAjaxSave(r *ghttp.Request) {
|
||||
t := &entity.Task{
|
||||
TaskName: taskName, Description: description, GroupId: groupId,
|
||||
ServerIds: serverIds, ServerType: serverType, CronSpec: cronSpec,
|
||||
Concurrent: concurrent, Command: command, Timeout: timeout,
|
||||
Concurrent: concurrent, TaskType: taskType,
|
||||
Command: command, Url: url, Method: method,
|
||||
Headers: headers, Body: body, Timeout: timeout,
|
||||
Status: status, IsNotify: isNotify, NotifyType: notifyType,
|
||||
NotifyTplId: notifyTplId, NotifyUserIds: notifyUserIds,
|
||||
CreateId: userId, UpdateId: userId, CreateTime: now, UpdateTime: now,
|
||||
@@ -493,6 +516,16 @@ func TaskLogTable(r *ghttp.Request) {
|
||||
return
|
||||
}
|
||||
|
||||
// 构建任务ID→名称映射
|
||||
taskNameMap := make(map[int]string)
|
||||
for _, l := range list {
|
||||
if _, ok := taskNameMap[l.TaskId]; !ok {
|
||||
if t, _ := dao.Task.GetById(ctx, l.TaskId); t != nil {
|
||||
taskNameMap[l.TaskId] = t.TaskName
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
rows := make([]g.Map, 0)
|
||||
for _, l := range list {
|
||||
st := "成功"
|
||||
@@ -511,8 +544,9 @@ func TaskLogTable(r *ghttp.Request) {
|
||||
sizeText = fmt.Sprintf("%.1f KB", float64(outputSize)/1024)
|
||||
}
|
||||
rows = append(rows, g.Map{
|
||||
"id": l.Id, "task_id": l.TaskId, "server_name": l.ServerName,
|
||||
"status": l.Status, "statusText": st,
|
||||
"id": l.Id, "task_id": l.TaskId, "task_name": taskNameMap[l.TaskId],
|
||||
"server_name": l.ServerName,
|
||||
"status": l.Status, "statusText": st,
|
||||
"process_time": l.ProcessTime,
|
||||
"start_time": startTime,
|
||||
"output_size": sizeText,
|
||||
|
||||
Reference in New Issue
Block a user