Categorygithub.com/xxl-job/xxl-job-executor-go
modulepackage
1.2.0
Repository: https://github.com/xxl-job/xxl-job-executor-go.git
Documentation: pkg.go.dev

# README

xxl-job-executor-go

很多公司java与go开发共存,java中有xxl-job做为任务调度引擎,为此也出现了go执行器(客户端),使用起来比较简单:

支持

1.执行器注册
2.耗时任务取消
3.任务注册,像写http.Handler一样方便
4.任务panic处理
5.阻塞策略处理
6.任务完成支持返回执行备注
7.任务超时取消 (单位:秒,0为不限制)
8.失败重试次数(在参数param中,目前由任务自行处理)
9.可自定义日志
10.自定义日志查看handler
11.支持外部路由(可与gin集成)
12.支持自定义中间件

Example

package main

import (
	"context"
	"fmt"
	xxl "github.com/xxl-job/xxl-job-executor-go"
	"github.com/xxl-job/xxl-job-executor-go/example/task"
	"log"
)

func main() {
	exec := xxl.NewExecutor(
		xxl.ServerAddr("http://127.0.0.1/xxl-job-admin"),
		xxl.AccessToken(""),            //请求令牌(默认为空)
		xxl.ExecutorIp("127.0.0.1"),    //可自动获取
		xxl.ExecutorPort("9999"),       //默认9999(非必填)
		xxl.RegistryKey("golang-jobs"), //执行器名称
		xxl.SetLogger(&logger{}),       //自定义日志
	)
	exec.Init()
	exec.Use(customMiddleware)
	//设置日志查看handler
	exec.LogHandler(customLogHandle)
	//注册任务handler
	exec.RegTask("task.test", task.Test)
	exec.RegTask("task.test2", task.Test2)
	exec.RegTask("task.panic", task.Panic)
	log.Fatal(exec.Run())
}

// 自定义日志处理器
func customLogHandle(req *xxl.LogReq) *xxl.LogRes {
	return &xxl.LogRes{Code: xxl.SuccessCode, Msg: "", Content: xxl.LogResContent{
		FromLineNum: req.FromLineNum,
		ToLineNum:   2,
		LogContent:  "这个是自定义日志handler",
		IsEnd:       true,
	}}
}

// xxl.Logger接口实现
type logger struct{}

func (l *logger) Info(format string, a ...interface{}) {
	fmt.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}

func (l *logger) Error(format string, a ...interface{}) {
	log.Println(fmt.Sprintf("自定义日志 - "+format, a...))
}

// 自定义中间件
func customMiddleware(tf xxl.TaskFunc) xxl.TaskFunc {
	return func(cxt context.Context, param *xxl.RunReq) string {
		log.Println("I am a middleware start")
		res := tf(cxt, param)
		log.Println("I am a middleware end")
		return res
	}
}

示例项目

github.com/xxl-job/xxl-job-executor-go/example/

与gin框架集成

https://github.com/gin-middleware/xxl-job-executor

xxl-job-admin配置

添加执行器

执行器管理->新增执行器,执行器列表如下:

AppName		名称		注册方式	OnLine 		机器地址 		操作
golang-jobs	golang执行器	自动注册 		查看 ( 1 )   

查看->注册节点

http://127.0.0.1:9999

添加任务

任务管理->新增(注意,使用BEAN模式,JobHandler与RegTask名称一致)

1	测试panic	BEAN:task.panic	* 0 * * * ?	admin	STOP	
2	测试耗时任务	BEAN:task.test2	* * * * * ?	admin	STOP	
3	测试golang	BEAN:task.test		* * * * * ?	admin	STOP

# Packages

No description provided by the author

# Functions

AccessToken 请求令牌.
ExecutorIp 设置执行器IP.
ExecutorPort 设置执行器端口.
Int64ToStr int64 to str.
NewExecutor 创建执行器.
RegistryKey 设置执行器标识.
ServerAddr 设置调度中心地址.
SetLogger 设置日志处理器.

# Constants

响应码.
响应码.

# Variables

No description provided by the author
No description provided by the author

# Structs

ExecuteResult 任务执行结果 200 表示任务执行正常,500表示失败.
LogReq 日志请求.
LogRes 日志响应.
LogResContent 日志响应内容.
No description provided by the author
Registry 注册参数.
RunReq 触发任务请求参数.
Task 任务.

# Interfaces

Executor 执行器.
Logger 系统日志.

# Type aliases

LogFunc 应用日志.
No description provided by the author
Middleware 中间件构造函数.
No description provided by the author
TaskFunc 任务执行函数.