-
Notifications
You must be signed in to change notification settings - Fork 3
/
task.go
44 lines (39 loc) · 880 Bytes
/
task.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
package xxl
import (
"context"
"fmt"
"runtime/debug"
)
//任务执行函数
type TaskFunc func(cxt context.Context, param *RunReq) string
//任务
type Task struct {
Id int64
Name string
Ext context.Context
Param *RunReq
fn TaskFunc
Cancel context.CancelFunc
StartTime int64
EndTime int64
//日志
log Logger
}
//运行任务
func (t *Task) Run(callback func(code int64, msg string)) {
defer func(cancel func()) {
if err := recover(); err != nil {
t.log.Info(t.Info()+" panic: %v", err)
debug.PrintStack() //堆栈跟踪
callback(500, "task panic:"+fmt.Sprintf("%v", err))
cancel()
}
}(t.Cancel)
msg := t.fn(t.Ext, t.Param)
callback(200, msg)
return
}
//任务信息
func (t *Task) Info() string {
return "任务ID[" + Int64ToStr(t.Id) + "]任务名称[" + t.Name + "]参数:" + t.Param.ExecutorParams
}