2014-08-20 20:45:34 +00:00
|
|
|
package watch
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"log"
|
2014-08-21 20:09:13 +00:00
|
|
|
"os"
|
2014-08-20 20:45:34 +00:00
|
|
|
"reflect"
|
|
|
|
"time"
|
|
|
|
|
2015-01-06 18:40:00 +00:00
|
|
|
consulapi "github.com/hashicorp/consul/api"
|
2014-08-20 20:45:34 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// retryInterval is the base retry value
|
|
|
|
retryInterval = 5 * time.Second
|
|
|
|
|
|
|
|
// maximum back off time, this is to prevent
|
|
|
|
// exponential runaway
|
|
|
|
maxBackoffTime = 180 * time.Second
|
|
|
|
)
|
|
|
|
|
|
|
|
// Run is used to run a watch plan
|
2018-05-31 21:07:36 +00:00
|
|
|
func (p *Plan) Run(address string, conf *consulapi.Config) error {
|
2014-08-20 20:45:34 +00:00
|
|
|
// Setup the client
|
|
|
|
p.address = address
|
2018-05-31 21:07:36 +00:00
|
|
|
if conf == nil {
|
|
|
|
conf = consulapi.DefaultConfig()
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
conf.Address = address
|
|
|
|
conf.Datacenter = p.Datacenter
|
2014-08-20 23:45:37 +00:00
|
|
|
conf.Token = p.Token
|
2014-08-20 20:45:34 +00:00
|
|
|
client, err := consulapi.NewClient(conf)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("Failed to connect to agent: %v", err)
|
|
|
|
}
|
|
|
|
p.client = client
|
|
|
|
|
2014-08-21 20:09:13 +00:00
|
|
|
// Create the logger
|
|
|
|
output := p.LogOutput
|
|
|
|
if output == nil {
|
|
|
|
output = os.Stderr
|
|
|
|
}
|
|
|
|
logger := log.New(output, "", log.LstdFlags)
|
|
|
|
|
2014-08-20 20:45:34 +00:00
|
|
|
// Loop until we are canceled
|
|
|
|
failures := 0
|
2014-08-20 22:18:08 +00:00
|
|
|
OUTER:
|
2014-08-20 20:45:34 +00:00
|
|
|
for !p.shouldStop() {
|
|
|
|
// Invoke the handler
|
2017-04-21 00:46:29 +00:00
|
|
|
index, result, err := p.Watcher(p)
|
2014-08-20 20:45:34 +00:00
|
|
|
|
|
|
|
// Check if we should terminate since the function
|
|
|
|
// could have blocked for a while
|
|
|
|
if p.shouldStop() {
|
|
|
|
break
|
|
|
|
}
|
|
|
|
|
|
|
|
// Handle an error in the watch function
|
|
|
|
if err != nil {
|
|
|
|
// Perform an exponential backoff
|
|
|
|
failures++
|
2017-01-17 08:02:39 +00:00
|
|
|
p.lastIndex = 0
|
2014-08-20 20:45:34 +00:00
|
|
|
retry := retryInterval * time.Duration(failures*failures)
|
|
|
|
if retry > maxBackoffTime {
|
|
|
|
retry = maxBackoffTime
|
|
|
|
}
|
2018-03-21 15:56:14 +00:00
|
|
|
logger.Printf("[ERR] consul.watch: Watch (type: %s) errored: %v, retry in %v",
|
2014-08-21 20:09:13 +00:00
|
|
|
p.Type, err, retry)
|
2014-08-20 20:45:34 +00:00
|
|
|
select {
|
|
|
|
case <-time.After(retry):
|
2014-08-20 22:18:08 +00:00
|
|
|
continue OUTER
|
2014-08-20 20:45:34 +00:00
|
|
|
case <-p.stopCh:
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Clear the failures
|
|
|
|
failures = 0
|
|
|
|
|
|
|
|
// If the index is unchanged do nothing
|
|
|
|
if index == p.lastIndex {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update the index, look for change
|
2014-08-22 00:24:20 +00:00
|
|
|
oldIndex := p.lastIndex
|
2014-08-20 20:45:34 +00:00
|
|
|
p.lastIndex = index
|
2014-08-22 00:24:20 +00:00
|
|
|
if oldIndex != 0 && reflect.DeepEqual(p.lastResult, result) {
|
2014-08-20 20:45:34 +00:00
|
|
|
continue
|
|
|
|
}
|
2017-01-17 08:02:53 +00:00
|
|
|
if p.lastIndex < oldIndex {
|
|
|
|
p.lastIndex = 0
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
|
|
|
|
// Handle the updated result
|
|
|
|
p.lastResult = result
|
2014-08-20 22:18:08 +00:00
|
|
|
if p.Handler != nil {
|
|
|
|
p.Handler(index, result)
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Stop is used to stop running the watch plan
|
2017-04-21 00:46:29 +00:00
|
|
|
func (p *Plan) Stop() {
|
2014-08-20 20:45:34 +00:00
|
|
|
p.stopLock.Lock()
|
|
|
|
defer p.stopLock.Unlock()
|
|
|
|
if p.stop {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
p.stop = true
|
2017-06-26 20:52:03 +00:00
|
|
|
if p.cancelFunc != nil {
|
|
|
|
p.cancelFunc()
|
|
|
|
}
|
2014-08-20 20:45:34 +00:00
|
|
|
close(p.stopCh)
|
|
|
|
}
|
|
|
|
|
2017-04-21 00:46:29 +00:00
|
|
|
func (p *Plan) shouldStop() bool {
|
2014-08-20 20:45:34 +00:00
|
|
|
select {
|
|
|
|
case <-p.stopCh:
|
|
|
|
return true
|
|
|
|
default:
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
2017-06-24 19:52:41 +00:00
|
|
|
|
|
|
|
func (p *Plan) IsStopped() bool {
|
|
|
|
p.stopLock.Lock()
|
|
|
|
defer p.stopLock.Unlock()
|
|
|
|
return p.stop
|
|
|
|
}
|