Skip to content

Commit

Permalink
panic处理
Browse files Browse the repository at this point in the history
  • Loading branch information
rushuinet committed Aug 13, 2020
1 parent 47165c2 commit 69c4c6f
Show file tree
Hide file tree
Showing 5 changed files with 28 additions and 13 deletions.
1 change: 1 addition & 0 deletions example/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,5 +16,6 @@ func main() {
exec.Init()
exec.RegTask("task.test", task.Test)
exec.RegTask("task.test2", task.Test2)
exec.RegTask("task.panic", task.Panic)
exec.Run()
}
10 changes: 10 additions & 0 deletions example/task/panic.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
package task

import (
"context"
xxl "github.com/xxl-job/go-client"
)

func Panic(cxt context.Context, param *xxl.RunReq) {
panic("test panic")
}
10 changes: 5 additions & 5 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -97,7 +97,7 @@ func (e *executor) runTask(writer http.ResponseWriter, request *http.Request) {
param := &RunReq{}
json.Unmarshal(req, &param)
if !e.regList.Exists(param.ExecutorHandler) {
writer.Write(returnCall(param, 500))
writer.Write(returnCall(param, 500, "Task not registered"))
log.Println("任务[" + Int64ToStr(param.JobID) + "]没有注册:" + param.ExecutorHandler)
return
}
Expand All @@ -107,8 +107,8 @@ func (e *executor) runTask(writer http.ResponseWriter, request *http.Request) {
task.Name = param.ExecutorHandler
task.Param = param
e.runList.Set(Int64ToStr(param.JobID), task)
go task.Run(func() {
e.callback(task)
go task.Run(func(code int64, msg string) {
e.callback(task, code, msg)
})
log.Println("任务[" + Int64ToStr(param.JobID) + "]开始执行:" + param.ExecutorHandler)
writer.Write(returnGeneral())
Expand Down Expand Up @@ -196,8 +196,8 @@ func (e *executor) registryRemove() {
}

//回调任务列表
func (e *executor) callback(task *Task) {
res, err := e.post("/api/callback", string(returnCall(task.Param, 200)))
func (e *executor) callback(task *Task, code int64, msg string) {
res, err := e.post("/api/callback", string(returnCall(task.Param, code, msg)))
if err != nil {
fmt.Println(err)
}
Expand Down
13 changes: 11 additions & 2 deletions task.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@ package xxl

import (
"context"
"fmt"
"log"
)

//任务执行函数
Expand All @@ -20,10 +22,17 @@ type Task struct {
}

//运行任务
func (t *Task) Run(callback func()) {
func (t *Task) Run(callback func(code int64, msg string)) {
t.Ext, t.Cancel = context.WithCancel(context.Background())
defer func(cancel func()) {
if err := recover(); err != nil {
log.Println(t.Info()+" panic: ", err)
callback(500, "task panic:"+fmt.Sprintf("%v", err))
cancel()
}
}(t.Cancel)
t.fn(t.Ext, t.Param)
callback()
callback(200, "")
return
}

Expand Down
7 changes: 1 addition & 6 deletions util.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,11 +17,7 @@ func StrToInt64(str string) int64 {
}

//执行任务回调
func returnCall(req *RunReq, code int64) []byte {
msg := ""
if code != 200 {
msg = "Task not registered"
}
func returnCall(req *RunReq, code int64, msg string) []byte {
data := call{
&callElement{
LogID: req.LogID,
Expand All @@ -36,7 +32,6 @@ func returnCall(req *RunReq, code int64) []byte {
return str
}


//杀死任务返回
func returnKill(req *killReq, code int64) []byte {
msg := ""
Expand Down

0 comments on commit 69c4c6f

Please sign in to comment.