同步错误返回

This commit is contained in:
苏尹岚
2019-12-04 09:17:06 +08:00
parent 2b979a4f68
commit 7f8615abfb
2 changed files with 111 additions and 43 deletions

View File

@@ -65,8 +65,7 @@ type ITask interface {
AddBatchErr(err error)
AddErrMsg(failedList ...interface{})
GetErrMsg() (failedList []interface{})
SetFinishHook(task *ParallelTask)
GetFinishHook() *ParallelTask
SetFinishHook(func(task ITask, ctx *jxcontext.Context))
json.Marshaler
}
@@ -132,7 +131,7 @@ type BaseTask struct {
ctx *jxcontext.Context
isGetResultCalled bool
FailedList []interface{}
finishHook *ParallelTask
finishHook func(task ITask, ctx *jxcontext.Context)
}
func (s TaskList) Len() int {
@@ -174,6 +173,9 @@ func (t *BaseTask) GetID() string {
return t.ID
}
func (t *BaseTask) Run() {
}
// 此函数成功返回结果后结果在任务中会被删除以免被管理的任务不必要的HOLD住对象
func (t *BaseTask) GetResult(duration time.Duration) (retVal []interface{}, err error) {
if t.GetStatus() >= TaskStatusEndBegin {
@@ -360,26 +362,19 @@ func (t *BaseTask) Error() (errMsg string) {
return errMsg
}
func (t *BaseTask) SetFinishHook(task *ParallelTask) {
func (t *BaseTask) SetFinishHook(hook func(task ITask, ctx *jxcontext.Context)) {
t.locker.RLock()
defer t.locker.RUnlock()
t.finishHook = task
}
func (t *BaseTask) GetFinishHook() *ParallelTask {
return t.finishHook
t.finishHook = hook
}
func (t *BaseTask) GetErrMsg() (failedList []interface{}) {
t.locker.RLock()
defer t.locker.RUnlock()
if len(t.FailedList) == 0 {
return nil
}
if t.parent != nil {
for _, v := range t.FailedList {
failedList = append(failedList, v)
}
failedList = append(failedList, t.FailedList...)
t.locker.RUnlock()
for _, v := range t.Children {
failedList = append(failedList, v.GetErrMsg()...)
}
return failedList
}
@@ -387,9 +382,7 @@ func (t *BaseTask) GetErrMsg() (failedList []interface{}) {
func (t *BaseTask) AddErrMsg(failedList ...interface{}) {
t.locker.Lock()
defer t.locker.Unlock()
for _, v := range failedList {
t.FailedList = append(t.FailedList, v)
}
t.FailedList = append(t.FailedList, failedList...)
}
// func (t *BaseTask) GetDetailErrList() []error {
@@ -427,8 +420,7 @@ 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, "", r)
globals.SugarLogger.Errorf("panic in BaseTask.run task:%s, task detail:%s, r:%v", t.Name, utils.Format4Output(t, false), r)
}
}()
@@ -462,30 +454,27 @@ func (t *BaseTask) run(taskHandler func()) {
globals.SugarLogger.Infof("BaseTask run, failed with error:%v", err)
}
}
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)
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()
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
}
ddmsg.SendUserMessage(dingdingapi.MsgTyeText, authInfo.UserID, "异步任务完成", content)
} else {
content += ",\n" + t.Error()
}
ddmsg.SendUserMessage(dingdingapi.MsgTyeText, authInfo.UserID, "异步任务完成", content)
}
} else {
}
if t.finishHook != nil {
t.finishHook(t,t.ctx)
}
})
}