老师 超时处理代码:
func (e *Executor) JobOvertimeHandle(result *basics.JobExecuteResult) {
go func(result *basics.JobExecuteResult) {
//获取当前时间
var(
seconds time.Duration
overtime time.Time
)
//格式化时间
seconds , _ = time.ParseDuration("-" + strconv.FormatInt(result.ExecuteInfo.Job.Overtime , 10))
for {
//超时时间
overtime = result.StartTime.Add(seconds)
//如果超时时间大于或等于当前时间 退出当前进程
if overtime.Before(result.StartTime) || overtime.Equal(result.StartTime){
result.ExecuteInfo.CancelFunc() //触发command杀死shell子进程, 任务得到退出
return
}
time.Sleep(1 * time.Second)
}
}(result)
}
结果:
执行任务: job1 2019-11-11 04:28:48 +0800 CST 2019-11-11 04:28:48.0000114 +0800 CST m=+0.889000001
执行任务: job3 2019-11-11 04:28:48 +0800 CST 2019-11-11 04:28:48.0000114 +0800 CST m=+0.889000001
任务执行完成: job3 context canceled
任务执行完成: job1 context canceled
执行任务: job3 2019-11-11 04:28:51 +0800 CST 2019-11-11 04:28:51.0000114 +0800 CST m=+3.889000001
执行任务: job1 2019-11-11 04:28:51 +0800 CST 2019-11-11 04:28:51.0000114 +0800 CST m=+3.889000001
任务执行完成: job3 context canceled
任务执行完成: job1 context canceled
执行任务: job1 2019-11-11 04:28:54 +0800 CST 2019-11-11 04:28:54.0000114 +0800 CST m=+6.889000001
执行任务: job3 2019-11-11 04:28:54 +0800 CST 2019-11-11 04:28:54.0000114 +0800 CST m=+6.889000001
任务执行完成: job3 context canceled
任务执行完成: job1 context canceled
老师这是超时终止的结果吗?