forked from xxl-job/xxl-job-executor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathexecutor.go
256 lines (237 loc) · 6.45 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
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
package xxl
import (
"context"
"encoding/json"
"fmt"
"io/ioutil"
"log"
"net/http"
"os"
"os/signal"
"strings"
"sync"
"syscall"
"time"
)
//执行器
type Executor interface {
//初始化
Init(...Option)
RegTask(pattern string, task TaskFunc)
Run() error
}
//创建执行器
func NewExecutor(opts ...Option) Executor {
return newExecutor(opts...)
}
func newExecutor(opts ...Option) *executor {
options := newOptions(opts...)
executor := &executor{
opts: options,
}
return executor
}
type executor struct {
opts Options
address string
regList *taskList //注册任务列表
runList *taskList //正在执行任务列表
mu sync.RWMutex
}
func (e *executor) Init(opts ...Option) {
for _, o := range opts {
o(&e.opts)
}
e.regList = &taskList{
data: make(map[string]*Task),
}
e.runList = &taskList{
data: make(map[string]*Task),
}
e.address = e.opts.ExecutorIp + ":" + e.opts.ExecutorPort
go e.registry()
}
func (e *executor) Run() (err error) {
// 创建路由器
mux := http.NewServeMux()
// 设置路由规则
mux.HandleFunc("/run", e.runTask)
mux.HandleFunc("/kill", e.killTask)
mux.HandleFunc("/log", e.taskLog)
// 创建服务器
server := &http.Server{
Addr: e.address,
WriteTimeout: time.Second * 3,
Handler: mux,
}
// 监听端口并提供服务
fmt.Println("Starting server at " + e.address)
go server.ListenAndServe()
quit := make(chan os.Signal)
signal.Notify(quit, syscall.SIGKILL, syscall.SIGQUIT, syscall.SIGINT, syscall.SIGTERM)
<-quit
e.registryRemove()
return nil
}
//注册任务
func (e *executor) RegTask(pattern string, task TaskFunc) {
var t = &Task{}
t.fn = task
e.regList.Set(pattern, t)
return
}
//运行一个任务
func (e *executor) runTask(writer http.ResponseWriter, request *http.Request) {
e.mu.Lock()
defer e.mu.Unlock()
req, _ := ioutil.ReadAll(request.Body)
param := &RunReq{}
err := json.Unmarshal(req, ¶m)
if err != nil {
writer.Write(returnCall(param, 500, "params err"))
log.Println("参数解析错误:" + string(req))
return
}
fmt.Printf("任务参数:%v", param)
if !e.regList.Exists(param.ExecutorHandler) {
writer.Write(returnCall(param, 500, "Task not registered"))
log.Println("任务[" + Int64ToStr(param.JobID) + "]没有注册:" + param.ExecutorHandler)
return
}
cxt := context.Background()
task := e.regList.Get(param.ExecutorHandler)
if param.ExecutorTimeout > 0 {
task.Ext, task.Cancel = context.WithTimeout(cxt, time.Duration(param.ExecutorTimeout)*time.Second)
} else {
task.Ext, task.Cancel = context.WithCancel(cxt)
}
task.Id = param.JobID
task.Name = param.ExecutorHandler
task.Param = param
//阻塞策略处理
if e.runList.Exists(Int64ToStr(task.Id)) {
if param.ExecutorBlockStrategy == coverEarly { //覆盖之前调度
oldTask := e.runList.Get(Int64ToStr(task.Id))
if oldTask != nil {
oldTask.Cancel()
e.runList.Del(Int64ToStr(task.Id))
}
} else { //单机串行,丢弃后续调度 都进行阻塞
writer.Write(returnCall(param, 500, "There are tasks running"))
log.Println("任务[" + Int64ToStr(param.JobID) + "]已经在运行了:" + param.ExecutorHandler)
return
}
}
e.runList.Set(Int64ToStr(task.Id), task)
go task.Run(func(code int64, msg string) {
e.callback(task, code, msg)
})
fmt.Println("任务[" + Int64ToStr(param.JobID) + "]开始执行:" + param.ExecutorHandler)
writer.Write(returnGeneral())
}
//删除一个任务
func (e *executor) killTask(writer http.ResponseWriter, request *http.Request) {
e.mu.Lock()
defer e.mu.Unlock()
req, _ := ioutil.ReadAll(request.Body)
param := &killReq{}
_ = json.Unmarshal(req, ¶m)
if !e.runList.Exists(Int64ToStr(param.JobID)) {
writer.Write(returnKill(param, 500))
log.Println("任务[" + Int64ToStr(param.JobID) + "]没有运行")
return
}
task := e.runList.Get(Int64ToStr(param.JobID))
task.Cancel()
e.runList.Del(Int64ToStr(param.JobID))
writer.Write(returnGeneral())
}
//任务日志
func (e *executor) taskLog(writer http.ResponseWriter, request *http.Request) {
data, _ := ioutil.ReadAll(request.Body)
req := &logReq{}
_ = json.Unmarshal(data, &req)
writer.Write(returnLog(req, 200))
}
//注册执行器到调度中心
func (e *executor) registry() {
t := time.NewTimer(time.Second * 0) //初始立即执行
defer t.Stop()
req := &Registry{
RegistryGroup: "EXECUTOR",
RegistryKey: e.opts.RegistryKey,
RegistryValue: "http://" + e.address,
}
param, err := json.Marshal(req)
if err != nil {
log.Fatal("执行器注册信息解析失败:" + err.Error())
}
for {
<-t.C
t.Reset(time.Second * time.Duration(20)) //20秒心跳防止过期
func() {
result, err := e.post("/api/registry", string(param))
if err != nil {
log.Println("执行器注册失败1:" + err.Error())
return
}
defer result.Body.Close()
body, err := ioutil.ReadAll(result.Body)
if err != nil {
log.Println("执行器注册失败2:" + err.Error())
return
}
res := &res{}
_ = json.Unmarshal(body, &res)
if res.Code != 200 {
log.Println("执行器注册失败3:" + string(body))
return
}
fmt.Println("执行器注册成功:" + string(body))
}()
}
}
//执行器注册摘除
func (e *executor) registryRemove() {
t := time.NewTimer(time.Second * 0) //初始立即执行
defer t.Stop()
req := &Registry{
RegistryGroup: "EXECUTOR",
RegistryKey: e.opts.RegistryKey,
RegistryValue: "http://" + e.address,
}
param, err := json.Marshal(req)
if err != nil {
log.Println("执行器摘除失败:" + err.Error())
}
res, err := e.post("/api/registryRemove", string(param))
if err != nil {
log.Println("执行器摘除失败:" + err.Error())
}
body, err := ioutil.ReadAll(res.Body)
fmt.Println("执行器摘除成功:" + string(body))
_ = res.Body.Close()
}
//回调任务列表
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)
}
body, err := ioutil.ReadAll(res.Body)
e.runList.Del(Int64ToStr(task.Id))
fmt.Println("任务回调成功:" + string(body))
}
//post
func (e *executor) post(action, body string) (resp *http.Response, err error) {
request, err := http.NewRequest("POST", e.opts.ServerAddr+action, strings.NewReader(body))
if err != nil {
return nil, err
}
request.Header.Set("Content-Type", "application/json;charset=UTF-8")
request.Header.Set("XXL-JOB-ACCESS-TOKEN", e.opts.AccessToken)
client := http.Client{
Timeout: e.opts.Timeout,
}
return client.Do(request)
}