-
Notifications
You must be signed in to change notification settings - Fork 456
/
csctl.go
73 lines (60 loc) · 1.37 KB
/
csctl.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
package cronsun
import (
"encoding/json"
"errors"
client "github.com/coreos/etcd/clientv3"
"github.com/shunfei/cronsun/conf"
)
const (
NodeCmdUnknown NodeCmd = iota
NodeCmdRmOld
NodeCmdSync
NodeCmdMax
)
var (
InvalidNodeCmdErr = errors.New("invalid node command")
NodeCmds = []string{
"unknown",
"rmold",
"sync",
}
)
type NodeCmd int
func (cmd NodeCmd) String() string {
if NodeCmdMax <= cmd || cmd <= NodeCmdUnknown {
return "unknown"
}
return NodeCmds[cmd]
}
func ToNodeCmd(cmd string) (NodeCmd, error) {
for nc := NodeCmdUnknown + 1; nc < NodeCmdMax; nc++ {
if cmd == NodeCmds[nc] {
return nc, nil
}
}
return NodeCmdUnknown, InvalidNodeCmdErr
}
type CsctlCmd struct {
// the command send to node
Cmd NodeCmd
// the node ids that needs to execute the command, empty means all node
Include []string
// the node ids that doesn't need to execute the command, empty means none
Exclude []string
}
// 执行 csctl 发送的命令
// 注册到 /cronsun/csctl/<cmd>
func PutCsctl(cmd *CsctlCmd) error {
if NodeCmdMax <= cmd.Cmd || cmd.Cmd <= NodeCmdUnknown {
return InvalidNodeCmdErr
}
params, err := json.Marshal(cmd)
if err != nil {
return err
}
_, err = DefalutClient.Put(conf.Config.Csctl+NodeCmds[cmd.Cmd], string(params))
return err
}
func WatchCsctl() client.WatchChan {
return DefalutClient.Watch(conf.Config.Csctl, client.WithPrefix())
}