diff --git a/.travis.yml b/.travis.yml index bd936eb..14e1302 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,15 +1,9 @@ language: go - go: - 1.5.x - 1.6.x - 1.7.x - 1.8.x - master - env: - - GO15VENDOREXPERIMENT=1 - -os: - - linux - - osx \ No newline at end of file + - GO15VENDOREXPERIMENT=1 \ No newline at end of file diff --git a/cmd/web.go b/cmd/web.go index fc8f318..e8ffe65 100644 --- a/cmd/web.go +++ b/cmd/web.go @@ -14,8 +14,10 @@ import ( "os/exec" "syscall" "github.com/ouqiang/cron-scheduler/modules/logger" + "github.com/ouqiang/cron-scheduler/modules/crontask" ) +// 1号进程id const InitProcess = 1 // web服务器默认端口 @@ -56,7 +58,6 @@ func run(ctx *cli.Context) { app.InitEnv() // 捕捉信号,配置热更新等 go catchSignal() - m := macaron.Classic() // 注册路由 routers.Register(m) @@ -115,17 +116,16 @@ func setEnvironment(ctx *cli.Context) { // 捕捉信号 func catchSignal() { c := make(chan os.Signal) - signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM, - syscall.SIGUSR1, syscall.SIGUSR2) + // todo 配置热更新, windows 不支持 syscall.SIGUSR1, syscall.SIGUSR2 + signal.Notify(c, syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM) for { s := <- c logger.Info("收到信号 -- ", s) switch s { case syscall.SIGHUP, syscall.SIGINT, syscall.SIGTERM: - // 清理资源, 准备退出 + // 删除所有任务 + crontask.DefaultCronTask.DeleteAll() os.Exit(1) - case syscall.SIGUSR1, syscall.SIGUSR2: - // 热更新 } } } @@ -141,7 +141,7 @@ func becomeDaemon(ctx *cli.Context) { if os.Getppid() == InitProcess { - // 子进程不再处理 + // 已是守护进程,不再处理 return } diff --git a/modules/crontask/cron_task.go b/modules/crontask/cron_task.go index 63b033c..973ceb7 100644 --- a/modules/crontask/cron_task.go +++ b/modules/crontask/cron_task.go @@ -54,7 +54,7 @@ func (cronTask *CronTask) Add(name string, spec string, cmd cron.FuncJob) (err e } // 任务不存在则新增,任务已存在则删除后新增 -func (cronTask *CronTask) AddOrReplace(name string, spec string, cmd cron.FuncJob) error { +func (cronTask *CronTask) Update(name string, spec string, cmd cron.FuncJob) error { if cronTask.IsExist(name) { cronTask.Delete(name) } @@ -85,3 +85,10 @@ func (cronTask *CronTask) Delete(name string) { defer cronTask.Unlock() delete(cronTask.tasks, name) } + +// 删除所有任务 +func(cronTask *CronTask) DeleteAll() { + for taskName, _ := range(cronTask.tasks) { + cronTask.Delete(taskName) + } +} \ No newline at end of file diff --git a/service/task.go b/service/task.go index e0f6320..9ca9eb5 100644 --- a/service/task.go +++ b/service/task.go @@ -40,7 +40,7 @@ func (task *Task) Add(taskModel models.Task) { } // 定时任务 if taskModel.Type == models.Timing { - err := crontask.DefaultCronTask.AddOrReplace(strconv.Itoa(taskModel.Id), taskModel.Spec, taskFunc) + err := crontask.DefaultCronTask.Update(strconv.Itoa(taskModel.Id), taskModel.Spec, taskFunc) if err != nil { logger.Error(err) } @@ -120,9 +120,16 @@ func execSSHHandler(module string, taskModel models.Task) (string, error) { return "", nil } -func createTaskLog(taskId int) (int, error) { +func createTaskLog(taskModel models.Task) (int, error) { taskLogModel := new(models.TaskLog) - taskLogModel.TaskId = taskId + taskLogModel.Name = taskModel.Name + taskLogModel.Spec = taskModel.Spec + taskLogModel.Protocol = taskModel.Protocol + taskLogModel.Type = taskModel.Type + taskLogModel.Command = taskModel.Command + taskLogModel.Timeout = taskModel.Timeout + taskLogModel.Delay = taskModel.Delay + taskLogModel.SshHosts = taskModel.SshHosts taskLogModel.StartTime = time.Now() taskLogModel.Status = models.Running insertId, err := taskLogModel.Create() @@ -160,7 +167,7 @@ func createHandlerJob(taskModel models.Task) cron.FuncJob { return nil } taskFunc := func() { - taskLogId, err := createTaskLog(taskModel.Id) + taskLogId, err := createTaskLog(taskModel) if err != nil { logger.Error("写入任务日志失败-", err) return