func keepAlive(name string, id string) { var err error discoveryConf := cfg.GetAgentDiscovery() ticker := time.NewTicker(time.Duration(discoveryConf.TTL-1) * time.Second) opt := discovery.Options{ "TTL": time.Duration(discoveryConf.TTL) * time.Second, } ch_stop := ch.CreateInstanceChannel(id) isntanceKey := discoveryConf.AppRoot + "/" + name + "/" + id instanceValue := addressMap[id] for { select { case <-ticker.C: err = discovery.Set(isntanceKey, instanceValue, opt) if err != nil { log.WithFields(log.Fields{ "service": name, "instance": id, "address": instanceValue, "err": err, }).Errorln("Error keeping instance alive") } case <-ch_stop: log.Debugln("Stopping keep alive routine") return } } }
func RegisterCluster(name string, id string) { var err error err = discovery.Register(c_GRU_REMOTE+name+"/uuid", id) if err != nil { log.Errorln("Error registering cluster") } log.Debugln("Created cluster forder: ", name) opt := discovery.Options{"Dir": true} err = discovery.Set(c_GRU_REMOTE+name+"/"+c_NODES_REMOTE, "", opt) if err != nil { log.Errorln("Error creating nodes folder") } log.Debugln("Created nodes folder") err = discovery.Set(c_GRU_REMOTE+name+"/"+c_SERVICES_REMOTE, "", opt) if err != nil { log.Errorln("Error creating services folder") } log.Debugln("Created services folder") err = discovery.Set(c_GRU_REMOTE+name+"/"+c_CONFIG_REMOTE, "empty", discovery.Options{}) if err != nil { log.Errorln("Error creating config key") } log.Debugln("Created config key") err = discovery.Set(c_GRU_REMOTE+name+"/"+c_POLICY_REMOTE, "empty", discovery.Options{}) if err != nil { log.Errorln("Error creating policy key") } log.Debugln("Created policy key") err = discovery.Set(c_GRU_REMOTE+name+"/"+c_ANALYTIC_REMOTE, "", opt) if err != nil { log.Errorln("Error creating analytics folder") } log.Debugln("Created analytics folder") }
func writeData(remote string, src interface{}) error { var err error data, err := json.Marshal(src) if err != nil { return err } err = discovery.Set(remote, string(data), discovery.Options{}) if err != nil { return err } return nil }
func WriteNodeActive(remote string, active bool) { activePath := remote + "/active" var value string if active { value = "true" } else { value = "false" } err := discovery.Set(activePath, value, discovery.Options{}) if err != nil { log.WithField("err", err).Errorln("Error writing node active") } }
func updateNodeFolder(ttl int) error { opt := discovery.Options{ "TTL": time.Second * time.Duration(ttl), "Dir": true, "PrevExist": true, } remote := cfg.GetNodeConfig().Remote err := discovery.Set(remote, "", opt) cfg.WriteNodeConfig(remote, cfg.GetNode().Configuration) cfg.WriteNodeConstraints(remote, cfg.GetNode().Constraints) cfg.WriteNodeResources(remote, cfg.GetNode().Resources) cfg.WriteNodeActive(remote, cfg.GetNode().Active) if err != nil { log.WithField("err", err).Errorln("Error updating node folder") return err } return nil }
func RegisterServiceInstanceId(name string, id string) { var err error discoveryConf := cfg.GetAgentDiscovery() opt := discovery.Options{ "TTL": time.Duration(discoveryConf.TTL) * time.Second, } isntanceKey := discoveryConf.AppRoot + "/" + name + "/" + id instanceValue := addressMap[id] err = discovery.Set(isntanceKey, instanceValue, opt) if err != nil { log.WithFields(log.Fields{ "service": name, "instance": id, "address": instanceValue, "err": err, }).Errorln("Error registering service instance to discovery service") } }
func createNodeFolder() error { var err error opt := discovery.Options{ "TTL": time.Second * time.Duration(c_TTL+1), "Dir": true, } config := cfg.GetNodeConfig() config.Cluster = myCluster.Name remote := c_GRU_REMOTE + myCluster.Name + "/" + c_NODES_REMOTE + config.Name config.Remote = remote err = discovery.Set(remote, "", opt) cfg.WriteNodeConfig(remote, cfg.GetNode().Configuration) cfg.WriteNodeConstraints(remote, cfg.GetNode().Constraints) cfg.WriteNodeResources(remote, cfg.GetNode().Resources) cfg.WriteNodeActive(remote, cfg.GetNode().Active) if err != nil { log.WithField("err", err).Errorln("Error creating node folder") return err } return nil }