Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cjg develop #383

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Dockerfile
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
1 change: 1 addition & 0 deletions go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 2 additions & 0 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -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=
Expand Down
1 change: 1 addition & 0 deletions internal/models/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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:"-"`
Expand Down
5 changes: 5 additions & 0 deletions internal/routers/routers.go
Original file line number Diff line number Diff line change
Expand Up @@ -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错误
Expand Down
21 changes: 19 additions & 2 deletions internal/routers/task/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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")
Expand All @@ -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)
}

Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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,
})
}

// 删除任务
Expand Down
12 changes: 11 additions & 1 deletion internal/service/task.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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)
}
Expand Down