提交 265017da authored 作者: mooncake9527's avatar mooncake9527

update

上级 c95efefe
...@@ -51,7 +51,7 @@ func (l *logger) Error(format string, a ...interface{}) { ...@@ -51,7 +51,7 @@ func (l *logger) Error(format string, a ...interface{}) {
} }
// 自定义中间件 // 自定义中间件
func customMiddleware(tf xxl.TaskFunc) xxl.TaskFunc { func customMiddleware(tf xxl.TaskFn) xxl.TaskFn {
return func(cxt context.Context, param *xxl.RunReq) string { return func(cxt context.Context, param *xxl.RunReq) string {
log.Println("I am a middleware start") log.Println("I am a middleware start")
res := tf(cxt, param) res := tf(cxt, param)
......
...@@ -23,7 +23,7 @@ type Executor interface { ...@@ -23,7 +23,7 @@ type Executor interface {
// Use 使用中间件 // Use 使用中间件
Use(middlewares ...Middleware) Use(middlewares ...Middleware)
// RegTask 注册任务 // RegTask 注册任务
RegTask(pattern string, task TaskFunc) RegTask(pattern string, task TaskFn)
// RunTask 运行任务 // RunTask 运行任务
RunTask(writer http.ResponseWriter, request *http.Request) RunTask(writer http.ResponseWriter, request *http.Request)
// KillTask 杀死任务 // KillTask 杀死任务
...@@ -119,7 +119,7 @@ func (e *executor) Stop() { ...@@ -119,7 +119,7 @@ func (e *executor) Stop() {
} }
// RegTask 注册任务 // RegTask 注册任务
func (e *executor) RegTask(pattern string, task TaskFunc) { func (e *executor) RegTask(pattern string, task TaskFn) {
var t = &Task{} var t = &Task{}
t.fn = e.chain(task) t.fn = e.chain(task)
e.regList.Set(pattern, t) e.regList.Set(pattern, t)
......
package xxl package xxl
// Middleware 中间件构造函数 // Middleware 中间件构造函数
type Middleware func(TaskFunc) TaskFunc type Middleware func(TaskFn) TaskFn
func (e *executor) chain(next TaskFunc) TaskFunc { func (e *executor) chain(next TaskFn) TaskFn {
for i := range e.middlewares { for i := range e.middlewares {
next = e.middlewares[len(e.middlewares)-1-i](next) next = e.middlewares[len(e.middlewares)-1-i](next)
} }
......
...@@ -6,16 +6,14 @@ import ( ...@@ -6,16 +6,14 @@ import (
"runtime/debug" "runtime/debug"
) )
// TaskFunc 任务执行函数 type TaskFn func(ctx context.Context, param *RunReq) string
type TaskFunc func(cxt context.Context, param *RunReq) string
// Task 任务
type Task struct { type Task struct {
Id int64 Id int64
Name string Name string
Ext context.Context Ext context.Context
Param *RunReq Param *RunReq
fn TaskFunc fn TaskFn
Cancel context.CancelFunc Cancel context.CancelFunc
StartTime int64 StartTime int64
EndTime int64 EndTime int64
...@@ -23,7 +21,6 @@ type Task struct { ...@@ -23,7 +21,6 @@ type Task struct {
log Logger log Logger
} }
// Run 运行任务
func (t *Task) Run(callback func(code int64, msg string)) { func (t *Task) Run(callback func(code int64, msg string)) {
defer func(cancel func()) { defer func(cancel func()) {
if err := recover(); err != nil { if err := recover(); err != nil {
...@@ -35,10 +32,8 @@ func (t *Task) Run(callback func(code int64, msg string)) { ...@@ -35,10 +32,8 @@ func (t *Task) Run(callback func(code int64, msg string)) {
}(t.Cancel) }(t.Cancel)
msg := t.fn(t.Ext, t.Param) msg := t.fn(t.Ext, t.Param)
callback(SuccessCode, msg) callback(SuccessCode, msg)
return
} }
// Info 任务信息
func (t *Task) Info() string { func (t *Task) Info() string {
return fmt.Sprintf("任务ID[%d]任务名称[%s]参数:%s", t.Id, t.Name, t.Param.ExecutorParams) return fmt.Sprintf("任务ID[%d]任务名称[%s]参数:%s", t.Id, t.Name, t.Param.ExecutorParams)
} }
Markdown 格式
0%
您添加了 0 到此讨论。请谨慎行事。
请先完成此评论的编辑!
注册 或者 后发表评论