コード例 #1
0
ファイル: main.go プロジェクト: lucmichalski/nsqd-discovery
func main() {
	flag.Parse()

	if *cfgAddr == "" {
		fmt.Println("required flag not provided: --config-http-address")
		os.Exit(1)
	}

	if *dnsAddr == "" {
		fmt.Println("required flag not provided: --lookupd-dns-address")
		os.Exit(1)
	}

	log.SetHandler(text.New(os.Stderr))
	ctx := log.WithFields(log.Fields{
		"cfgAddr": *cfgAddr,
		"dnsAddr": *dnsAddr,
		"ldPort":  *ldPort,
	})

	ips, err := dnscfg.Get(dnsAddr, ldPort)
	if err != nil {
		ctx.WithError(err).Error("dns lookup")
		os.Exit(1)
	}

	if len(ips) == 0 {
		ctx.Error("no ip addresses found")
		os.Exit(1)
	}

	cfgURL := "http://" + *cfgAddr + "/config/nsqlookupd_tcp_addresses"
	err = httpcfg.Set(cfgURL, ips)
	if err != nil {
		ctx.WithError(err).Error("setting config")
	} else {
		ctx.WithField("ips", ips).Info("setting config")
	}

	go configLoop(ctx, cfgURL)

	http.ListenAndServe(":6060", nil)
}
コード例 #2
0
ファイル: main.go プロジェクト: lucmichalski/nsqd-discovery
// continue looking at dns entry for changes in config
func configLoop(ctx log.Interface, cfgURL string) {
	ticker := time.Tick(15 * time.Second)

	for {
		select {
		case <-ticker:
			newIPs, err := dnscfg.Get(dnsAddr, ldPort)
			if err != nil {
				ctx.WithError(err).Error("dns lookup")
				continue
			}

			if len(newIPs) == 0 {
				ctx.Error("no ip addresses found")
				continue
			}

			oldIPs, err := httpcfg.Get(cfgURL)
			if err != nil {
				ctx.WithError(err).Error("getting config")
				continue
			}

			if eq(newIPs, oldIPs) {
				ctx.Info("config up to date")
				continue
			}

			err = httpcfg.Set(cfgURL, newIPs)
			if err != nil {
				ctx.WithError(err).Error("setting config")
				continue
			}

			ctx.WithField("ips", newIPs).Info("setting config")
		}
	}
}