forked from shunfei/cronsun
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathnode.go
More file actions
598 lines (501 loc) · 11.2 KB
/
node.go
File metadata and controls
598 lines (501 loc) · 11.2 KB
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
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
package node
import (
"encoding/json"
"fmt"
"io/ioutil"
"os"
"path"
"strconv"
"strings"
"syscall"
"time"
client "github.com/coreos/etcd/clientv3"
"github.com/shunfei/cronsun"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
"github.com/shunfei/cronsun/node/cron"
"github.com/shunfei/cronsun/utils"
)
// Node 执行 cron 命令服务的结构体
type Node struct {
*cronsun.Client
*cronsun.Node
*cron.Cron
jobs Jobs // 和结点相关的任务
groups Groups
cmds map[string]*cronsun.Cmd
link
// 删除的 job id,用于 group 更新
delIDs map[string]bool
ttl int64
lID client.LeaseID // lease id
done chan struct{}
}
func NewNode(cfg *conf.Conf) (n *Node, err error) {
uuid, err := cfg.UUID()
if err != nil {
return
}
ip, err := utils.LocalIP()
if err != nil {
return
}
hostname, err := os.Hostname()
if err != nil {
hostname = uuid
err = nil
}
n = &Node{
Client: cronsun.DefalutClient,
Node: &cronsun.Node{
ID: uuid,
PID: strconv.Itoa(os.Getpid()),
PIDFile: strings.TrimSpace(cfg.PIDFile),
IP: ip.String(),
Hostname: hostname,
},
Cron: cron.New(),
jobs: make(Jobs, 8),
cmds: make(map[string]*cronsun.Cmd),
link: newLink(8),
delIDs: make(map[string]bool, 8),
ttl: cfg.Ttl,
done: make(chan struct{}),
}
return
}
// 注册到 /cronsun/node/xx
func (n *Node) Register() (err error) {
pid, err := n.Node.Exist()
if err != nil {
return
}
if pid != -1 {
return fmt.Errorf("node[%s] pid[%d] exist", n.Node.ID, pid)
}
return n.set()
}
func (n *Node) set() error {
resp, err := n.Client.Grant(n.ttl + 2)
if err != nil {
return err
}
if _, err = n.Node.Put(client.WithLease(resp.ID)); err != nil {
return err
}
n.lID = resp.ID
n.writePIDFile()
return nil
}
func (n *Node) writePIDFile() {
if len(n.PIDFile) == 0 {
return
}
filename := "cronnode_pid"
if !strings.HasSuffix(n.PIDFile, "/") {
filename = path.Base(n.PIDFile)
}
dir := path.Dir(n.PIDFile)
err := os.MkdirAll(dir, 0755)
if err != nil {
log.Errorf("Failed to write pid file: %s. you can change PIDFile config in base.json", err)
return
}
n.PIDFile = path.Join(dir, filename)
err = ioutil.WriteFile(n.PIDFile, []byte(n.PID), 0644)
if err != nil {
log.Errorf("Failed to write pid file: %s. you can change PIDFile config in base.json", err)
return
}
}
func (n *Node) removePIDFile() {
if len(n.PIDFile) == 0 {
return
}
if err := os.Remove(n.PIDFile); err != nil {
log.Warnf("Failed to remove pid file: %s", err)
}
}
// 断网掉线重新注册
func (n *Node) keepAlive() {
duration := time.Duration(n.ttl) * time.Second
timer := time.NewTimer(duration)
for {
select {
case <-n.done:
return
case <-timer.C:
if n.lID > 0 {
_, err := n.Client.KeepAliveOnce(n.lID)
if err == nil {
timer.Reset(duration)
continue
}
log.Warnf("%s lid[%x] keepAlive err: %s, try to reset...", n.String(), n.lID, err.Error())
n.lID = 0
}
if err := n.set(); err != nil {
log.Warnf("%s set lid err: %s, try to reset after %d seconds...", n.String(), err.Error(), n.ttl)
} else {
log.Infof("%s set lid[%x] success", n.String(), n.lID)
}
timer.Reset(duration)
}
}
}
func (n *Node) loadJobs() (err error) {
if n.groups, err = cronsun.GetGroups(""); err != nil {
return
}
jobs, err := cronsun.GetJobs()
if err != nil {
return
}
if len(jobs) == 0 {
return
}
for _, job := range jobs {
job.Init(n.ID, n.Hostname, n.IP)
n.addJob(job, false)
}
return
}
func (n *Node) addJob(job *cronsun.Job, notice bool) {
n.link.addJob(job)
if job.IsRunOn(n.ID, n.groups) {
n.jobs[job.ID] = job
}
cmds := job.Cmds(n.ID, n.groups)
if len(cmds) == 0 {
return
}
for _, cmd := range cmds {
n.addCmd(cmd, notice)
}
return
}
func (n *Node) delJob(id string) {
n.delIDs[id] = true
job, ok := n.jobs[id]
// 之前此任务没有在当前结点执行
if !ok {
return
}
delete(n.jobs, id)
n.link.delJob(job)
cmds := job.Cmds(n.ID, n.groups)
if len(cmds) == 0 {
return
}
for _, cmd := range cmds {
n.delCmd(cmd)
}
return
}
func (n *Node) modJob(job *cronsun.Job) {
oJob, ok := n.jobs[job.ID]
// 之前此任务没有在当前结点执行,直接增加任务
if !ok {
n.addJob(job, true)
return
}
n.link.delJob(oJob)
prevCmds := oJob.Cmds(n.ID, n.groups)
job.Count = oJob.Count
*oJob = *job
cmds := oJob.Cmds(n.ID, n.groups)
for id, cmd := range cmds {
n.modCmd(cmd, true)
delete(prevCmds, id)
}
for _, cmd := range prevCmds {
n.delCmd(cmd)
}
n.link.addJob(oJob)
}
func (n *Node) addCmd(cmd *cronsun.Cmd, notice bool) {
n.Cron.Schedule(cmd.JobRule.Schedule, cmd)
n.cmds[cmd.GetID()] = cmd
if notice {
log.Infof("job[%s] group[%s] rule[%s] timer[%s] has added", cmd.Job.ID, cmd.Job.Group, cmd.JobRule.ID, cmd.JobRule.Timer)
}
return
}
func (n *Node) modCmd(cmd *cronsun.Cmd, notice bool) {
c, ok := n.cmds[cmd.GetID()]
if !ok {
n.addCmd(cmd, notice)
return
}
sch := c.JobRule.Timer
*c = *cmd
// 节点执行时间改变,更新 cron
// 否则不用更新 cron
if c.JobRule.Timer != sch {
n.Cron.Schedule(c.JobRule.Schedule, c)
}
if notice {
log.Infof("job[%s] group[%s] rule[%s] timer[%s] has updated", c.Job.ID, c.Job.Group, c.JobRule.ID, c.JobRule.Timer)
}
}
func (n *Node) delCmd(cmd *cronsun.Cmd) {
delete(n.cmds, cmd.GetID())
n.Cron.DelJob(cmd)
log.Infof("job[%s] group[%s] rule[%s] timer[%s] has deleted", cmd.Job.ID, cmd.Job.Group, cmd.JobRule.ID, cmd.JobRule.Timer)
}
func (n *Node) addGroup(g *cronsun.Group) {
n.groups[g.ID] = g
}
func (n *Node) delGroup(id string) {
// delete job first
defer n.link.delGroup(id)
defer delete(n.groups, id)
jobLinks := n.link[id]
if len(jobLinks) == 0 {
return
}
for jID := range jobLinks {
job, ok := n.jobs[jID]
// 之前此任务没有在当前结点执行
if !ok {
continue
}
cmds := job.Cmds(n.ID, n.groups)
if len(cmds) == 0 {
continue
}
for _, cmd := range cmds {
n.delCmd(cmd)
}
}
return
}
func (n *Node) modGroup(g *cronsun.Group) {
oGroup, ok := n.groups[g.ID]
if !ok {
n.addGroup(g)
return
}
// 都包含/都不包含当前节点,对当前节点任务无影响
if (oGroup.Included(n.ID) && g.Included(n.ID)) || (!oGroup.Included(n.ID) && !g.Included(n.ID)) {
*oGroup = *g
return
}
// 增加当前节点
if !oGroup.Included(n.ID) && g.Included(n.ID) {
n.groupAddNode(g)
return
}
// 移除当前节点
n.groupRmNode(g, oGroup)
return
}
func (n *Node) groupAddNode(g *cronsun.Group) {
n.groups[g.ID] = g
jls := n.link[g.ID]
if len(jls) == 0 {
return
}
var err error
for jid, jl := range jls {
job, ok := n.jobs[jid]
if !ok {
// job 已删除
if n.delIDs[jid] {
n.link.delGroupJob(g.ID, jid)
continue
}
if job, err = cronsun.GetJob(jl.gname, jid); err != nil {
log.Warnf("get job[%s][%s] err: %s", jl.gname, jid, err.Error())
n.link.delGroupJob(g.ID, jid)
continue
}
if err = job.Valid(); err != nil {
log.Warnf("invalid job[%s][%s]: %s", jl.gname, jid, err.Error())
n.link.delGroupJob(g.ID, jid)
continue
}
job.Init(n.ID, n.Hostname, n.IP)
n.jobs[jid] = job
}
cmds := job.Cmds(n.ID, n.groups)
for _, cmd := range cmds {
n.addCmd(cmd, true)
}
}
return
}
func (n *Node) groupRmNode(g, og *cronsun.Group) {
jls := n.link[g.ID]
if len(jls) == 0 {
n.groups[g.ID] = g
return
}
for jid, _ := range jls {
job, ok := n.jobs[jid]
// 之前此任务没有在当前结点执行
if !ok {
n.link.delGroupJob(g.ID, jid)
continue
}
n.groups[og.ID] = og
prevCmds := job.Cmds(n.ID, n.groups)
n.groups[g.ID] = g
cmds := job.Cmds(n.ID, n.groups)
for id, cmd := range cmds {
n.addCmd(cmd, true)
delete(prevCmds, id)
}
for _, cmd := range prevCmds {
n.delCmd(cmd)
}
}
n.groups[g.ID] = g
}
func (n *Node) KillExcutingProc(process *cronsun.Process) {
pid, _ := strconv.Atoi(process.ID)
if err := syscall.Kill(-pid, syscall.SIGKILL); err != nil {
log.Warnf("process:[%d] force kill failed, error:[%s]\n", pid, err)
return
}
}
func (n *Node) watchJobs() {
rch := cronsun.WatchJobs()
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
case ev.IsCreate():
job, err := cronsun.GetJobFromKv(ev.Kv.Key, ev.Kv.Value)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
}
job.Init(n.ID, n.Hostname, n.IP)
n.addJob(job, true)
case ev.IsModify():
job, err := cronsun.GetJobFromKv(ev.Kv.Key, ev.Kv.Value)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
}
job.Init(n.ID, n.Hostname, n.IP)
n.modJob(job)
case ev.Type == client.EventTypeDelete:
n.delJob(cronsun.GetIDFromKey(string(ev.Kv.Key)))
default:
log.Warnf("unknown event type[%v] from job[%s]", ev.Type, string(ev.Kv.Key))
}
}
}
}
func (n *Node) watchExcutingProc() {
rch := cronsun.WatchProcs(n.ID)
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
case ev.IsModify():
key := string(ev.Kv.Key)
process, err := cronsun.GetProcFromKey(key)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
}
val := string(ev.Kv.Value)
pv := &cronsun.ProcessVal{}
err = json.Unmarshal([]byte(val), pv)
if err != nil {
continue
}
process.ProcessVal = *pv
if process.Killed {
n.KillExcutingProc(process)
}
}
}
}
}
func (n *Node) watchGroups() {
rch := cronsun.WatchGroups()
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
case ev.IsCreate():
g, err := cronsun.GetGroupFromKv(ev.Kv.Key, ev.Kv.Value)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
}
n.addGroup(g)
case ev.IsModify():
g, err := cronsun.GetGroupFromKv(ev.Kv.Key, ev.Kv.Value)
if err != nil {
log.Warnf("err: %s, kv: %s", err.Error(), ev.Kv.String())
continue
}
n.modGroup(g)
case ev.Type == client.EventTypeDelete:
n.delGroup(cronsun.GetIDFromKey(string(ev.Kv.Key)))
default:
log.Warnf("unknown event type[%v] from group[%s]", ev.Type, string(ev.Kv.Key))
}
}
}
}
func (n *Node) watchOnce() {
rch := cronsun.WatchOnce()
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
case ev.IsCreate(), ev.IsModify():
if len(ev.Kv.Value) != 0 && string(ev.Kv.Value) != n.ID {
continue
}
job, ok := n.jobs[cronsun.GetIDFromKey(string(ev.Kv.Key))]
if !ok || !job.IsRunOn(n.ID, n.groups) {
continue
}
go job.RunWithRecovery()
}
}
}
}
func (n *Node) watchCsctl() {
rch := cronsun.WatchCsctl()
for wresp := range rch {
for _, ev := range wresp.Events {
switch {
case ev.IsCreate(), ev.IsModify():
n.executCsctlCmd(ev.Kv.Key, ev.Kv.Value)
}
}
}
}
// 启动服务
func (n *Node) Run() (err error) {
go n.keepAlive()
defer func() {
if err != nil {
n.Stop(nil)
}
}()
if err = n.loadJobs(); err != nil {
return
}
n.Cron.Start()
go n.watchJobs()
go n.watchExcutingProc()
go n.watchGroups()
go n.watchOnce()
go n.watchCsctl()
n.Node.On()
return
}
// 停止服务
func (n *Node) Stop(i interface{}) {
n.Node.Down()
close(n.done)
n.Node.Del()
n.Client.Close()
n.Cron.Stop()
n.removePIDFile()
}