2014-10-08 06:44:51 +02:00
|
|
|
package resource
|
|
|
|
|
|
|
|
import (
|
|
|
|
"log"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
2017-04-19 18:14:23 +02:00
|
|
|
var refreshGracePeriod = 30 * time.Second
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
// StateRefreshFunc is a function type used for StateChangeConf that is
|
|
|
|
// responsible for refreshing the item being watched for a state change.
|
|
|
|
//
|
|
|
|
// It returns three results. `result` is any object that will be returned
|
|
|
|
// as the final object after waiting for state change. This allows you to
|
|
|
|
// return the final updated object, for example an EC2 instance after refreshing
|
|
|
|
// it.
|
|
|
|
//
|
|
|
|
// `state` is the latest state of that object. And `err` is any error that
|
|
|
|
// may have happened while refreshing the state.
|
|
|
|
type StateRefreshFunc func() (result interface{}, state string, err error)
|
|
|
|
|
|
|
|
// StateChangeConf is the configuration struct used for `WaitForState`.
|
|
|
|
type StateChangeConf struct {
|
2015-02-26 18:59:42 +01:00
|
|
|
Delay time.Duration // Wait this time before starting checks
|
|
|
|
Pending []string // States that are "allowed" and will continue trying
|
|
|
|
Refresh StateRefreshFunc // Refreshes the current state
|
2016-01-21 02:20:41 +01:00
|
|
|
Target []string // Target state
|
2015-02-26 18:59:42 +01:00
|
|
|
Timeout time.Duration // The amount of time to wait before timeout
|
|
|
|
MinTimeout time.Duration // Smallest time to wait before refreshes
|
2016-07-13 23:38:23 +02:00
|
|
|
PollInterval time.Duration // Override MinTimeout/backoff and only poll this often
|
2015-02-26 18:59:42 +01:00
|
|
|
NotFoundChecks int // Number of times to allow not found
|
2015-12-25 21:41:40 +01:00
|
|
|
|
|
|
|
// This is to work around inconsistent APIs
|
|
|
|
ContinuousTargetOccurence int // Number of times the Target state has to occur continuously
|
2014-10-08 06:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// WaitForState watches an object and waits for it to achieve the state
|
|
|
|
// specified in the configuration using the specified Refresh() func,
|
|
|
|
// waiting the number of seconds specified in the timeout configuration.
|
2015-04-28 18:31:56 +02:00
|
|
|
//
|
|
|
|
// If the Refresh function returns a error, exit immediately with that error.
|
|
|
|
//
|
|
|
|
// If the Refresh function returns a state other than the Target state or one
|
|
|
|
// listed in Pending, return immediately with an error.
|
|
|
|
//
|
|
|
|
// If the Timeout is exceeded before reaching the Target state, return an
|
|
|
|
// error.
|
|
|
|
//
|
|
|
|
// Otherwise, result the result of the first call to the Refresh function to
|
|
|
|
// reach the target state.
|
2014-10-08 06:44:51 +02:00
|
|
|
func (conf *StateChangeConf) WaitForState() (interface{}, error) {
|
|
|
|
log.Printf("[DEBUG] Waiting for state to become: %s", conf.Target)
|
|
|
|
|
|
|
|
notfoundTick := 0
|
2015-12-25 21:41:40 +01:00
|
|
|
targetOccurence := 0
|
2014-10-08 06:44:51 +02:00
|
|
|
|
2015-02-26 18:59:42 +01:00
|
|
|
// Set a default for times to check for not found
|
|
|
|
if conf.NotFoundChecks == 0 {
|
|
|
|
conf.NotFoundChecks = 20
|
|
|
|
}
|
|
|
|
|
2015-12-25 21:41:40 +01:00
|
|
|
if conf.ContinuousTargetOccurence == 0 {
|
|
|
|
conf.ContinuousTargetOccurence = 1
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:22:21 +02:00
|
|
|
type Result struct {
|
|
|
|
Result interface{}
|
|
|
|
State string
|
|
|
|
Error error
|
2017-04-19 16:10:07 +02:00
|
|
|
Done bool
|
2016-08-30 22:22:21 +02:00
|
|
|
}
|
2014-10-08 06:44:51 +02:00
|
|
|
|
2017-04-19 18:14:23 +02:00
|
|
|
// Read every result from the refresh loop, waiting for a positive result.Done.
|
2017-04-19 16:10:07 +02:00
|
|
|
resCh := make(chan Result, 1)
|
|
|
|
// cancellation channel for the refresh loop
|
|
|
|
cancelCh := make(chan struct{})
|
|
|
|
|
2017-04-19 18:14:23 +02:00
|
|
|
result := Result{}
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
go func() {
|
2017-04-19 16:10:07 +02:00
|
|
|
defer close(resCh)
|
2014-10-08 06:44:51 +02:00
|
|
|
|
|
|
|
time.Sleep(conf.Delay)
|
|
|
|
|
2017-04-19 16:10:07 +02:00
|
|
|
// start with 0 delay for the first loop
|
|
|
|
var wait time.Duration
|
2016-08-22 10:15:46 +02:00
|
|
|
|
2016-08-30 23:56:26 +02:00
|
|
|
for {
|
2017-04-19 18:14:23 +02:00
|
|
|
// store the last result
|
|
|
|
resCh <- result
|
|
|
|
|
2017-04-19 16:10:07 +02:00
|
|
|
// wait and watch for cancellation
|
|
|
|
select {
|
|
|
|
case <-cancelCh:
|
|
|
|
return
|
|
|
|
case <-time.After(wait):
|
|
|
|
// first round had no wait
|
|
|
|
if wait == 0 {
|
|
|
|
wait = 100 * time.Millisecond
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-08-30 22:22:21 +02:00
|
|
|
res, currentState, err := conf.Refresh()
|
2017-04-19 18:14:23 +02:00
|
|
|
result = Result{
|
2016-08-30 22:22:21 +02:00
|
|
|
Result: res,
|
|
|
|
State: currentState,
|
|
|
|
Error: err,
|
|
|
|
}
|
|
|
|
|
2014-10-08 06:44:51 +02:00
|
|
|
if err != nil {
|
2017-04-19 18:14:23 +02:00
|
|
|
resCh <- result
|
2014-10-08 06:44:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// If we're waiting for the absence of a thing, then return
|
2016-08-30 22:22:21 +02:00
|
|
|
if res == nil && len(conf.Target) == 0 {
|
2017-04-20 00:19:48 +02:00
|
|
|
targetOccurence++
|
2015-12-25 21:41:40 +01:00
|
|
|
if conf.ContinuousTargetOccurence == targetOccurence {
|
2017-04-19 16:10:07 +02:00
|
|
|
result.Done = true
|
|
|
|
resCh <- result
|
2015-12-25 21:41:40 +01:00
|
|
|
return
|
|
|
|
}
|
2017-04-20 00:19:48 +02:00
|
|
|
continue
|
2014-10-08 06:44:51 +02:00
|
|
|
}
|
|
|
|
|
2016-08-30 22:22:21 +02:00
|
|
|
if res == nil {
|
2014-10-08 06:44:51 +02:00
|
|
|
// If we didn't find the resource, check if we have been
|
|
|
|
// not finding it for awhile, and if so, report an error.
|
2017-04-20 00:19:48 +02:00
|
|
|
notfoundTick++
|
2015-02-26 18:59:42 +01:00
|
|
|
if notfoundTick > conf.NotFoundChecks {
|
2016-08-30 22:22:21 +02:00
|
|
|
result.Error = &NotFoundError{
|
|
|
|
LastError: err,
|
2016-03-15 14:58:06 +01:00
|
|
|
Retries: notfoundTick,
|
2016-06-02 01:50:43 +02:00
|
|
|
}
|
2017-04-19 16:10:07 +02:00
|
|
|
resCh <- result
|
2014-10-08 06:44:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// Reset the counter for when a resource isn't found
|
|
|
|
notfoundTick = 0
|
2015-12-25 21:41:40 +01:00
|
|
|
found := false
|
2014-10-08 06:44:51 +02:00
|
|
|
|
2016-01-21 02:20:41 +01:00
|
|
|
for _, allowed := range conf.Target {
|
|
|
|
if currentState == allowed {
|
2015-12-25 21:41:40 +01:00
|
|
|
found = true
|
2017-04-20 00:19:48 +02:00
|
|
|
targetOccurence++
|
2015-12-25 21:41:40 +01:00
|
|
|
if conf.ContinuousTargetOccurence == targetOccurence {
|
2017-04-19 16:10:07 +02:00
|
|
|
result.Done = true
|
|
|
|
resCh <- result
|
2015-12-25 21:41:40 +01:00
|
|
|
return
|
|
|
|
}
|
2017-04-20 00:19:48 +02:00
|
|
|
continue
|
2016-01-21 02:20:41 +01:00
|
|
|
}
|
2014-10-08 06:44:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, allowed := range conf.Pending {
|
|
|
|
if currentState == allowed {
|
|
|
|
found = true
|
2015-12-25 21:41:40 +01:00
|
|
|
targetOccurence = 0
|
2014-10-08 06:44:51 +02:00
|
|
|
break
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-03-28 21:38:42 +02:00
|
|
|
if !found && len(conf.Pending) > 0 {
|
2016-08-30 22:22:21 +02:00
|
|
|
result.Error = &UnexpectedStateError{
|
|
|
|
LastError: err,
|
|
|
|
State: result.State,
|
2016-06-02 01:50:43 +02:00
|
|
|
ExpectedState: conf.Target,
|
|
|
|
}
|
2017-04-19 16:10:07 +02:00
|
|
|
resCh <- result
|
2014-10-08 06:44:51 +02:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2016-08-30 23:56:26 +02:00
|
|
|
|
2017-04-19 18:14:23 +02:00
|
|
|
// Wait between refreshes using exponential backoff, except when
|
|
|
|
// waiting for the target state to reoccur.
|
|
|
|
if targetOccurence == 0 {
|
|
|
|
wait *= 2
|
|
|
|
}
|
|
|
|
|
2016-08-30 23:56:26 +02:00
|
|
|
// If a poll interval has been specified, choose that interval.
|
|
|
|
// Otherwise bound the default value.
|
|
|
|
if conf.PollInterval > 0 && conf.PollInterval < 180*time.Second {
|
|
|
|
wait = conf.PollInterval
|
|
|
|
} else {
|
|
|
|
if wait < conf.MinTimeout {
|
|
|
|
wait = conf.MinTimeout
|
|
|
|
} else if wait > 10*time.Second {
|
|
|
|
wait = 10 * time.Second
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
log.Printf("[TRACE] Waiting %s before next try", wait)
|
2014-10-08 06:44:51 +02:00
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
2017-04-19 16:10:07 +02:00
|
|
|
// store the last value result from the refresh loop
|
|
|
|
lastResult := Result{}
|
|
|
|
|
|
|
|
timeout := time.After(conf.Timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r, ok := <-resCh:
|
|
|
|
// channel closed, so return the last result
|
|
|
|
if !ok {
|
|
|
|
return lastResult.Result, lastResult.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// we reached the intended state
|
|
|
|
if r.Done {
|
|
|
|
return r.Result, r.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
// still waiting, store the last result
|
|
|
|
lastResult = r
|
|
|
|
|
|
|
|
case <-timeout:
|
2017-04-19 18:14:23 +02:00
|
|
|
log.Printf("[WARN] WaitForState timeout after %s", conf.Timeout)
|
|
|
|
log.Printf("[WARN] WaitForState starting %s refresh grace period", refreshGracePeriod)
|
|
|
|
|
|
|
|
// cancel the goroutine and start our grace period timer
|
2017-04-19 16:10:07 +02:00
|
|
|
close(cancelCh)
|
2017-04-19 18:14:23 +02:00
|
|
|
timeout := time.After(refreshGracePeriod)
|
|
|
|
|
|
|
|
// we need a for loop and a label to break on, because we may have
|
|
|
|
// an extra response value to read, but still want to wait for the
|
|
|
|
// channel to close.
|
|
|
|
forSelect:
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case r, ok := <-resCh:
|
|
|
|
if r.Done {
|
|
|
|
// the last refresh loop reached the desired state
|
|
|
|
return r.Result, r.Error
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ok {
|
2017-04-20 00:19:48 +02:00
|
|
|
// the goroutine returned
|
2017-04-19 18:14:23 +02:00
|
|
|
break forSelect
|
|
|
|
}
|
|
|
|
|
|
|
|
// target state not reached, save the result for the
|
|
|
|
// TimeoutError and wait for the channel to close
|
|
|
|
lastResult = r
|
|
|
|
case <-timeout:
|
|
|
|
log.Println("[ERROR] WaitForState exceeded refresh grace period")
|
|
|
|
break forSelect
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-19 16:10:07 +02:00
|
|
|
return nil, &TimeoutError{
|
|
|
|
LastError: lastResult.Error,
|
|
|
|
LastState: lastResult.State,
|
|
|
|
Timeout: conf.Timeout,
|
|
|
|
ExpectedState: conf.Target,
|
|
|
|
}
|
2016-06-02 01:50:43 +02:00
|
|
|
}
|
2014-10-08 06:44:51 +02:00
|
|
|
}
|
|
|
|
}
|