Example #1
0
/*
Update the list of remote hosts with the specified hostname.
*/
func (otter *Otter) UpdateHost(hostname, previous string) (string, error) {
	options := etcd.SetOptions{
		TTL: 60 * time.Second,
	}
	now := time.Now().String()
	if previous != "" {
		options.PrevValue = previous
	}
	_, err := otter.etcdKeysApi.Set(context.Background(), fmt.Sprintf("/ping/%s", hostname), now, &options)
	if err != nil {
		return now, err
	}
	return now, nil
}
Example #2
0
func Register(heartbeat time.Duration, ttl time.Duration) {
	Hearbeat, TTL = heartbeat, ttl

	// begin book keeping "THIS" montior unit
	go func() {
		var work ctx.Context

		work, Cancel = ctx.WithCancel(ctx.Background())

		client := NewDiscovery()

		var (
			iden = path.Join(RegisterPath, Advertise)
			opts = etcd.SetOptions{TTL: ttl}
			kAPI = etcd.NewKeysAPI(client)
			f    = log.Fields{"heartbeat": heartbeat, "ttl": ttl}
			t    = time.NewTicker(heartbeat)
		)
		defer t.Stop()

		// Allow for implicit bootstrap on register
		if err := upkeep(kAPI, iden, &opts); err != nil {
			log.Error("1:", err)
		} else {
			log.WithFields(f).Info("uptime")
		}

		// Tick... Toc...
		for {
			select {
			case <-t.C:
				if err := upkeep(kAPI, iden, &opts); err != nil {
					log.Error("2:", err)
					opts.PrevExist = etcd.PrevIgnore
				} else {
					log.WithFields(f).Info("uptime")
					opts.PrevExist = etcd.PrevExist
				}
			case <-work.Done():
				log.WithFields(f).Info("abort")
				return
			}
		}
	}()
}