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