forked from xxl-job/xxl-job-executor-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
optinos.go
121 lines (101 loc) · 2.65 KB
/
optinos.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package xxl
import (
"fmt"
"github.com/go-basic/ipv4"
"time"
)
type Options struct {
ServerAddr string `json:"server_addr"` //调度中心地址
AccessToken string `json:"access_token"` //请求令牌
Timeout time.Duration `json:"timeout"` //接口超时时间
ExecutorIp string `json:"executor_ip"` //本地(执行器)IP(可自行获取)
ExecutorPort string `json:"executor_port"` //本地(执行器)端口
RegistryKey string `json:"registry_key"` //执行器名称
LogDir string `json:"log_dir"` //日志目录
Storage Storager // 任务存储
HandlerMap map[string]TaskFunc // 任务函数
l Logger // 日志处理
ConcurrentExecute bool // 是否并发执行
}
// GetRunningTaskId 生成运行任务ID
func (o *Options) GetRunningTaskId(jobId, logId int64) string {
if o.ConcurrentExecute {
return fmt.Sprintf("%d-%d", jobId, logId)
}
return Int64ToStr(jobId)
}
func newOptions(opts ...Option) Options {
opt := Options{
ExecutorIp: ipv4.LocalIP(),
ExecutorPort: DefaultExecutorPort,
RegistryKey: DefaultRegistryKey,
HandlerMap: make(map[string]TaskFunc, 0),
Storage: NewSessionStorage(),
}
for _, o := range opts {
o(&opt)
}
if opt.l == nil {
opt.l = &logger{}
}
return opt
}
type Option func(o *Options)
var (
DefaultExecutorPort = "9999"
DefaultRegistryKey = "golang-jobs"
)
// ServerAddr 设置调度中心地址
func ServerAddr(addr string) Option {
return func(o *Options) {
o.ServerAddr = addr
}
}
// AccessToken 请求令牌
func AccessToken(token string) Option {
return func(o *Options) {
o.AccessToken = token
}
}
// ExecutorIp 设置执行器IP
func ExecutorIp(ip string) Option {
return func(o *Options) {
o.ExecutorIp = ip
}
}
// ExecutorPort 设置执行器端口
func ExecutorPort(port string) Option {
return func(o *Options) {
o.ExecutorPort = port
}
}
// RegistryKey 设置执行器标识
func RegistryKey(registryKey string) Option {
return func(o *Options) {
o.RegistryKey = registryKey
}
}
// SetLogger 设置日志处理器
func SetLogger(l Logger) Option {
return func(o *Options) {
o.l = l
}
}
// SetHandlerMap 设置job处理器
func SetHandlerMap(m map[string]TaskFunc) Option {
return func(o *Options) {
o.HandlerMap = m
}
}
// SetStorage 设置job处理器
func SetStorage(storage Storager) Option {
return func(o *Options) {
o.Storage = storage
}
}
// SetConcurrentExecute 设置是否并发执行
func SetConcurrentExecute(concurrentExecute bool) Option {
return func(o *Options) {
o.ConcurrentExecute = concurrentExecute
}
}