diff --git a/models/task_log.go b/models/task_log.go index f57d861..f28be96 100644 --- a/models/task_log.go +++ b/models/task_log.go @@ -4,10 +4,19 @@ import ( "time" ) +type TaskType int8 + +const ( + Timing = iota + 1 // 定时任务 + Delay // 延时任务 +) + // 任务执行日志 type TaskLog struct { Id int64 `xorm:"bigint pk autoincr"` - taskId int `xorm:"int notnull index default 0"` // 任务id + TaskId int `xorm:"int notnull index default 0"` // 任务id + Type TaskType `xorm:"tinyint notnull default 1"` // 任务类型 1 定时任务 2 延时任务 + Delay int `xorm:"int notnull default 0"` // 延时任务-延时时间 Name string `xorm:"varchar(64) notnull"` // 任务名称 Spec string `xorm:"varchar(64) notnull"` // crontab Protocol TaskProtocol `xorm:"tinyint notnull"` // 协议 1:http 2:ssh-command diff --git a/routers/api/v1/delaytask/delay_task.go b/routers/api/v1/delaytask/delay_task.go index 722e00e..b9362ae 100644 --- a/routers/api/v1/delaytask/delay_task.go +++ b/routers/api/v1/delaytask/delay_task.go @@ -1,6 +1,18 @@ package delaytask -import "gopkg.in/macaron.v1" +import ( + "gopkg.in/macaron.v1" + "github.com/ouqiang/gocron/models" +) + +type DelayForm struct { + Protocol models.TaskType + Type models.TaskType + Host string + Delay int + Command string + Timeout int +} // 创建延时任务 func Create(ctx *macaron.Context) { diff --git a/service/task.go b/service/task.go index 4687cef..67cb427 100644 --- a/service/task.go +++ b/service/task.go @@ -45,7 +45,9 @@ func (task *Task) Add(taskModel models.TaskHost) { cronName := strconv.Itoa(taskModel.Id) Cron.RemoveJob(cronName) err := Cron.AddFunc(taskModel.Spec, taskFunc, cronName) - logger.Error("添加任务到调度器失败#", err) + if err != nil { + logger.Error("添加任务到调度器失败#", err) + } } type Handler interface { @@ -103,6 +105,8 @@ func (h *SSHCommandHandler) Run(taskModel models.TaskHost) (string, error) { func createTaskLog(taskModel models.TaskHost) (int64, error) { taskLogModel := new(models.TaskLog) + taskLogModel.TaskId = taskModel.Id + taskLogModel.Type = models.Timing taskLogModel.Name = taskModel.Task.Name taskLogModel.Spec = taskModel.Spec taskLogModel.Protocol = taskModel.Protocol diff --git a/templates/host/index.html b/templates/host/index.html index b170ee0..8711c29 100644 --- a/templates/host/index.html +++ b/templates/host/index.html @@ -14,7 +14,7 @@ - +
@@ -35,7 +35,10 @@ - + {{{end}}} diff --git a/templates/task/log.html b/templates/task/log.html index 0707bfd..f29edb4 100644 --- a/templates/task/log.html +++ b/templates/task/log.html @@ -27,9 +27,11 @@ + + @@ -41,9 +43,11 @@ {{{range $i, $v := .Logs}}} + +
主机名{{{.Password}}} {{{.Port}}} {{{.Remark}}} + + +
任务名称任务类型 cron表达式 协议 超时时间(秒)延时时间(秒) 主机 开始时间 结束时间
{{{.Name}}}{{{if eq .Type 1}}}定时任务{{{else}}}延时任务{{{end}}} {{{.Spec}}} {{{if eq .Protocol 1}}} HTTP {{{else}}} SSH {{{end}}} {{{.Timeout}}}{{{.Delay}}} {{{.Hostname}}} {{{.StartTime.Format "2006-01-02 15:03:04" }}}