/
action_sum.go
63 lines (51 loc) · 1.1 KB
/
action_sum.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
package teacluster
import (
"github.com/TeaWeb/code/teaconfigs"
"github.com/iwind/TeaGo/Tea"
"github.com/iwind/TeaGo/files"
"github.com/iwind/TeaGo/maps"
"github.com/iwind/TeaGo/types"
"strings"
)
// cluster -> master|node
type SumAction struct {
Action
}
func (this *SumAction) Name() string {
return "sum"
}
func (this *SumAction) OnSuccess(success *SuccessAction) error {
if success.Data == nil {
return nil
}
sumMap := success.Data.Get("sum")
if sumMap == nil || !types.IsMap(sumMap) {
return nil
}
sumList := []string{}
m := maps.NewMap(sumMap)
for id, sum := range m {
sumList = append(sumList, id+"|"+types.String(sum))
}
// write to local file
file := files.NewFile(Tea.ConfigFile("cluster.sum"))
file.WriteString(strings.Join(sumList, "\n"))
// push or pull
node := teaconfigs.SharedNodeConfig()
if node == nil {
return nil
}
if node.IsMaster() {
SharedManager.PushItems()
} else {
SharedManager.PullItems()
}
return nil
}
func (this *SumAction) OnFail(fail *FailAction) error {
// TODO retry later
return nil
}
func (this *SumAction) TypeId() int8 {
return 9
}