open-nomad/command/agent/retry_join.go

71 lines
1.3 KiB
Go
Raw Normal View History

2018-04-18 16:18:18 +00:00
package agent
import (
"log"
2018-04-18 22:09:46 +00:00
"strings"
2018-04-18 16:18:18 +00:00
"time"
)
type DiscoverInterface interface {
Addrs(string, *log.Logger) ([]string, error)
Help() string
Names() []string
}
type retryJoiner struct {
attempt int
join func([]string) (int, error)
discover DiscoverInterface
errCh chan struct{}
logger *log.Logger
}
func (r *retryJoiner) RetryJoin(config *Config) {
if len(config.Server.RetryJoin) == 0 || !config.Server.Enabled {
return
}
r.logger.Printf("[INFO] agent: Joining cluster...")
for {
2018-04-18 22:09:46 +00:00
var addrs []string
for _, addr := range config.Server.RetryJoin {
switch {
case strings.Contains(addr, "provider"):
servers, err := r.discover.Addrs(addr, r.logger)
if err != nil {
r.logger.Printf("[ERR] agent: Join error %s", err)
} else {
addrs = append(addrs, servers...)
}
default:
addrs = append(addrs, addr)
}
}
2018-04-18 16:18:18 +00:00
n, err := r.join(addrs)
if err == nil {
r.logger.Printf("[INFO] agent: Join completed. Synced with %d initial agents", n)
return
}
r.attempt++
if config.Server.RetryMaxAttempts > 0 && r.attempt > config.Server.RetryMaxAttempts {
r.logger.Printf("[ERR] agent: max join retry exhausted, exiting")
close(r.errCh)
return
}
r.logger.Printf("[WARN] agent: Join failed: %v, retrying in %v", err,
config.Server.RetryInterval)
time.Sleep(config.Server.retryInterval)
}
}