mirror of https://github.com/ouqiang/gocron
应用退出时,清空定时任务
parent
c25e4f9e61
commit
b3b926fcbd
|
@ -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
|
||||
- GO15VENDOREXPERIMENT=1
|
14
cmd/web.go
14
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
|
||||
}
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
|
@ -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
|
||||
|
|
Loading…
Reference in New Issue