diff --git a/models/task.go b/models/task.go index 0de5458..03a0845 100644 --- a/models/task.go +++ b/models/task.go @@ -52,6 +52,10 @@ func (task *Task) Create() (insertId int, err error) { return } +func (task *Task) UpdateBean(id int) (int64, error) { + return Db.UseBool("status").Update(task) +} + // 更新 func (task *Task) Update(id int, data CommonMap) (int64, error) { return Db.Table(task).ID(id).Update(data) @@ -89,7 +93,11 @@ func (task *Task) HostIdExist(hostId int16) (bool, error) { } // 判断任务名称是否存在 -func (task *Task) NameExist(name string) (bool, error) { +func (task *Task) NameExist(name string, id int) (bool, error) { + if id > 0 { + count, err := Db.Where("name = ? AND status = ? AND id != ?", name, Enabled, id).Count(task); + return count > 0, err + } count, err := Db.Where("name = ? AND status = ?", name, Enabled).Count(task); return count > 0, err diff --git a/modules/ssh/ssh.go b/modules/ssh/ssh.go index 0d6232a..2d06bee 100644 --- a/modules/ssh/ssh.go +++ b/modules/ssh/ssh.go @@ -39,7 +39,6 @@ func Exec(sshConfig SSHConfig, cmd string) (output string, err error) { var resultChan chan Result = make(chan Result) var timeoutChan chan bool = make(chan bool) go func() { - cmd += fmt.Sprintf(" & { sleep %d; eval 'kill $!' &> /dev/null; }", sshConfig.ExecTimeout) output, err := session.CombinedOutput(cmd) resultChan <- Result{string(output), err} }() diff --git a/routers/routers.go b/routers/routers.go index 3ef62e1..77119ec 100644 --- a/routers/routers.go +++ b/routers/routers.go @@ -60,6 +60,7 @@ func Register(m *macaron.Macaron) { m.Group("/task", func() { m.Get("/create", task.Create) m.Post("/store", binding.Bind(task.TaskForm{}), task.Store) + m.Get("/edit/:id", task.Edit) m.Get("", task.Index) m.Get("/log", tasklog.Index) m.Post("/log/clear", tasklog.Clear) diff --git a/routers/task/task.go b/routers/task/task.go index e3418b3..95bb73c 100644 --- a/routers/task/task.go +++ b/routers/task/task.go @@ -27,7 +27,31 @@ func Create(ctx *macaron.Context) { if err != nil || len(hosts) == 0 { logger.Error(err) } - ctx.Data["Title"] = "任务管理" + ctx.Data["Title"] = "添加任务" + ctx.Data["Hosts"] = hosts + if len(hosts) > 0 { + ctx.Data["FirstHostName"] = hosts[0].Name + ctx.Data["FirstHostId"] = hosts[0].Id + } + ctx.HTML(200, "task/task_form") +} + +func Edit(ctx *macaron.Context) { + id := ctx.ParamsInt(":id") + hostModel := new(models.Host) + hosts, err := hostModel.List() + if err != nil || len(hosts) == 0 { + logger.Error(err) + } + taskModel := new(models.Task) + task, err := taskModel.Detail(id) + if err != nil { + logger.Errorf("编辑任务#获取任务详情失败#任务ID-%d#%s", id, err.Error()) + ctx.Redirect("/task") + } + ctx.Data["TaskId"] = id + ctx.Data["Task"] = task + ctx.Data["Title"] = "编辑" ctx.Data["Hosts"] = hosts if len(hosts) > 0 { ctx.Data["FirstHostName"] = hosts[0].Name @@ -37,6 +61,7 @@ func Create(ctx *macaron.Context) { } type TaskForm struct { + Id int Name string `binding:"Required;"` Spec string `binding:"Required;MaxSize(64)"` Protocol models.TaskProtocol `binding:"In(1,2,3)"` @@ -44,18 +69,19 @@ type TaskForm struct { Timeout int `binding:"Range(0,86400)"` HostId int16 Remark string - Status models.Status `binding:"In(1,0)"` + Status models.Status `binding:"In(1,2)"` } // 保存任务 func Store(ctx *macaron.Context, form TaskForm) string { json := utils.JsonResponse{} taskModel := models.Task{} + var id int = form.Id _, err := cron.Parse(form.Spec) if err != nil { return json.CommonFailure("crontab表达式解析失败", err) } - nameExists, err := taskModel.NameExist(form.Name) + nameExists, err := taskModel.NameExist(form.Name, form.Id) if err != nil { return json.CommonFailure(utils.FailureContent, err) } @@ -74,15 +100,22 @@ func Store(ctx *macaron.Context, form TaskForm) string { taskModel.HostId = form.HostId taskModel.Remark = form.Remark taskModel.Status = form.Status + if taskModel.Status != models.Enabled { + taskModel.Status = models.Disabled + } taskModel.Spec = form.Spec - insertId, err := taskModel.Create() + if id == 0 { + id, err = taskModel.Create() + } else { + _, err = taskModel.UpdateBean(id) + } if err != nil { return json.CommonFailure("保存失败", err) } // 任务处于激活状态,加入调度管理 if (taskModel.Status == models.Enabled) { - addTaskToTimer(insertId) + addTaskToTimer(id) } return json.Success("保存成功", nil) diff --git a/templates/host/host_form.html b/templates/host/host_form.html index ec277ff..632eed8 100644 --- a/templates/host/host_form.html +++ b/templates/host/host_form.html @@ -62,7 +62,7 @@ -
提交
+
保存
diff --git a/templates/task/index.html b/templates/task/index.html index 0489796..fb665d4 100644 --- a/templates/task/index.html +++ b/templates/task/index.html @@ -39,6 +39,7 @@ {{{.Remark}}} {{{if eq .Status 1}}} {{{else}}} {{{end}}} + 编辑 {{{if eq .Status 1}}} {{{else}}} diff --git a/templates/task/task_form.html b/templates/task/task_form.html index 70b7916..f7b5a91 100644 --- a/templates/task/task_form.html +++ b/templates/task/task_form.html @@ -8,36 +8,41 @@

- 添加任务 + {{{.Title}}}

+
- +
- +
@@ -49,7 +54,13 @@ {{{range $i, $v := .Hosts}}}
- + {{{if $.Task}}} + + {{{else}}} + + {{{end}}}
@@ -58,23 +69,27 @@
- +
- +
@@ -83,10 +98,10 @@
- +
-
提交
+
保存