I’m writing a daemon that will watch a key prefix, and then deploy code on changes to that prefix.
I’m looking for advice on how to do this correctly with the SDK. The only thing I’ve found after googling is this post from several years ago: https://groups.google.com/forum/#!topic/consul-tool/g0tMCaCnszg
So, do an initial GET, and use the LastIndex to issue a blocking query. And since I am a daemon that needs to watch “forever”, do this in an infinite loop.
opts := api.QueryOptions{}
//initial query
kvpair, meta, err := c.KV().Get(key, &opts)
if err != nil {
return err
}
for {
// use kvpair's value to do some deploy logic, and then issue a blocking query
_ = kvpair
// blocking query; 5 min max?
opts.WaitIndex = meta.LastIndex
kvpair, meta, err = c.KV().Get(key, &opts)
if err != nil {
return err
}
}
Is there a better way?