修复异步任务panic未捕获问题

This commit is contained in:
白茶清欢 2022-06-25 19:42:25 +08:00
parent a7552c210d
commit 1136b75c32

21
task.go
View File

@ -97,6 +97,17 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
defer func() { defer func() {
result.FinishTime = time.Now().UnixNano() result.FinishTime = time.Now().UnixNano()
result.Used = result.FinishTime - result.StartTime result.Used = result.FinishTime - result.StartTime
}()
d.lock.RLock()
if taskInstance, exist = d.taskTable[cfg.TaskName]; !exist {
result.Err = fmt.Errorf("%v 任务未注册", cfg.TaskName)
}
d.lock.RUnlock()
result.TaskRunID = taskInstance.GetRunID()
result.TaskDescription = taskInstance.Description()
if cfg.Async {
// 异步运行
go func() {
if e := recover(); nil != e { if e := recover(); nil != e {
switch e.(type) { switch e.(type) {
case runtime.Error: // 运行时错误 case runtime.Error: // 运行时错误
@ -105,16 +116,6 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result {
result.Err = fmt.Errorf("出现其他场景Panic : %v", e) result.Err = fmt.Errorf("出现其他场景Panic : %v", e)
} }
} }
}()
d.lock.RLock()
if taskInstance, exist = d.taskTable[cfg.TaskName]; !exist {
result.Err = fmt.Errorf("%v 任务未注册", cfg.TaskName)
}
result.TaskRunID = taskInstance.GetRunID()
result.TaskDescription = taskInstance.Description()
if cfg.Async {
// 异步运行
go func() {
result.Data, result.Err = taskInstance.Execute(ctx, cfg) result.Data, result.Err = taskInstance.Execute(ctx, cfg)
}() }()
} else { } else {