-
Notifications
You must be signed in to change notification settings - Fork 0
/
vars.go
189 lines (169 loc) · 4.54 KB
/
vars.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
package vars
import (
"context"
"github.com/fsnotify/fsnotify"
log "github.com/sirupsen/logrus"
datadir "github.com/superisaac/jointrpc/datadir"
dispatch "github.com/superisaac/jointrpc/dispatch"
misc "github.com/superisaac/jointrpc/misc"
rpcrouter "github.com/superisaac/jointrpc/rpcrouter"
jsonrpc "github.com/superisaac/jsonrpc"
yaml "gopkg.in/yaml.v2"
"io/ioutil"
"os"
//"time"
)
type VarsService struct {
disp *dispatch.Dispatcher
chResult chan dispatch.ResultT
//vars map[string](map[string](interface{}))
namedVars map[string](map[string]interface{})
//router *rpcrouter.Router
conn *rpcrouter.ConnT
}
func NewVarsService() *VarsService {
srv := new(VarsService)
return srv
}
func (self VarsService) Name() string {
return "vars"
}
func (self VarsService) CanRun(rootCtx context.Context) bool {
varsPath := datadir.Datapath("vars.yml")
if _, err := os.Stat(varsPath); os.IsNotExist(err) {
// the vars.yml doesnot exist
return false
}
return true
}
func (self *VarsService) BroadcastVars(factory *rpcrouter.RouterFactory) error {
for namespace, vars := range self.namedVars {
router := factory.GetOrNil(namespace)
if router == nil {
continue
}
notify := jsonrpc.NewNotifyMessage("vars.change", []interface{}{vars})
notify.SetTraceId(misc.NewUuid())
notify.Log().Infof("broadcast vars.change")
_, err := router.CallOrNotify(
notify, namespace, rpcrouter.WithBroadcast(true))
if err != nil {
return err
}
}
return nil
}
func (self *VarsService) ReadVars(varsPath string) error {
log.Infof("read vars from %s", varsPath)
data, err := ioutil.ReadFile(varsPath)
if err != nil {
return err
}
namedVars := make(map[string](map[string]interface{}))
err = yaml.Unmarshal(data, namedVars)
if err != nil {
return err
}
self.namedVars = namedVars
return nil
}
func (self *VarsService) Start(rootCtx context.Context) error {
factory := rpcrouter.RouterFactoryFromContext(rootCtx)
commonRouter := factory.CommonRouter()
varsPath := datadir.Datapath("vars.yml")
err := self.ReadVars(varsPath)
if err != nil {
return err
}
self.disp = dispatch.NewDispatcher()
self.chResult = make(chan dispatch.ResultT, misc.DefaultChanSize())
//self.conn = commonRouter.Join()
self.conn = rpcrouter.NewConn()
commonRouter.ChJoin <- rpcrouter.CmdJoin{Conn: self.conn}
ctx, cancel := context.WithCancel(rootCtx)
defer func() {
cancel()
commonRouter.ChLeave <- rpcrouter.CmdLeave{Conn: self.conn}
//commonRouter.Leave(self.conn)
self.conn = nil
}()
self.disp.OnTyped("_vars.list", func(req *dispatch.RPCRequest) (map[string]interface{}, error) {
if vars, ok := self.namedVars[req.CmdMsg.Namespace]; ok {
return vars, nil
} else {
return make(map[string]interface{}), nil
}
})
// setup watcher
watcher, err := fsnotify.NewWatcher()
if err != nil {
return err
}
defer watcher.Close()
err = watcher.Add(varsPath)
if err != nil {
return err
}
self.declareMethods(factory)
senderCtx, cancelSender := context.WithCancel(ctx)
defer cancelSender()
go dispatch.SenderLoop(senderCtx, self, self.conn, self.chResult)
for {
select {
case <-ctx.Done():
log.Debugf("vars handlers, context done")
return nil
case event, ok := <-watcher.Events:
if !ok {
return nil
}
log.Debugf("vars watcher event %+v", event)
if event.Op&fsnotify.Write == fsnotify.Write {
log.Infof("modified file %s", event.Name)
err := self.ReadVars(varsPath)
if err != nil {
return err
}
err = self.BroadcastVars(factory)
if err != nil {
return err
}
}
case err, ok := <-watcher.Errors:
if !ok {
return nil
}
log.Warnf("vars watcher error: %+v", err)
}
}
return nil
}
func (self *VarsService) declareMethods(factory *rpcrouter.RouterFactory) {
if self.conn != nil {
minfos := self.disp.GetMethodInfos()
cmdMethods := rpcrouter.CmdMethods{
Namespace: factory.CommonRouter().Name(),
ConnId: self.conn.ConnId,
Methods: minfos,
}
factory.Get(cmdMethods.Namespace).ChMethods <- cmdMethods
}
}
func (self VarsService) SendMessage(ctx context.Context, msg jsonrpc.IMessage) error {
factory := rpcrouter.RouterFactoryFromContext(ctx)
commonRouter := factory.CommonRouter()
self.conn.MsgInput() <- rpcrouter.CmdMsg{
Msg: msg,
Namespace: commonRouter.Name(),
}
return nil
}
func (self VarsService) SendCmdMsg(ctx context.Context, cmdMsg rpcrouter.CmdMsg) error {
msg := cmdMsg.Msg
if msg.IsRequestOrNotify() {
self.disp.Feed(ctx, cmdMsg, self.chResult)
} else {
log.Warnf("builtin handler, receved none request msg %+v", msg)
}
return nil
}