diff --git a/business/jxstore/cms/sync.go b/business/jxstore/cms/sync.go index 8e62ed74d..46a304bf6 100644 --- a/business/jxstore/cms/sync.go +++ b/business/jxstore/cms/sync.go @@ -540,11 +540,9 @@ func (v *VendorSync) LoopStoresMap2(ctx *jxcontext.Context, db *dao.DaoDB, taskN taskName = fmt.Sprintf("%s,处理平台%s", taskName, model.VendorChineseNames[loopInfoList[0].VendorID]) } task = tasksch.NewParallelTask(taskName, tasksch.NewParallelConfig().SetIsContinueWhenError(true), ctx, handler, loopInfoList) - task.SetFinishHook(func(task tasksch.ITask) { - fmt.Println("test2") - failedList := task.GetErrMsg() - fmt.Println(failedList) - }) + task.SetFinishHook(task) + failedList := task.GetErrMsg() + tasksch.HandleTask(task, nil, isManageIt).Run() if !isAsync { resultList, err2 := task.GetResult(0) diff --git a/business/jxutils/tasksch/task.go b/business/jxutils/tasksch/task.go index a221a7787..ca52b2f06 100644 --- a/business/jxutils/tasksch/task.go +++ b/business/jxutils/tasksch/task.go @@ -65,8 +65,8 @@ type ITask interface { AddBatchErr(err error) AddErrMsg(failedList ...interface{}) GetErrMsg() (failedList []interface{}) - SetFinishHook(func(task ITask)) - GetFinishHook() func(task ITask) + SetFinishHook(task *ParallelTask) + GetFinishHook() *ParallelTask json.Marshaler } @@ -132,7 +132,7 @@ type BaseTask struct { ctx *jxcontext.Context isGetResultCalled bool FailedList []interface{} - FinishHook func(task ITask) + finishHook *ParallelTask } func (s TaskList) Len() int { @@ -230,10 +230,6 @@ func (t *BaseTask) GetTotalJobCount() int { return t.TotalJobCount } -func (t *BaseTask) GetFinishHook() func(ITask) { - return t.FinishHook -} - func (t *BaseTask) GetFinishedJobCount() int { t.locker.RLock() defer t.locker.RUnlock() @@ -364,8 +360,14 @@ func (t *BaseTask) Error() (errMsg string) { return errMsg } -func (t *BaseTask) SetFinishHook(hook func(task ITask)) { - t.FinishHook = hook +func (t *BaseTask) SetFinishHook(task *ParallelTask) { + t.locker.RLock() + defer t.locker.RUnlock() + t.finishHook = task +} + +func (t *BaseTask) GetFinishHook() *ParallelTask { + return t.finishHook } func (t *BaseTask) GetErrMsg() (failedList []interface{}) { @@ -425,7 +427,8 @@ func (t *BaseTask) run(taskHandler func()) { utils.CallFuncAsync(func() { defer func() { if r := recover(); r != nil { - globals.SugarLogger.Errorf("panic in BaseTask.run task:%s, task detail:%s, r:%v", t.Name, utils.Format4Output(t, false), r) + // globals.SugarLogger.Errorf("panic in BaseTask.run task:%s, task detail:%s, r:%v", t.Name, utils.Format4Output(t, false), r) + globals.SugarLogger.Errorf("panic in BaseTask.run task:%s, task detail:%s, r:%v", t.Name, "", r) } }() @@ -463,23 +466,27 @@ func (t *BaseTask) run(taskHandler func()) { close(t.finishChan) time.Sleep(10 * time.Millisecond) // 等待GetResult中的isGetResultCalled赋值 globals.SugarLogger.Debugf("BaseTask task ID:%s, name:%s finished, isGetResultCalled:%t", t.ID, t.Name, t.isGetResultCalled) - if !t.isGetResultCalled && t.parent == nil && len(GetTasks(t.ID, TaskStatusBegin, TaskStatusEnd, 24, "")) > 0 { - if authInfo, err := t.ctx.GetV2AuthInfo(); err == nil { // 这里应该是不管登录类型,直接以可能的方式发消息 - var content string - taskDesc := fmt.Sprintf("你的异步任务[%s],ID[%s],开始于:%s,结束于:%s,", t.Name, t.ID, utils.Time2Str(t.CreatedAt), utils.Time2Str(t.TerminatedAt)) - content = fmt.Sprintf("%s执行%s", taskDesc, TaskStatusName[t.Status]) - if t.Error() == "" { - noticeMsg := t.GetNoticeMsg() - if noticeMsg != "" { - content += ",通知消息:" + noticeMsg + p := t.GetFinishHook() + if p != nil { + if !t.isGetResultCalled && t.parent == nil && len(GetTasks(t.ID, TaskStatusBegin, TaskStatusEnd, 24, "")) > 0 { + if authInfo, err := t.ctx.GetV2AuthInfo(); err == nil { // 这里应该是不管登录类型,直接以可能的方式发消息 + var content string + taskDesc := fmt.Sprintf("你的异步任务[%s],ID[%s],开始于:%s,结束于:%s,", t.Name, t.ID, utils.Time2Str(t.CreatedAt), utils.Time2Str(t.TerminatedAt)) + content = fmt.Sprintf("%s执行%s", taskDesc, TaskStatusName[t.Status]) + if t.Error() == "" { + noticeMsg := t.GetNoticeMsg() + if noticeMsg != "" { + content += ",通知消息:" + noticeMsg + } + } else { + content += ",\n" + t.Error() } - } else { - content += ",\n" + t.Error() + ddmsg.SendUserMessage(dingdingapi.MsgTyeText, authInfo.UserID, "异步任务完成", content) } - ddmsg.SendUserMessage(dingdingapi.MsgTyeText, authInfo.UserID, "异步任务完成", content) } + } else { + } - t.GetFinishHook() }) } }