| 34 | } |
| 35 | |
| 36 | func watchConfigChange(sigChan chan int, errChan chan error) { |
| 37 | for { |
| 38 | ctx, cancel := context.WithCancel(context.Background()) |
| 39 | |
| 40 | watchChan := utils.EtcdClient.Watch( |
| 41 | ctx, |
| 42 | key.NodeInstanceConfigKeySelf, |
| 43 | clientv3.WithPrefix(), |
| 44 | ) |
| 45 | |
| 46 | for { |
| 47 | select { |
| 48 | case sig := <-sigChan: |
| 49 | if sig == signal.STOP_SIGNAL { |
| 50 | cancel() |
| 51 | return |
| 52 | } |
| 53 | case res := <-watchChan: |
| 54 | if len(res.Events) < 1 { |
| 55 | logrus.Error("Unexpected Instance Config Info List Length:", len(res.Events)) |
| 56 | continue |
| 57 | } |
| 58 | for _, event := range res.Events { |
| 59 | keySplit := strings.Split(string(event.Kv.Key), "/") |
| 60 | instanceID := keySplit[len(keySplit)-1] |
| 61 | configPath := path.Join(dir.TopoInfoDir, instanceID) |
| 62 | if len(event.Kv.Value) > 0 { |
| 63 | fd, err := os.Create(configPath) |
| 64 | if err != nil { |
| 65 | errMsg := fmt.Sprintf("Open File %s to Write Error: %s", configPath, err.Error()) |
| 66 | logrus.Error(errMsg) |
| 67 | continue |
| 68 | } |
| 69 | _, err = fd.Write(event.Kv.Value) |
| 70 | if err != nil { |
| 71 | errMsg := fmt.Sprintf("Write to File %s Error: %s", configPath, err.Error()) |
| 72 | logrus.Error(errMsg) |
| 73 | continue |
| 74 | } |
| 75 | } else { |
| 76 | os.Remove(configPath) |
| 77 | } |
| 78 | } |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | } |