예제 #1
0
func storeData(kv *api.KV, key string, value []byte) {
	pair, _, err := kv.Get(key, nil)
	if err != nil {
		Error.Fatal("Failed reading from Consul for key:", key, "Error:", err)
	}
	if pair == nil || string(pair.Value) != string(value) {
		var p *api.KVPair
		if pair == nil {
			p = &api.KVPair{Key: key, Value: value}
		} else {
			p = &api.KVPair{Key: key, Value: value, ModifyIndex: pair.ModifyIndex}
		}
		success, meta, err := kv.CAS(p, nil)
		if err != nil {
			Error.Fatal(err)
		}
		if !success {
			storeData(kv, key, value)
		} else {
			Trace.Print("Request took: ", meta.RequestTime.Seconds(), " seconds")
			Info.Print("Updated key '", key, "' to '", string(value), "'")
		}
	} else {
		Trace.Print("not updating key", key)
	}
}
예제 #2
0
func monitorConsulGoroutine(kv *consulapi.KV, service string, lastIndex uint64) {
	for {
		pair, qm, err := kv.Get(
			consulPrefix+service,
			&consulapi.QueryOptions{
				WaitIndex:         lastIndex,
				RequireConsistent: true,
			})
		if err != nil {
			if consulapi.IsServerError(err) {
				// Consul unavailable. Try again.
				time.Sleep(1 * time.Second)
				continue
			}
			if strings.Contains(err.Error(), "read: connection timed out") {
				// Try again.
				time.Sleep(1 * time.Second)
				continue
			}

			log.Fatalf("Error monitoring config in Consul: %v\n", err)
		}
		if pair == nil {
			log.Fatalf("Config in consul has been deleted\n")
		}

		updateConsulFlags(pair.Value)
		lastIndex = qm.LastIndex
	}
}
예제 #3
0
func keyExists(key string, kv *consul.KV) bool {
	kp, _, err := kv.Get(key, nil)
	if err != nil {
		log.Warnf("Could not get key path %s: %v", key, err)
		return false
	}

	return kp != nil
}
예제 #4
0
파일: consul.go 프로젝트: hypersleep/go-sms
func fetchConsul(kv *consul.KV, env string, key string) (value string, conf_err error) {
	pair, _, _ := kv.Get("go-sms/"+env+"/"+key, nil)
	if pair == nil {
		conf_err = errors.New("Failed to fetch \"" + key + "\" key for you environment. Leave default: " + defaults[key])
		value = defaults[key]
		return
	}

	value = string(pair.Value)
	return
}
// getSequencer gets the lockindex, session id and the key which constitutes the sequencer for the current lock.
func getSequencer(kv *api.KV, key string) (*sequencer, error) {
	kvPair, _, err := kv.Get(key, nil)
	if err != nil {
		log.Println("Can't get the sequencer")
		return nil, err
	}
	if kvPair == nil {
		return nil, nil
	}
	seq := sequencer{}
	seq.lockIndex = kvPair.LockIndex
	seq.session = kvPair.Session
	seq.key = kvPair.Key
	return &seq, nil
}
예제 #6
0
		keysPairs, _, err := kvClient.Keys(joinedKeys, "/", nil)
		if err != nil {
			log.Error("Error getting keys ", joinedKeys, err)
			return nil
		}
		keysPairs = fun.Filter(func(key string) bool {
			if key == joinedKeys {
				return false
			}
			return true
		}, keysPairs).([]string)
		return keysPairs
	},
	"Get": func(keys ...string) string {
		joinedKeys := strings.Join(keys, "")
		keyPair, _, err := kvClient.Get(joinedKeys, nil)
		if err != nil {
			log.Error("Error getting key ", joinedKeys, err)
			return ""
		}
		return string(keyPair.Value)
	},
	"Last": func(key string) string {
		splittedKey := strings.Split(key, "/")
		return splittedKey[len(splittedKey)-2]
	},
}

func NewConsulProvider() *ConsulProvider {
	consulProvider := new(ConsulProvider)
	// default values
예제 #7
0
파일: consul.go 프로젝트: nsaje/dagger
func consulGet(kv *api.KV, key string, lastIndex uint64) (interface{}, *api.QueryMeta, error) {
	return kv.Get(key, &api.QueryOptions{WaitIndex: lastIndex})
}