聊聊xxl-job-executor-go

codecraft

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

Executor

//执行器
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 //正在执行任务列表
    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("参数解析错误:" + 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来异步执行任务

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方法则执行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("日志请求参数:%+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接口。

doc

阅读 243

code-craft
spring boot , docker and so on 欢迎关注微信公众号: geek_luandun

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下了或重如泰山或轻如鸿毛的几笔。

11.5k 声望
1.9k 粉丝
0 条评论
你知道吗?

当一个代码的工匠回首往事时,不因虚度年华而悔恨,也不因碌碌无为而羞愧,这样,当他老的时候,可以很自豪告诉世人,我曾经将代码注入生命去打造互联网的浪潮之巅,那是个很疯狂的时代,我在一波波的浪潮上留下了或重如泰山或轻如鸿毛的几笔。

11.5k 声望
1.9k 粉丝
宣传栏