Esempio n. 1
0
func Before(c *cli.Context) error {
	var (
		heartbeat time.Duration
		ttl       time.Duration
	)

	if adver := c.String("advertise"); adver == "" {
		return ErrRequireAdvertise
	} else {
		disc.Advertise = adver
	}

	if hbStr := c.String("heartbeat"); hbStr == "" {
		heartbeat = disc.DefaultHeartbeat
	} else {
		if hb, err := time.ParseDuration(hbStr); err != nil {
			log.Warning(err)
			heartbeat = disc.DefaultHeartbeat
		} else {
			heartbeat = hb
		}
	}

	if ttlStr := c.String("ttl"); ttlStr == "" {
		ttl = disc.DefaultTTL
	} else {
		if t, err := time.ParseDuration(ttlStr); err != nil {
			log.Warning(err)
			ttl = disc.DefaultTTL
		} else {
			ttl = t
		}
	}

	if disc.Discovery == "" {
		if pos := c.Args(); len(pos) != 1 {
			return ErrRequireDiscovery
		} else {
			disc.Discovery = pos[0]
		}
	}

	// register monitor instance
	disc.Register(heartbeat, ttl)

	log.WithFields(log.Fields{"advertise": disc.Advertise, "discovery": disc.Discovery, "heartbeat": heartbeat, "ttl": ttl}).Info("begin advertise")
	return nil
}
Esempio n. 2
0
func Follow(w http.ResponseWriter, r *http.Request) {
	if err := common("PUT", r); err != nil {
		http.Error(w, err.Error(), 400)
		return
	}

	var (
		discovery = r.Form.Get("discovery")
		cluster   = r.Form.Get("cluster")

		proxycfg = r.Form.Get("key")

		heartbeat time.Duration
		ttl       time.Duration
	)

	if proxycfg == "" {
		http.Error(w, "Bad Request", 400)
		return
	} else {
		proxy.ProxyConfigKey = proxycfg
	}

	if hbStr := r.Form.Get("hb"); hbStr == "" {
		heartbeat = disc.DefaultHeartbeat
	} else {
		if hb, err := time.ParseDuration(hbStr); err != nil {
			heartbeat = disc.DefaultHeartbeat
		} else {
			heartbeat = hb
		}
	}

	if ttlStr := r.Form.Get("ttl"); ttlStr == "" {
		ttl = disc.DefaultTTL
	} else {
		if t, err := time.ParseDuration(ttlStr); err != nil {
			ttl = disc.DefaultTTL
		} else {
			ttl = t
		}
	}

	if proxy.ConfigReset != nil {
		proxy.ConfigReset() // abort config
	}
	if disc.Cancel != nil {
		disc.Cancel() // abort previous session
	}

	// resume advertising node, if we were advertising
	if discovery != "null" && discovery != "" {
		disc.Discovery = discovery
	}
	if cluster != "null" && cluster != "" {
		disc.RegisterPath = path.Join(cluster, proxy.DiscoveryPath)
	}
	if disc.Discovery != "" {
		disc.Register(heartbeat, ttl)
		proxy.Follow()
	}

	w.Write([]byte("done"))
}