MCPcopy Create free account
hub / github.com/OpenSN-Library/OpenSN-Library / watchInstanceDaemon

Function watchInstanceDaemon

daemon/pkg/module/instance_module.go:253–302  ·  view source on GitHub ↗
(sigChan chan int, errChan chan error)

Source from the content-addressed store, hash-verified

251}
252
253func watchInstanceDaemon(sigChan chan int, errChan chan error) {
254 for {
255 ctx, cancel := context.WithCancel(context.Background())
256 watchChan := utils.EtcdClient.Watch(ctx, key.NodeInstanceListKeySelf, clientv3.WithPrefix(), clientv3.WithPrevKV())
257
258 for {
259 select {
260 case sig := <-sigChan:
261 if sig == signal.STOP_SIGNAL {
262 cancel()
263 return
264 }
265 case res := <-watchChan:
266 for _, v := range res.Events {
267 go func(v *clientv3.Event) {
268 etcdKey := ""
269 oldInstance := new(model.Instance)
270 newInstance := new(model.Instance)
271 if v.PrevKv != nil && len(v.PrevKv.Value) > 0 {
272 etcdKey = string(v.PrevKv.Key)
273 err := json.Unmarshal(v.PrevKv.Value, oldInstance)
274 if err != nil {
275 errMsg := fmt.Sprintf("Parse Instance Info From %s Error: %s", etcdKey, err.Error())
276 logrus.Error(errMsg)
277 return
278 }
279 }
280 if v.Kv != nil && len(v.Kv.Value) > 0 {
281 etcdKey = string(v.Kv.Key)
282 err := json.Unmarshal(v.Kv.Value, newInstance)
283 if err != nil {
284 errMsg := fmt.Sprintf("Parse Instance Info From %s Error: %s", etcdKey, err.Error())
285 logrus.Error(errMsg)
286 return
287 }
288 }
289 err := UpdateInstanceState(oldInstance, newInstance)
290 if err != nil {
291 errMsg := fmt.Sprintf("Update Instance %s State Error: %s", etcdKey, err.Error())
292 logrus.Error(errMsg)
293 } else {
294 logrus.Infof("Update Instance %s State Success", etcdKey)
295 }
296 }(v)
297 }
298
299 }
300 }
301 }
302}
303
304func CreateInstanceManagerModule() *InstanceModule {
305 InstanceParallelLimitChan = make(chan uint8, config.GlobalConfig.App.ParallelNum)

Callers

nothing calls this directly

Calls 1

UpdateInstanceStateFunction · 0.85

Tested by

no test coverage detected