diff --git a/task.go b/task.go index 05725f1..937af1f 100644 --- a/task.go +++ b/task.go @@ -97,24 +97,25 @@ func (d *dispatch) Run(ctx context.Context, cfg *Config) *Result { defer func() { result.FinishTime = time.Now().UnixNano() result.Used = result.FinishTime - result.StartTime - if e := recover(); nil != e { - switch e.(type) { - case runtime.Error: // 运行时错误 - result.Err = fmt.Errorf("出现运行时Panic : %v", e) - default: // 非运行时错误 - 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) } + d.lock.RUnlock() result.TaskRunID = taskInstance.GetRunID() result.TaskDescription = taskInstance.Description() if cfg.Async { // 异步运行 go func() { + if e := recover(); nil != e { + switch e.(type) { + case runtime.Error: // 运行时错误 + result.Err = fmt.Errorf("出现运行时Panic : %v", e) + default: // 非运行时错误 + result.Err = fmt.Errorf("出现其他场景Panic : %v", e) + } + } result.Data, result.Err = taskInstance.Execute(ctx, cfg) }() } else {