forked from snail8501/xxl-job-executor
-
Notifications
You must be signed in to change notification settings - Fork 0
/
xxl_client.go
106 lines (93 loc) · 2.66 KB
/
xxl_client.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
package xxl
import (
"context"
"github.com/snail8501/xxl-job-executor/admin"
executor2 "github.com/snail8501/xxl-job-executor/executor"
"github.com/snail8501/xxl-job-executor/handler"
"github.com/snail8501/xxl-job-executor/logger"
"github.com/snail8501/xxl-job-executor/option"
)
type XxlClient struct {
executor *executor2.Executor
requestHandler *handler.RequestProcess
}
func NewXxlClient(clientOptions option.ClientOptions) *XxlClient {
clientOps := option.NewClientOptions(
option.WithServerAddrs(clientOptions.ServerAddrs...),
option.WithAccessToken(clientOptions.AccessToken),
option.WithAppName(clientOptions.AppName),
option.WithClientPort(clientOptions.ClientPort),
option.WithTimeout(clientOptions.Timeout),
option.WithBeatTime(clientOptions.BeatTime),
option.WithLogLevel(clientOptions.LogLevel),
option.WithDefaultOptions(),
)
executor := executor2.NewExecutor(
clientOps.AppName,
clientOps.ClientPort,
)
adminServer := admin.NewAdminServer(
clientOps.ServerAddrs,
clientOps.Timeout,
clientOps.BeatTime,
executor,
)
var requestHandler *handler.RequestProcess
adminServer.AccessToken = map[string]string{
"XXL-JOB-ACCESS-TOKEN": clientOps.AccessToken,
}
requestHandler = handler.NewRequestProcess(adminServer, &handler.HttpRequestHandler{})
httpServer := executor2.NewHttpServer(requestHandler.RequestProcess)
executor.SetServer(httpServer)
return &XxlClient{
requestHandler: requestHandler,
executor: executor,
}
}
func (c *XxlClient) ExitApplication() {
c.requestHandler.RemoveRegisterExecutor()
}
func GetParam(ctx context.Context, key string) (val string, has bool) {
jobMap := ctx.Value("jobParam")
if jobMap != nil {
inputParam, ok := jobMap.(map[string]map[string]interface{})["inputParam"]
if ok {
val, vok := inputParam[key]
if vok {
return val.(string), true
}
}
}
return "", false
}
func GetSharding(ctx context.Context) (shardingIdx, shardingTotal int32) {
jobMap := ctx.Value("jobParam")
if jobMap != nil {
shardingParam, ok := jobMap.(map[string]map[string]interface{})["sharding"]
if ok {
idx, vok := shardingParam["shardingIdx"]
if vok {
shardingIdx = idx.(int32)
}
total, ok := shardingParam["shardingTotal"]
if ok {
shardingTotal = total.(int32)
}
}
}
return shardingIdx, shardingTotal
}
func (c *XxlClient) Run() error {
c.requestHandler.RegisterExecutor()
logger.InitLogPath()
return c.executor.Run()
}
func (c *XxlClient) Close() error {
if err := c.executor.Close(); err != nil {
return err
}
return nil
}
func (c *XxlClient) RegisterJob(jobName string, function handler.JobHandlerFunc) {
c.requestHandler.RegisterJob(jobName, function)
}