Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

修复任务执行超时后不向调度中心返回超时的bug #35

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 31 additions & 7 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,17 +25,41 @@ type Task struct {

// Run 运行任务
func (t *Task) Run(callback func(code int64, msg string)) {
defer func(cancel func()) {
done := make(chan *res, 1)
defer func() {
if err := recover(); err != nil {
t.log.Info(t.Info()+" panic: %v", err)
t.log.Error(t.Info()+" panic: %v", err)
debug.PrintStack() //堆栈跟踪
callback(500, "task panic:"+fmt.Sprintf("%v", err))
cancel()
msg := "task panic:" + fmt.Sprintf("%v", err)
result := &res{
Code: 500,
Msg: msg,
}
done <- result
}
}(t.Cancel)
}()
// 启动监测协程
go monitor(done, t, callback)
msg := t.fn(t.Ext, t.Param)
callback(200, msg)
return
result := &res{
Code: 200,
Msg: msg,
}
done <- result
}

// monitor 检测任务运行状态,任务执行结果回调
func monitor(done chan *res, task *Task, callback func(code int64, msg string)) {
select {
// 判断任务执行是否超时
case <-task.Ext.Done():
task.log.Error("time out")
callback(502, "job execute timeout")
return
case result := <-done:
callback(result.Code, result.Msg.(string))
return
}
}

// Info 任务信息
Expand Down