-
Notifications
You must be signed in to change notification settings - Fork 456
/
group.go
118 lines (96 loc) · 2.35 KB
/
group.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
package cronsun
import (
"encoding/json"
"fmt"
"strings"
client "github.com/coreos/etcd/clientv3"
"github.com/shunfei/cronsun/conf"
"github.com/shunfei/cronsun/log"
)
// 结点类型分组
// 注册到 /cronsun/group/<id>
type Group struct {
ID string `json:"id"`
Name string `json:"name"`
NodeIDs []string `json:"nids"`
}
func GetGroupById(gid string) (g *Group, err error) {
if len(gid) == 0 {
return
}
resp, err := DefalutClient.Get(conf.Config.Group + gid)
if err != nil || resp.Count == 0 {
return
}
err = json.Unmarshal(resp.Kvs[0].Value, &g)
return
}
// GetGroups 获取包含 nid 的 group
// 如果 nid 为空,则获取所有的 group
func GetGroups(nid string) (groups map[string]*Group, err error) {
resp, err := DefalutClient.Get(conf.Config.Group, client.WithPrefix())
if err != nil {
return
}
count := len(resp.Kvs)
groups = make(map[string]*Group, count)
if count == 0 {
return
}
for _, g := range resp.Kvs {
group := new(Group)
if e := json.Unmarshal(g.Value, group); e != nil {
log.Warnf("group[%s] umarshal err: %s", string(g.Key), e.Error())
continue
}
if len(nid) == 0 || group.Included(nid) {
groups[group.ID] = group
}
}
return
}
func WatchGroups() client.WatchChan {
return DefalutClient.Watch(conf.Config.Group, client.WithPrefix(), client.WithPrevKV())
}
func GetGroupFromKv(key, value []byte) (g *Group, err error) {
g = new(Group)
if err = json.Unmarshal(value, g); err != nil {
err = fmt.Errorf("group[%s] umarshal err: %s", string(key), err.Error())
}
return
}
func DeleteGroupById(id string) (*client.DeleteResponse, error) {
return DefalutClient.Delete(GroupKey(id))
}
func GroupKey(id string) string {
return conf.Config.Group + id
}
func (g *Group) Key() string {
return GroupKey(g.ID)
}
func (g *Group) Put(modRev int64) (*client.PutResponse, error) {
b, err := json.Marshal(g)
if err != nil {
return nil, err
}
return DefalutClient.PutWithModRev(g.Key(), string(b), modRev)
}
func (g *Group) Check() error {
g.ID = strings.TrimSpace(g.ID)
if !IsValidAsKeyPath(g.ID) {
return ErrIllegalNodeGroupId
}
g.Name = strings.TrimSpace(g.Name)
if len(g.Name) == 0 {
return ErrEmptyNodeGroupName
}
return nil
}
func (g *Group) Included(nid string) bool {
for i, count := 0, len(g.NodeIDs); i < count; i++ {
if nid == g.NodeIDs[i] {
return true
}
}
return false
}