(sigChan chan int, errChan chan error)
| 251 | } |
| 252 | |
| 253 | func 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 | |
| 304 | func CreateInstanceManagerModule() *InstanceModule { |
| 305 | InstanceParallelLimitChan = make(chan uint8, config.GlobalConfig.App.ParallelNum) |
nothing calls this directly
no test coverage detected