Skip to content

Commit

Permalink
printstack
Browse files Browse the repository at this point in the history
  • Loading branch information
rushuinet committed Sep 19, 2020
1 parent d9a4fde commit 2b845aa
Show file tree
Hide file tree
Showing 2 changed files with 28 additions and 20 deletions.
43 changes: 26 additions & 17 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (e *executor) Run() (err error) {
Handler: mux,
}
// 监听端口并提供服务
log.Println("Starting server at " + e.address)
fmt.Println("Starting server at " + e.address)
go server.ListenAndServe()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGTERM)
Expand Down Expand Up @@ -101,7 +101,7 @@ func (e *executor) runTask(writer http.ResponseWriter, request *http.Request) {
log.Println("参数解析错误:" + string(req))
return
}
log.Printf("任务参数:%v", param)
fmt.Printf("任务参数:%v", param)
if !e.regList.Exists(param.ExecutorHandler) {
writer.Write(returnCall(param, 500, "Task not registered"))
log.Println("任务[" + Int64ToStr(param.JobID) + "]没有注册:" + param.ExecutorHandler)
Expand Down Expand Up @@ -137,7 +137,7 @@ func (e *executor) runTask(writer http.ResponseWriter, request *http.Request) {
go task.Run(func(code int64, msg string) {
e.callback(task, code, msg)
})
log.Println("任务[" + Int64ToStr(param.JobID) + "]开始执行:" + param.ExecutorHandler)
fmt.Println("任务[" + Int64ToStr(param.JobID) + "]开始执行:" + param.ExecutorHandler)
writer.Write(returnGeneral())
}

Expand Down Expand Up @@ -183,19 +183,28 @@ func (e *executor) registry() {
}
for {
<-t.C
result, err := e.post("/api/registry", string(param))
if err != nil {
log.Println("执行器注册失败:" + err.Error())
}
body, err := ioutil.ReadAll(result.Body)
res := &res{}
_ = json.Unmarshal(body, &res)
if res.Code != 200 {
log.Println("执行器注册失败:" + string(body))
}
log.Println("执行器注册成功:" + string(body))
_ = result.Body.Close()
t.Reset(time.Second * time.Duration(20)) //20秒心跳防止过期
func() {
result, err := e.post("/api/registry", string(param))
if err != nil {
log.Println("执行器注册失败1:" + err.Error())
return
}
defer result.Body.Close()
body, err := ioutil.ReadAll(result.Body)
if err != nil {
log.Println("执行器注册失败2:" + err.Error())
return
}
res := &res{}
_ = json.Unmarshal(body, &res)
if res.Code != 200 {
log.Println("执行器注册失败3:" + string(body))
return
}
fmt.Println("执行器注册成功:" + string(body))
}()

}
}

Expand All @@ -217,7 +226,7 @@ func (e *executor) registryRemove() {
log.Println("执行器摘除失败:" + err.Error())
}
body, err := ioutil.ReadAll(res.Body)
log.Println("执行器摘除成功:" + string(body))
fmt.Println("执行器摘除成功:" + string(body))
_ = res.Body.Close()
}

Expand All @@ -229,7 +238,7 @@ func (e *executor) callback(task *Task, code int64, msg string) {
}
body, err := ioutil.ReadAll(res.Body)
e.runList.Del(Int64ToStr(task.Id))
log.Println("任务回调成功:" + string(body))
fmt.Println("任务回调成功:" + string(body))
}

//post
Expand Down
5 changes: 2 additions & 3 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ import (
"context"
"fmt"
"log"
"runtime"
"runtime/debug"
)

//任务执行函数
Expand All @@ -27,8 +27,7 @@ func (t *Task) Run(callback func(code int64, msg string)) {
defer func(cancel func()) {
if err := recover(); err != nil {
log.Println(t.Info()+" panic: ", err)
_, file, line, _ := runtime.Caller(2)
log.Printf("panic file %s:%d", file, line) //堆栈跟踪
debug.PrintStack() //堆栈跟踪
callback(500, "task panic:"+fmt.Sprintf("%v", err))
cancel()
}
Expand Down

0 comments on commit 2b845aa

Please sign in to comment.