-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathagent.go
207 lines (170 loc) · 4.29 KB
/
agent.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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
package cron
import (
"context"
"errors"
"net/http"
"os"
"os/signal"
"syscall"
"github.com/go-redis/redis/v8"
"github.com/hashicorp/memberlist"
)
var (
ErrJobNotSupport = errors.New("unsupported job")
ErrJobNameEmpty = errors.New("job node can not be empty")
)
type Agent struct {
cron *Cron
executor *Executor
server http.Server
stop chan os.Signal
}
func NewAgent(conf *Conf) *Agent {
cli := redis.NewClient(&conf.Base.RedisOptions)
// gossip
gossipConf := memberlist.DefaultLANConfig()
if conf.Gossip.Network == "Local" {
gossipConf = memberlist.DefaultLocalConfig()
}
if conf.Gossip.Network == "WAN" {
gossipConf = memberlist.DefaultWANConfig()
}
gossipConf.BindAddr = conf.Gossip.BindAddr
gossipConf.BindPort = conf.Gossip.BindPort
gossipConf.Name = conf.Gossip.NodeName
timeline := NewRedisTimeline(cli, conf.Custom.KeyTimeline)
entries := NewGossipEntries(cli, gossipConf)
executor := NewExecutor(cli, entries.list.LocalNode().Name)
cron := NewCron(entries, timeline, executor.Receiver())
// custom
entries.WithKeyPrefix(conf.Custom.KeyEntry)
executor.WithKeyPrefix(conf.Custom.KeyExecutor)
executor.WithMaxHistoryNum(conf.Custom.MaxHistoryNum)
return &Agent{
cron: cron,
executor: executor,
server: http.Server{Addr: conf.Base.HttpAddr},
stop: make(chan os.Signal),
}
}
// Join must call before Run()
func (a *Agent) Join(existing []string) { a.cron.entries.Join(existing) }
func (a *Agent) Run() {
go a.executor.consume()
go a.cron.run()
go a.serveHTTP()
signal.Notify(a.stop, syscall.SIGINT)
s := <-a.stop
Logger.Infof("receive a signal %s, begin to shutdown...", s.String())
a.close()
}
func (a *Agent) Register(jobs ...Job) error {
for _, job := range jobs {
if err := a.register(job); err != nil {
return err
}
}
return nil
}
func (a *Agent) register(job Job) error {
if job.Name() == "" {
return ErrJobNameEmpty
}
a.executor.Register(job)
return nil
}
func (a *Agent) serveHTTP() {
a.server.Handler = a.Router()
Logger.Info("start admin http server: ", a.server.Addr)
Logger.Info(a.server.ListenAndServe())
}
func (a *Agent) close() {
a.server.Close()
a.cron.close()
a.executor.close()
a.cron.timeline.Close()
a.cron.entries.Close()
Logger.Info("agent shutdown gracefully")
}
func (a *Agent) Add(spec, jobName string) error {
if err := a.validate(jobName); err != nil {
return err
}
return a.cron.Add(spec, jobName)
}
func (a *Agent) Active(jobName string) error {
if err := a.validate(jobName); err != nil {
return err
}
return a.cron.Activate(jobName)
}
func (a *Agent) Pause(jobName string) error {
if err := a.validate(jobName); err != nil {
return err
}
return a.cron.Pause(jobName)
}
func (a *Agent) Remove(jobName string) error {
if err := a.validate(jobName); err != nil {
return err
}
return a.cron.Remove(jobName)
}
func (a *Agent) ExecuteOnce(jobName string) error {
if err := a.validate(jobName); err != nil {
return err
}
go a.executor.executeTask(context.Background(), jobName)
Logger.Info("execute once:", jobName)
return nil
}
func (a *Agent) Schedule() ([]entryRecord, error) {
events, err := a.cron.Events()
if err != nil {
return nil, err
}
var results = make([]entryRecord, len(events))
for i, event := range events {
results[i] = entryRecord{
Name: event.Name,
Next: event.Time.Unix() * 1000,
Displayed: event.Displayed,
}
if e, ok := a.cron.entries.Get(event.Name); ok {
results[i].Spec = e.Spec
}
}
return results, nil
}
func (a *Agent) Running() ([]Execution, error) {
return a.executor.Running()
}
func (a *Agent) History(jobName string, offset, size int64) ([]Execution, int64, error) {
total := a.executor.maxHistoryNum
if jobName == "" {
return nil, total, ErrJobNameEmpty
}
executions, err := a.executor.History(jobName, offset, size)
return executions, total, err
}
func (a *Agent) Jobs() []string {
return a.executor.Jobs()
}
func (a *Agent) Members() []*memberlist.Node {
return a.cron.entries.list.Members()
}
func (a *Agent) validate(jobName string) error {
if jobName == "" {
return ErrJobNameEmpty
}
if !a.executor.Contain(jobName) {
return ErrJobNotSupport
}
return nil
}
type entryRecord struct {
Name string `json:"name"`
Spec string `json:"spec"`
Next int64 `json:"next"`
Displayed bool `json:"displayed"`
}