diff --git a/Dockerfile b/Dockerfile index 63fe8a135..14df14bf3 100644 --- a/Dockerfile +++ b/Dockerfile @@ -8,7 +8,7 @@ RUN go env -w GO111MODULE=on && \ WORKDIR /app -RUN git clone https://github.com/ouqiang/gocron.git \ +RUN git clone https://github.com/cuijianguo/gocron.git \ && cd gocron \ && yarn config set ignore-engines true \ && make install-vue \ diff --git a/go.mod b/go.mod index 194c67953..f466deaf1 100644 --- a/go.mod +++ b/go.mod @@ -28,6 +28,7 @@ require ( golang.org/x/crypto v0.0.0-20190530122614-20be4c3c3ed5 // indirect golang.org/x/net v0.0.0-20190522155817-f3200d17e092 golang.org/x/sync v0.0.0-20181108010431-42b317875d0f // indirect + golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68 // indirect golang.org/x/text v0.3.2 // indirect google.golang.org/genproto v0.0.0-20190530194941-fb225487d101 // indirect google.golang.org/grpc v1.21.0 diff --git a/go.sum b/go.sum index 9aacf0803..95a8937eb 100644 --- a/go.sum +++ b/go.sum @@ -125,6 +125,8 @@ golang.org/x/sys v0.0.0-20190412213103-97732733099d h1:+R4KGOnez64A81RvjARKc4UT5 golang.org/x/sys v0.0.0-20190412213103-97732733099d/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= golang.org/x/sys v0.0.0-20190422165155-953cdadca894 h1:Cz4ceDQGXuKRnVBDTS23GTn/pU5OE2C0WrNTOYK1Uuc= golang.org/x/sys v0.0.0-20190422165155-953cdadca894/go.mod h1:h1NjWce9XRLGQEsW7wpKNCjG9DtNlClVuFLEZdDNbEs= +golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68 h1:z8Hj/bl9cOV2grsOpEaQFUaly0JWN3i97mo3jXKJNp0= +golang.org/x/sys v0.0.0-20220608164250-635b8c9b7f68/go.mod h1:oPkhp1MJrh7nUepCBck5+mAzfO9JrbApNNgaTdGDITg= golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= golang.org/x/text v0.3.2 h1:tW2bmiBqwgJj/UpqtC8EpXEZVYOwU0yG4iWbprSVAcs= golang.org/x/text v0.3.2/go.mod h1:bEr9sfX3Q8Zfm5fL9x+3itogRgK3+ptLWKqgva+5dAk= diff --git a/internal/models/task.go b/internal/models/task.go index f9062f90b..381c1144d 100644 --- a/internal/models/task.go +++ b/internal/models/task.go @@ -60,6 +60,7 @@ type Task struct { Status Status `json:"status" xorm:"tinyint notnull index default 0"` // 状态 1:正常 0:停止 Created time.Time `json:"created" xorm:"datetime notnull created"` // 创建时间 Deleted time.Time `json:"deleted" xorm:"datetime deleted"` // 删除时间 + TaskStartTime int `json:"task_start_time" xorm:"int notnull default 0"` // 任务开始时间 0表示不设置。 如xx年xx日后执行 BaseModel `json:"-" xorm:"-"` Hosts []TaskHostDetail `json:"hosts" xorm:"-"` NextRunTime time.Time `json:"next_run_time" xorm:"-"` diff --git a/internal/routers/routers.go b/internal/routers/routers.go index c2693feb6..65a5e827a 100644 --- a/internal/routers/routers.go +++ b/internal/routers/routers.go @@ -130,6 +130,11 @@ func Register(m *macaron.Macaron) { m.Post("/tasklog/remove/:id", tasklog.Remove) m.Post("/task/enable/:id", task.Enable) m.Post("/task/disable/:id", task.Disable) + m.Post("/task/store", binding.Bind(task.TaskForm{}), task.Store) + m.Post("/task/remove/:id", task.Remove) + m.Get("/task/total", task.Total) + m.Get("/task/run/:id", task.Run) + m.Get("/task/:id", task.Detail) }, apiAuth) // 404错误 diff --git a/internal/routers/task/task.go b/internal/routers/task/task.go index 42865a1d0..a1d7ebdae 100644 --- a/internal/routers/task/task.go +++ b/internal/routers/task/task.go @@ -37,6 +37,7 @@ type TaskForm struct { NotifyType int8 `binding:"In(1,2,3,4)"` NotifyReceiverId string NotifyKeyword string + TaskStartTime int } func (f TaskForm) Error(ctx *macaron.Context, errs binding.Errors) { @@ -72,6 +73,19 @@ func Index(ctx *macaron.Context) string { }) } +func Total(ctx *macaron.Context) string { + taskModel := new(models.Task) + queryParams := parseQueryParams(ctx) + total, err := taskModel.Total(queryParams) + if err != nil { + logger.Error(err) + } + jsonResp := utils.JsonResponse{} + return jsonResp.Success(utils.SuccessContent, map[string]interface{}{ + "total": total, + }) +} + // Detail 任务详情 func Detail(ctx *macaron.Context) string { id := ctx.ParamsInt(":id") @@ -82,7 +96,7 @@ func Detail(ctx *macaron.Context) string { logger.Errorf("编辑任务#获取任务详情失败#任务ID-%d", id) return jsonResp.Success(utils.SuccessContent, nil) } - + task.NextRunTime = service.ServiceTask.NextRunTime(task) return jsonResp.Success(utils.SuccessContent, task) } @@ -115,6 +129,7 @@ func Store(ctx *macaron.Context, form TaskForm) string { if taskModel.Multi != 1 { taskModel.Multi = 0 } + taskModel.TaskStartTime = form.TaskStartTime taskModel.NotifyStatus = form.NotifyStatus - 1 taskModel.NotifyType = form.NotifyType - 1 taskModel.NotifyReceiverId = form.NotifyReceiverId @@ -198,7 +213,9 @@ func Store(ctx *macaron.Context, form TaskForm) string { addTaskToTimer(id) } - return json.Success("保存成功", nil) + return json.Success("保存成功", map[string]interface{}{ + "taskId": id, + }) } // 删除任务 diff --git a/internal/service/task.go b/internal/service/task.go index 268093123..a18456f1c 100644 --- a/internal/service/task.go +++ b/internal/service/task.go @@ -219,6 +219,9 @@ func (h *HTTPHandler) Run(taskModel models.Task, taskUniqueId int64) (result str if taskModel.Timeout <= 0 || taskModel.Timeout > HttpExecTimeout { taskModel.Timeout = HttpExecTimeout } + if strings.Contains(taskModel.Command, "{{.TaskId}}") { + taskModel.Command = strings.ReplaceAll(taskModel.Command, "{{.TaskId}}", strconv.Itoa(taskModel.Id)) + } var resp httpclient.ResponseWrapper if taskModel.HttpMethod == models.TaskHTTPMethodGet { resp = httpclient.Get(taskModel.Command, taskModel.Timeout) @@ -325,6 +328,13 @@ func createJob(taskModel models.Task) cron.FuncJob { taskCount.Add() defer taskCount.Done() + // 判断是否到任务开始时间 + nowUnix := time.Now().Local().Unix() + if taskModel.TaskStartTime > 0 && (int64(taskModel.TaskStartTime)-nowUnix >= 0) { //未到开始时间 + //logger.Infof("任务ID:%s 未开始,开始时间: %s", taskModel.Id, time.Unix(int64(taskModel.TaskStartTime), 0).Format("2006-01-02 15:04:05")) + return + } + taskLogId := beforeExecJob(taskModel) if taskLogId <= 0 { return @@ -456,7 +466,7 @@ func SendNotification(taskModel models.Task, taskResult TaskResult) { "output": taskResult.Result, "status": statusName, "task_id": taskModel.Id, - "remark": taskModel.Remark, + "remark": taskModel.Remark, } notify.Push(msg) }