Example #1
0
// watchAutoConfig monitors the consul health checks and creates a new configuration
// on every change.
func watchAutoConfig(client *api.Client, tagPrefix string, config chan []string) {
	var lastIndex uint64

	for {
		q := &api.QueryOptions{RequireConsistent: true, WaitIndex: lastIndex}
		checks, meta, err := client.Health().State("passing", q)
		if err != nil {
			log.Printf("[WARN] Error fetching health state. %v", err)
			time.Sleep(time.Second)
			continue
		}

		log.Printf("[INFO] Health changed to #%d", meta.LastIndex)
		config <- servicesConfig(client, checks, tagPrefix)
		lastIndex = meta.LastIndex
	}
}