-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
141 lines (115 loc) · 3.36 KB
/
executor.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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
package xxl
import (
"github.com/go-xxl/xxl/admin"
"github.com/go-xxl/xxl/job"
"github.com/go-xxl/xxl/server"
"github.com/go-xxl/xxl/utils"
"log"
"net/http"
"net/http/pprof"
)
type JobExecutor interface {
// Job add a job endpoint
Job(handlerName string, handler job.Func)
WithHealthCheck(path string, handler server.Handler)
WithDebug(isDebug bool)
BeforeHandler(handlers ...server.Handler)
AfterHandler(handlers ...server.Handler)
Run()
}
type HealthFunc func(ctx *server.Context)
// Executor Executor
type Executor struct {
opts Options
address string
engine *server.Engine
isPprof bool
}
// NewExecutor create a JobExecutor
func NewExecutor(opts ...Option) JobExecutor {
opt := Options{
ExecutorIp: utils.GetLocalIp(),
ExecutorPort: DefaultExecutorPort,
RegistryKey: DefaultRegistryKey,
}
for _, o := range opts {
o(&opt)
}
e := &Executor{
opts: opt,
}
e.address = utils.BuildEndPoint(opt.ExecutorIp, opt.ExecutorPort)
e.opts.RegistryValue = e.address
e.engine = server.New()
return e
}
// Run start job service
func (e *Executor) Run() {
biz := NewExecutorService()
e.engine.AddRoute("/run", biz.Run)
e.engine.AddRoute("/kill", biz.Kill)
e.engine.AddRoute("/log", biz.Log)
e.engine.AddRoute("/beat", biz.Beat)
e.engine.AddRoute("/idleBeat", biz.IdleBeat)
if e.isPprof {
e.engine.AddRoute("/debug/pprof/", e.WrapF(pprof.Index))
e.engine.AddRoute("/debug/pprof/cmdline", e.WrapF(pprof.Cmdline))
e.engine.AddRoute("/debug/pprof/profile", e.WrapF(pprof.Profile))
e.engine.AddRoute("/debug/pprof/symbol", e.WrapF(pprof.Symbol))
e.engine.AddRoute("/debug/pprof/trace", e.WrapF(pprof.Trace))
e.engine.AddRoute("/debug/pprof/heap", e.WrapF(pprof.Index))
e.engine.AddRoute("/debug/pprof/goroutine", e.WrapF(pprof.Index))
e.engine.AddRoute("/debug/pprof/allocs", e.WrapF(pprof.Index))
e.engine.AddRoute("/debug/pprof/block", e.WrapF(pprof.Index))
e.engine.AddRoute("/debug/pprof/mutex", e.WrapF(pprof.Index))
e.engine.AddRoute("/debug/pprof/threadcreate", e.WrapF(pprof.Index))
}
adm := admin.NewAdmApi()
adm.SetOpt(
admin.SetAccessToken(e.opts.AccessToken),
admin.SetAdmAddresses(e.opts.AdmAddresses),
admin.SetTimeout(e.opts.Timeout),
admin.SetRegistryKey(e.opts.RegistryKey),
admin.SetRegistryValue(e.opts.RegistryValue),
admin.SetRegistryGroup(admin.GetGroupName(admin.EXECUTOR)),
)
defer adm.RegistryRemove()
go func() {
adm.Register()
log.Fatalln(e.engine.Run(e.address))
}()
utils.WatchSignal()
}
// WithHealthCheck ExecutorService's web health check endpoint
func (e *Executor) WithHealthCheck(path string, handler server.Handler) {
if path != "" {
e.engine.AddRoute(path, handler)
}
}
// WithDebug open pprof
func (e *Executor) WithDebug(debug bool) {
e.isPprof = debug
}
func (e *Executor) Job(handlerName string, handler job.Func) {
job.GetAllHandlerList().Set(handlerName, job.Job{
Id: 0,
Name: "",
Ext: nil,
Cancel: nil,
Param: nil,
Fn: handler,
StartTime: 0,
EndTime: 0,
})
}
func (e *Executor) BeforeHandler(handlers ...server.Handler) {
e.engine.SetBeforeHandlers(handlers...)
}
func (e *Executor) AfterHandler(handlers ...server.Handler) {
e.engine.SetAfterHandler(handlers...)
}
func (e *Executor) WrapF(f func(w http.ResponseWriter, r *http.Request)) server.Handler {
return func(ctx *server.Context) {
f(ctx.Writer, ctx.Request)
}
}