README
¶
xxljob
XXL-JOB golang executor is a standalone http server which manages the connection with XXL-JOB server.
The default port is 9999. It registers itself as a node in XXL-JOB server, listens and handles the calls from XXL-JOB server.
A golang executor can run multiple jobs concurrently, but each job is run in serial mode, which is strictly following the design pattern of XXL-JOB:
XXL-JOB的不同任务之间并行调度、并行执行。 XXL-JOB的单个任务,针对多个执行器是并行运行的,针对单个执行器是串行执行的。同时支持任务终止。
See 5.4.5 并行调度
Prerequisite
go version >= 1.16
Installation
go get -u github.com/hyperjiang/xxljob
Usage
1. Start the executor
import "github.com/hyperjiang/xxljob"
const (
appName = "xxl-job-executor-sample"
accessToken = "default_token"
host = "localhost:8080/xxl-job-admin"
demoHandler = "demoJobHandler"
)
e := xxljob.NewExecutor(
xxljob.WithAppName(appName),
xxljob.WithAccessToken(accessToken),
xxljob.WithHost(host),
)
// start in goroutine
go e.Start()
2. Add job handler
Job handlers are functions that implement xxljob.JobHandler
(that is func(ctx context.Context, param xxljob.JobParam) error
).
e.AddJobHandler(demoHandler, func(ctx context.Context, param xxljob.JobParam) error {
fmt.Println(param.Params)
return nil
})
3. Stop the executor
e.Stop()
Documentation
¶
Overview ¶
Package xxljob implements a golang executor for xxl-job. It provides functions to execute jobs, get job logs, and report job execution status. An executor can run multiple jobs concurrently, but each job is run in serial mode.
Index ¶
- Constants
- func LocalIP() string
- func ReadableSize(size int64) string
- func TruncateDuration(d time.Duration) time.Duration
- type CallbackParam
- type Executor
- type IdleBeatParam
- type Job
- type JobHandler
- type JobParam
- type KillParam
- type LogParam
- type LogResult
- type Logger
- type Option
- func WithAccessToken(token string) Option
- func WithAppName(appName string) Option
- func WithCallbackBufferSize(size int) Option
- func WithCallbackInterval(interval string) Option
- func WithClientTimeout(timeout time.Duration) Option
- func WithHost(host string) Option
- func WithIdleTimeout(timeout time.Duration) Option
- func WithInterruptSignals(signals []os.Signal) Option
- func WithLogger(logger Logger) Option
- func WithPort(port int) Option
- func WithReadTimeout(timeout time.Duration) Option
- func WithRegisterInterval(interval string) Option
- func WithSizeLimit(sizeLimit int64) Option
- func WithWaitTimeout(timeout time.Duration) Option
- func WithWriteTimeout(timeout time.Duration) Option
- type Options
- type RegistryParam
- type Response
- type RunParam
Constants ¶
const ( // SerialExecution: the scheduling job enters the FIFO queue and runs in serial mode. (default) SerialExecution = "SERIAL_EXECUTION" // DiscardLater: if there are running jobs in the executor, // this job will be discarded and marked as failed. DiscardLater = "DISCARD_LATER" // CoverEarly: if there are running jobs in the executor, // the running jobs will be terminated and the queue will be cleared, // and then this new job will be run. CoverEarly = "COVER_EARLY" )
Variables ¶
This section is empty.
Functions ¶
func ReadableSize ¶
ReadableSize prints the size in human readable format.
Types ¶
type CallbackParam ¶
type CallbackParam struct { LogID int64 `json:"logId"` LogDateTime int64 `json:"logDateTim"` HandleCode int `json:"handleCode"` HandleMsg string `json:"handleMsg"` }
CallbackParam is to report job execution result.
type Executor ¶
type Executor struct { Options // contains filtered or unexported fields }
Executor is responsible for executing jobs.
func (*Executor) AddJobHandler ¶
func (e *Executor) AddJobHandler(name string, h JobHandler)
AddJobHandler registers a job handler by name.
func (*Executor) GetJobHandler ¶
func (e *Executor) GetJobHandler(name string) JobHandler
GetJobHandler retrieves the job handler for a given name.
func (*Executor) RemoveJobHandler ¶
RemoveJobHandler removes a job handler by name.
func (*Executor) TriggerJob ¶
TriggerJob triggers a job. It will return error if handler does not exist or log id is duplicate.
type IdleBeatParam ¶
type IdleBeatParam struct {
JobID int `json:"jobId"`
}
IdleBeatParam is for idle checking.
type Job ¶
type Job struct { ID int LogID int64 Name string Handle JobHandler Param JobParam Timeout int // timeout in seconds StartTime time.Time EndTime time.Time // contains filtered or unexported fields }
Job represents a scheduled job.
type JobHandler ¶
JobHandler is the handler function for executing job.
type KillParam ¶
type KillParam struct {
JobID int `json:"jobId"`
}
KillParam is used to terminate a running job.
type LogParam ¶
type LogParam struct { LogId int64 `json:"logId"` LogDateTime int64 `json:"logDateTim"` FromLineNum int `json:"fromLineNum"` }
LogParam is used to get job logs.
type LogResult ¶
type LogResult struct { FromLineNum int `json:"fromLineNum"` ToLineNum int `json:"toLineNum"` LogContent string `json:"logContent"` IsEnd bool `json:"isEnd"` }
LogResult is the response format for log content.
type Option ¶
type Option func(*Options)
Option is for setting options.
func WithCallbackBufferSize ¶
WithCallbackBufferSize sets callback buffer size.
func WithCallbackInterval ¶
WithCallbackInterval sets callback interval.
func WithClientTimeout ¶
WithClientTimeout sets client timeout.
func WithIdleTimeout ¶
WithIdleTimeout sets idle timeout.
func WithInterruptSignals ¶
WithInterruptSignals sets interrupt signals.
func WithReadTimeout ¶
WithReadTimeout sets read timeout.
func WithRegisterInterval ¶
WithRegisterInterval sets register interval.
func WithWaitTimeout ¶
WithWaitTimeout sets wait timeout for graceful shutdown.
func WithWriteTimeout ¶
WithWriteTimeout sets write timeout.
type Options ¶
type Options struct { // client settings AccessToken string AppName string CallbackBufferSize int CallbackInterval string ClientTimeout time.Duration Host string Logger Logger RegisterInterval string SizeLimit int64 // we will not log the response if its size exceeds the size limit // http server settings Port int IdleTimeout time.Duration ReadTimeout time.Duration WriteTimeout time.Duration WaitTimeout time.Duration // contains filtered or unexported fields }
Options are executor options.
type RegistryParam ¶
type RegistryParam struct { RegistryGroup string `json:"registryGroup"` RegistryKey string `json:"registryKey"` RegistryValue string `json:"registryValue"` }
RegistryParam is to register or deregister an executor.
type Response ¶
type Response struct { Code int `json:"code"` // 200 means success, other means failed Msg string `json:"msg"` // error message Content interface{} `json:"content,omitempty"` }
Response is the response format.
func NewErrorResponse ¶
NewErrorResponse returns an error response.
func NewSuccResponse ¶
func NewSuccResponse() *Response
NewSuccResponse returns a default success response.
type RunParam ¶
type RunParam struct { JobID int `json:"jobId"` ExecutorHandler string `json:"executorHandler"` ExecutorParams string `json:"executorParams"` ExecutorBlockStrategy string `json:"executorBlockStrategy"` ExecutorTimeout int `json:"executorTimeout"` // job execution timeout in seconds LogID int64 `json:"logId"` LogDateTime int64 `json:"logDateTime"` // timestamp in milliseconds GlueType string `json:"glueType"` // unsupported in go executor GlueSource string `json:"glueSource"` // unsupported in go executor GlueUpdatetime int64 `json:"glueUpdatetime"` // unsupported in go executor BroadcastIndex int `json:"broadcastIndex"` BroadcastTotal int `json:"broadcastTotal"` }
RunParam is used to trigger a job.