From 1136b75c32a11029631852223235cb39b2a113f6 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=E7=99=BD=E8=8C=B6=E6=B8=85=E6=AC=A2?= Date: Sat, 25 Jun 2022 19:42:25 +0800 Subject: [PATCH] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=E5=BC=82=E6=AD=A5=E4=BB=BB?= =?UTF-8?q?=E5=8A=A1panic=E6=9C=AA=E6=8D=95=E8=8E=B7=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- task.go | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) 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 {