From 04884d5bdf7c5e25ce4453e84d9ae174d0445765 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E5=BB=BA=E5=9B=BD?= Date: Fri, 10 Jun 2022 18:21:17 +0800 Subject: [PATCH 1/6] =?UTF-8?q?=E8=87=AA=E5=AE=9A=E4=B9=89=E5=8A=9F?= =?UTF-8?q?=E8=83=BD=EF=BC=8C=E5=A2=9E=E5=8A=A0=20=E6=9A=B4=E9=9C=B2=20=20?= =?UTF-8?q?=E5=88=9B=E5=BB=BA=20=E5=88=A0=E9=99=A4=E4=BB=BB=E5=8A=A1api=20?= =?UTF-8?q?=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- Dockerfile | 2 +- go.mod | 1 + go.sum | 2 ++ internal/routers/routers.go | 2 ++ internal/routers/task/task.go | 4 +++- internal/service/task.go | 5 ++++- 6 files changed, 13 insertions(+), 3 deletions(-) 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/routers/routers.go b/internal/routers/routers.go index c2693feb6..cdaacf6a8 100644 --- a/internal/routers/routers.go +++ b/internal/routers/routers.go @@ -130,6 +130,8 @@ 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) }, apiAuth) // 404错误 diff --git a/internal/routers/task/task.go b/internal/routers/task/task.go index 42865a1d0..f2e38c5b5 100644 --- a/internal/routers/task/task.go +++ b/internal/routers/task/task.go @@ -198,7 +198,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..6d9c1b3d2 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) @@ -456,7 +459,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) } From 9e6ff7b3e7013ec5aa4a205cc04b92d32a34bbce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E5=B4=94=E5=BB=BA=E5=9B=BD?= Date: Mon, 31 Oct 2022 17:54:08 +0800 Subject: [PATCH 2/6] add: task total --- internal/routers/routers.go | 1 + internal/routers/task/task.go | 13 +++++++++++++ 2 files changed, 14 insertions(+) diff --git a/internal/routers/routers.go b/internal/routers/routers.go index cdaacf6a8..d8832d56f 100644 --- a/internal/routers/routers.go +++ b/internal/routers/routers.go @@ -132,6 +132,7 @@ func Register(m *macaron.Macaron) { 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) }, apiAuth) // 404错误 diff --git a/internal/routers/task/task.go b/internal/routers/task/task.go index f2e38c5b5..fc94eae05 100644 --- a/internal/routers/task/task.go +++ b/internal/routers/task/task.go @@ -72,6 +72,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") From 25551d280f51f51333b78acc3eaf49924c2d00fa Mon Sep 17 00:00:00 2001 From: cuijianguo <785662290@qq.com> Date: Thu, 30 Nov 2023 14:22:13 +0800 Subject: [PATCH 3/6] Update routers.go MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit add: 手动运行 api --- internal/routers/routers.go | 1 + 1 file changed, 1 insertion(+) diff --git a/internal/routers/routers.go b/internal/routers/routers.go index d8832d56f..b3eb2abea 100644 --- a/internal/routers/routers.go +++ b/internal/routers/routers.go @@ -133,6 +133,7 @@ func Register(m *macaron.Macaron) { 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) }, apiAuth) // 404错误 From 07f5a22a229b95522d165462d8bac0020d0a56e4 Mon Sep 17 00:00:00 2001 From: cuijianguo <785662290@qq.com> Date: Tue, 12 Dec 2023 09:31:28 +0800 Subject: [PATCH 4/6] =?UTF-8?q?add:=E6=B7=BB=E5=8A=A0=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E5=BC=80=E5=A7=8B=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/models/task.go | 1 + internal/routers/task/task.go | 2 ++ internal/service/task.go | 6 ++++++ 3 files changed, 9 insertions(+) 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/task/task.go b/internal/routers/task/task.go index fc94eae05..8f22cbd1b 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) { @@ -128,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 diff --git a/internal/service/task.go b/internal/service/task.go index 6d9c1b3d2..65900e0ce 100644 --- a/internal/service/task.go +++ b/internal/service/task.go @@ -328,6 +328,12 @@ func createJob(taskModel models.Task) cron.FuncJob { taskCount.Add() defer taskCount.Done() + // 判断是否到任务开始时间 + nowUnix := time.Now().Local().Unix() + if taskModel.TaskStartTime > 0 && (nowUnix-int64(taskModel.TaskStartTime) >= 0) { //未到开始时间 + return + } + taskLogId := beforeExecJob(taskModel) if taskLogId <= 0 { return From 9cc83041ba55b7b17e735c031e502844479b349a Mon Sep 17 00:00:00 2001 From: cuijianguo <785662290@qq.com> Date: Tue, 12 Dec 2023 13:04:25 +0800 Subject: [PATCH 5/6] =?UTF-8?q?add:=E5=88=A4=E6=96=AD=E6=98=AF=E5=90=A6?= =?UTF-8?q?=E5=88=B0=E4=BB=BB=E5=8A=A1=E5=BC=80=E5=A7=8B=E6=97=B6=E9=97=B4?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/service/task.go | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/service/task.go b/internal/service/task.go index 65900e0ce..a18456f1c 100644 --- a/internal/service/task.go +++ b/internal/service/task.go @@ -330,7 +330,8 @@ func createJob(taskModel models.Task) cron.FuncJob { // 判断是否到任务开始时间 nowUnix := time.Now().Local().Unix() - if taskModel.TaskStartTime > 0 && (nowUnix-int64(taskModel.TaskStartTime) >= 0) { //未到开始时间 + 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 } From 483ffc3b22be482d910530c433138dd26b54f2be Mon Sep 17 00:00:00 2001 From: cuijianguo <785662290@qq.com> Date: Wed, 13 Dec 2023 09:31:46 +0800 Subject: [PATCH 6/6] =?UTF-8?q?add:=20=E6=B7=BB=E5=8A=A0=E4=BB=BB=E5=8A=A1?= =?UTF-8?q?=E8=AF=A6=E6=83=85=E6=8E=A5=E5=8F=A3?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- internal/routers/routers.go | 1 + internal/routers/task/task.go | 2 +- 2 files changed, 2 insertions(+), 1 deletion(-) diff --git a/internal/routers/routers.go b/internal/routers/routers.go index b3eb2abea..65a5e827a 100644 --- a/internal/routers/routers.go +++ b/internal/routers/routers.go @@ -134,6 +134,7 @@ func Register(m *macaron.Macaron) { 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 8f22cbd1b..a1d7ebdae 100644 --- a/internal/routers/task/task.go +++ b/internal/routers/task/task.go @@ -96,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) }