聊聊xxl-job-executor-go

本文主要研究一下xxl-job-executor-go

Executor

//執(zhí)行器
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方法

executor

type executor struct {
    opts    Options
    address string
    regList *taskList //注冊任務列表
    runList *taskList //正在執(zhí)行任務列表
    mu      sync.RWMutex
    log     Logger

    logHandler LogHandler //日志查詢handler
}

executor定義了opts、address、regList、runList、mu、log、logHandler屬性

Init

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()

RegTask

//注冊任務
func (e *executor) RegTask(pattern string, task TaskFunc) {
    var t = &Task{}
    t.fn = task
    e.regList.Set(pattern, t)
    return
}

RegTask方法往regList添加指定pattern的task

runTask

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, &param)
    if err != nil {
        _, _ = writer.Write(returnCall(param, 500, "params err"))
        e.log.Error("參數(shù)解析錯誤:" + string(req))
        return
    }
    e.log.Info("任務參數(shù):%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 { //覆蓋之前調(diào)度
            oldTask := e.runList.Get(Int64ToStr(param.JobID))
            if oldTask != nil {
                oldTask.Cancel()
                e.runList.Del(Int64ToStr(oldTask.Id))
            }
        } else { //單機串行,丟棄后續(xù)調(diào)度 都進行阻塞
            _, _ = writer.Write(returnCall(param, 500, "There are tasks running"))
            e.log.Error("任務[" + Int64ToStr(param.JobID) + "]已經(jīng)在運行了:" + 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) + "]開始執(zhí)行:" + param.ExecutorHandler)
    _, _ = writer.Write(returnGeneral())
}

runTask方法先判斷task是否已經(jīng)注冊了,則根據(jù)ExecutorBlockStrategy做不同處理,若是coverEarly則cancel掉已有的task;最后通過task.Run來異步執(zhí)行任務

killTask

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, &param)
    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方法則執(zhí)行task.Cancel(),同時將其從runList移除

taskLog

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("日志請求參數(shù):%+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)來獲取日志

小結(jié)

xxl-job-executor-go的Executor定義了Init、LogHandler、RegTask、RunTask、KillTask、TaskLog、Run方法;executor實現(xiàn)了Executor接口,并提供了http的api接口。

doc

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時請結(jié)合常識與多方信息審慎甄別。
平臺聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點,簡書系信息發(fā)布平臺,僅提供信息存儲服務。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容