-
Notifications
You must be signed in to change notification settings - Fork 0
/
task_log.go
98 lines (85 loc) · 2.44 KB
/
task_log.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
package tasklog
// 任务日志
import (
"github.com/zmhuanf/gocron/internal/models"
"github.com/zmhuanf/gocron/internal/modules/logger"
"github.com/zmhuanf/gocron/internal/modules/utils"
"github.com/zmhuanf/gocron/internal/routers/base"
"github.com/zmhuanf/gocron/internal/service"
"gopkg.in/macaron.v1"
)
func Index(ctx *macaron.Context) string {
logModel := new(models.TaskLog)
queryParams := parseQueryParams(ctx)
total, err := logModel.Total(queryParams)
if err != nil {
logger.Error(err)
}
logs, err := logModel.List(queryParams)
if err != nil {
logger.Error(err)
}
jsonResp := utils.JsonResponse{}
return jsonResp.Success(utils.SuccessContent, map[string]interface{}{
"total": total,
"data": logs,
})
}
// 清空日志
func Clear(ctx *macaron.Context) string {
taskLogModel := new(models.TaskLog)
_, err := taskLogModel.Clear()
json := utils.JsonResponse{}
if err != nil {
return json.CommonFailure(utils.FailureContent)
}
return json.Success(utils.SuccessContent, nil)
}
// 停止运行中的任务
func Stop(ctx *macaron.Context) string {
id := ctx.QueryInt64("id")
taskId := ctx.QueryInt("task_id")
taskModel := new(models.Task)
task, err := taskModel.Detail(taskId)
json := utils.JsonResponse{}
if err != nil {
return json.CommonFailure("获取任务信息失败#"+err.Error(), err)
}
if task.Protocol != models.TaskRPC {
return json.CommonFailure("仅支持SHELL任务手动停止")
}
if len(task.Hosts) == 0 {
return json.CommonFailure("任务节点列表为空")
}
for _, host := range task.Hosts {
service.ServiceTask.Stop(host.Name, host.Port, id)
}
return json.Success("已执行停止操作, 请等待任务退出", nil)
}
// 删除N个月前的日志
func Remove(ctx *macaron.Context) string {
month := ctx.ParamsInt(":id")
json := utils.JsonResponse{}
if month < 1 || month > 12 {
return json.CommonFailure("参数取值范围1-12")
}
taskLogModel := new(models.TaskLog)
_, err := taskLogModel.Remove(month)
if err != nil {
return json.CommonFailure("删除失败", err)
}
return json.Success("删除成功", nil)
}
// 解析查询参数
func parseQueryParams(ctx *macaron.Context) models.CommonMap {
var params models.CommonMap = models.CommonMap{}
params["TaskId"] = ctx.QueryInt("task_id")
params["Protocol"] = ctx.QueryInt("protocol")
status := ctx.QueryInt("status")
if status >= 0 {
status -= 1
}
params["Status"] = status
base.ParsePageAndPageSize(ctx, params)
return params
}