2017-04-18 01:02:51 +00:00
|
|
|
package acme
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-11-10 07:04:36 +00:00
|
|
|
|
|
|
|
"github.com/xenolf/lego/log"
|
2017-04-18 01:02:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// WaitFor polls the given function 'f', once every 'interval', up to 'timeout'.
|
|
|
|
func WaitFor(timeout, interval time.Duration, f func() (bool, error)) error {
|
2018-11-10 07:04:36 +00:00
|
|
|
log.Infof("Wait [timeout: %s, interval: %s]", timeout, interval)
|
|
|
|
|
2017-04-18 01:02:51 +00:00
|
|
|
var lastErr string
|
2018-11-10 07:04:36 +00:00
|
|
|
timeUp := time.After(timeout)
|
2017-04-18 01:02:51 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-11-10 07:04:36 +00:00
|
|
|
case <-timeUp:
|
|
|
|
return fmt.Errorf("time limit exceeded: last error: %s", lastErr)
|
2017-04-18 01:02:51 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
stop, err := f()
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(interval)
|
|
|
|
}
|
|
|
|
}
|