func watchMaintain() { const PATH = "/maintain" ch := make(chan []string, 10) go etclib.WatchChildren(PATH, ch) for { select { case <-ch: kingdoms, err := etclib.Children(PATH) if err == nil { log.Trace("maintain kingdoms updated: %+v", kingdoms) dumpMaintainConfigPhp(kingdoms) } else { log.Error("maintain kingdom: %s", err) } } } log.Warn("maintain watcher died") }
func (this *Start) main() { ctx.LoadFromHome() // TODO zk session timeout err := etclib.Dial(strings.Split(ctx.ZoneZkAddrs(this.zone), ",")) swalllow(err) root := zkr.Root(this.zone) ch := make(chan []string, 10) go etclib.WatchChildren(root, ch) var servers = BackendServers{ CpuNum: ctx.NumCPU(), HaproxyRoot: this.root, ForwardFor: this.forwardFor, PubPort: this.pubPort, SubPort: this.subPort, ManPort: this.manPort, } var lastInstances []string for { select { case <-this.quitCh: time.Sleep(time.Second) // FIXME just wait log flush return case <-ch: kwInstances, err := etclib.Children(root) if err != nil { log.Error("%s: %v", root, err) continue } log.Info("kateway ids: %+v -> %+v", lastInstances, kwInstances) lastInstances = kwInstances servers.reset() for _, kwId := range kwInstances { kwNode := fmt.Sprintf("%s/%s", root, kwId) data, err := etclib.Get(kwNode) if err != nil { log.Error("%s: %v", kwNode, err) continue } info := make(map[string]string) if err = json.Unmarshal([]byte(data), &info); err != nil { log.Error("%s: %v", data, err) continue } // pub if info["pub"] != "" { be := Backend{ Name: "p" + info["id"], Addr: info["pub"], Cpu: info["cpu"], } servers.Pub = append(servers.Pub, be) } // sub if info["sub"] != "" { be := Backend{ Name: "s" + info["id"], Addr: info["sub"], Cpu: info["cpu"], } servers.Sub = append(servers.Sub, be) } // man if info["man"] != "" { be := Backend{ Name: "m" + info["id"], Addr: info["man"], Cpu: info["cpu"], } servers.Man = append(servers.Man, be) } } if err = this.createConfigFile(servers); err != nil { log.Error(err) continue } if err = this.reloadHAproxy(); err != nil { log.Error("reloading haproxy: %v", err) panic(err) } } } }