本文主要研究一下xxl-job-executor-gogit
//執行器 type Executor interface { //初始化 Init(...Option) //日誌查詢 LogHandler(handler LogHandler) //註冊任務 RegTask(pattern string, task TaskFunc) //運行任務 RunTask(writer http.ResponseWriter, request *http.Request) //殺死任務 KillTask(writer http.ResponseWriter, request *http.Request) //任務日誌 TaskLog(writer http.ResponseWriter, request *http.Request) //運行服務 Run() error }
Executor定義了Init、LogHandler、RegTask、RunTask、KillTask、TaskLog、Run方法
type executor struct { opts Options address string regList *taskList //註冊任務列表 runList *taskList //正在執行任務列表 mu sync.RWMutex log Logger logHandler LogHandler //日誌查詢handler }
executor定義了opts、address、regList、runList、mu、log、logHandler屬性
func (e *executor) Init(opts ...Option) { for _, o := range opts { o(&e.opts) } e.log = e.opts.l 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() }
Init方法遍歷opts應用opt,而後初始化regList、runList、address,最後異步e.registry()
//註冊任務 func (e *executor) RegTask(pattern string, task TaskFunc) { var t = &Task{} t.fn = task e.regList.Set(pattern, t) return }
RegTask方法往regList添加指定pattern的task
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")) e.log.Error("參數解析錯誤:" + string(req)) return } e.log.Info("任務參數:%v", param) if !e.regList.Exists(param.ExecutorHandler) { _, _ = writer.Write(returnCall(param, 500, "Task not registered")) e.log.Error("任務[" + Int64ToStr(param.JobID) + "]沒有註冊:" + param.ExecutorHandler) return } //阻塞策略處理 if e.runList.Exists(Int64ToStr(param.JobID)) { if param.ExecutorBlockStrategy == coverEarly { //覆蓋以前調度 oldTask := e.runList.Get(Int64ToStr(param.JobID)) if oldTask != nil { oldTask.Cancel() e.runList.Del(Int64ToStr(oldTask.Id)) } } else { //單機串行,丟棄後續調度 都進行阻塞 _, _ = writer.Write(returnCall(param, 500, "There are tasks running")) e.log.Error("任務[" + 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 task.log = e.log e.runList.Set(Int64ToStr(task.Id), task) go task.Run(func(code int64, msg string) { e.callback(task, code, msg) }) e.log.Info("任務[" + Int64ToStr(param.JobID) + "]開始執行:" + param.ExecutorHandler) _, _ = writer.Write(returnGeneral()) }
runTask方法先判斷task是否已經註冊了,則根據ExecutorBlockStrategy作不一樣處理,如果coverEarly則cancel掉已有的task;最後經過task.Run來異步執行任務
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)) e.log.Error("任務[" + Int64ToStr(param.JobID) + "]沒有運行") return } task := e.runList.Get(Int64ToStr(param.JobID)) task.Cancel() e.runList.Del(Int64ToStr(param.JobID)) _, _ = writer.Write(returnGeneral()) }
killTask方法則執行task.Cancel(),同時將其從runList移除
func (e *executor) taskLog(writer http.ResponseWriter, request *http.Request) { var res *LogRes data, err := ioutil.ReadAll(request.Body) req := &LogReq{} if err != nil { e.log.Error("日誌請求失敗:" + err.Error()) reqErrLogHandler(writer, req, err) return } err = json.Unmarshal(data, &req) if err != nil { e.log.Error("日誌請求解析失敗:" + err.Error()) reqErrLogHandler(writer, req, err) return } e.log.Info("日誌請求參數:%+v", req) if e.logHandler != nil { res = e.logHandler(req) } else { res = defaultLogHandler(req) } str, _ := json.Marshal(res) _, _ = writer.Write(str) }
taskLog方法經過e.logHandler(req)或者defaultLogHandler(req)來獲取日誌
xxl-job-executor-go的Executor定義了Init、LogHandler、RegTask、RunTask、KillTask、TaskLog、Run方法;executor實現了Executor接口,並提供了http的api接口。github