Skip to content

Commit

Permalink
add middlewares
Browse files Browse the repository at this point in the history
  • Loading branch information
rushuinet committed May 17, 2023
1 parent bac6dce commit a9a6830
Show file tree
Hide file tree
Showing 5 changed files with 72 additions and 21 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@
# Dependency directories (remove the comment below to include it)
# vendor/
go.sum
.idea/
37 changes: 27 additions & 10 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@
9.可自定义日志
10.自定义日志查看handler
11.支持外部路由(可与gin集成)
12.支持自定义中间件
```

# Example
```
```go
package main

import (
"context"
"fmt"
xxl "github.com/xxl-job/xxl-job-executor-go"
"github.com/xxl-job/xxl-job-executor-go/example/task"
Expand All @@ -36,23 +38,27 @@ func main() {
xxl.SetLogger(&logger{}), //自定义日志
)
exec.Init()
exec.Use(customMiddleware)
//设置日志查看handler
exec.LogHandler(func(req *xxl.LogReq) *xxl.LogRes {
return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
FromLineNum: req.FromLineNum,
ToLineNum: 2,
LogContent: "这个是自定义日志handler",
IsEnd: true,
}}
})
exec.LogHandler(customLogHandle)
//注册任务handler
exec.RegTask("task.test", task.Test)
exec.RegTask("task.test2", task.Test2)
exec.RegTask("task.panic", task.Panic)
log.Fatal(exec.Run())
}

//xxl.Logger接口实现
// 自定义日志处理器
func customLogHandle(req *xxl.LogReq) *xxl.LogRes {
return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
FromLineNum: req.FromLineNum,
ToLineNum: 2,
LogContent: "这个是自定义日志handler",
IsEnd: true,
}}
}

// xxl.Logger接口实现
type logger struct{}

func (l *logger) Info(format string, a ...interface{}) {
Expand All @@ -62,6 +68,17 @@ func (l *logger) Info(format string, a ...interface{}) {
func (l *logger) Error(format string, a ...interface{}) {
log.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}

// 自定义中间件
func customMiddleware(tf xxl.TaskFunc) xxl.TaskFunc {
return func(cxt context.Context, param *xxl.RunReq) string {
log.Println("I am a middleware start")
res := tf(cxt, param)
log.Println("I am a middleware end")
return res
}
}

```
# 示例项目
github.com/xxl-job/xxl-job-executor-go/example/
Expand Down
33 changes: 24 additions & 9 deletions example/main.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package main

import (
"context"
"fmt"
xxl "github.com/xxl-job/xxl-job-executor-go"
"github.com/xxl-job/xxl-job-executor-go/example/task"
Expand All @@ -17,23 +18,27 @@ func main() {
xxl.SetLogger(&logger{}), //自定义日志
)
exec.Init()
exec.Use(customMiddleware)
//设置日志查看handler
exec.LogHandler(func(req *xxl.LogReq) *xxl.LogRes {
return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
FromLineNum: req.FromLineNum,
ToLineNum: 2,
LogContent: "这个是自定义日志handler",
IsEnd: true,
}}
})
exec.LogHandler(customLogHandle)
//注册任务handler
exec.RegTask("task.test", task.Test)
exec.RegTask("task.test2", task.Test2)
exec.RegTask("task.panic", task.Panic)
log.Fatal(exec.Run())
}

//xxl.Logger接口实现
// 自定义日志处理器
func customLogHandle(req *xxl.LogReq) *xxl.LogRes {
return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
FromLineNum: req.FromLineNum,
ToLineNum: 2,
LogContent: "这个是自定义日志handler",
IsEnd: true,
}}
}

// xxl.Logger接口实现
type logger struct{}

func (l *logger) Info(format string, a ...interface{}) {
Expand All @@ -43,3 +48,13 @@ func (l *logger) Info(format string, a ...interface{}) {
func (l *logger) Error(format string, a ...interface{}) {
log.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}

// 自定义中间件
func customMiddleware(tf xxl.TaskFunc) xxl.TaskFunc {
return func(cxt context.Context, param *xxl.RunReq) string {
log.Println("I am a middleware start")
res := tf(cxt, param)
log.Println("I am a middleware end")
return res
}
}
11 changes: 9 additions & 2 deletions executor.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@ type Executor interface {
Init(...Option)
// LogHandler 日志查询
LogHandler(handler LogHandler)
// Use 使用中间件
Use(middlewares ...Middleware)
// RegTask 注册任务
RegTask(pattern string, task TaskFunc)
// RunTask 运行任务
Expand Down Expand Up @@ -59,7 +61,8 @@ type executor struct {
mu sync.RWMutex
log Logger

logHandler LogHandler //日志查询handler
logHandler LogHandler //日志查询handler
middlewares []Middleware //中间件
}

func (e *executor) Init(opts ...Option) {
Expand All @@ -82,6 +85,10 @@ func (e *executor) LogHandler(handler LogHandler) {
e.logHandler = handler
}

func (e *executor) Use(middlewares ...Middleware) {
e.middlewares = middlewares
}

func (e *executor) Run() (err error) {
// 创建路由器
mux := http.NewServeMux()
Expand Down Expand Up @@ -114,7 +121,7 @@ func (e *executor) Stop() {
// RegTask 注册任务
func (e *executor) RegTask(pattern string, task TaskFunc) {
var t = &Task{}
t.fn = task
t.fn = e.chain(task)
e.regList.Set(pattern, t)
return
}
Expand Down
11 changes: 11 additions & 0 deletions middleware.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
package xxl

// Middleware 中间件构造函数
type Middleware func(TaskFunc) TaskFunc

func (e *executor) chain(next TaskFunc) TaskFunc {
for i := range e.middlewares {
next = e.middlewares[len(e.middlewares)-1-i](next)
}
return next
}

0 comments on commit a9a6830

Please sign in to comment.