2018-04-06 15:04:03 +00:00
|
|
|
package acmev2
|
2018-03-26 12:12:03 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
|
|
|
)
|
|
|
|
|
|
|
|
// WaitFor polls the given function 'f', once every 'interval', up to 'timeout'.
|
|
|
|
func WaitFor(timeout, interval time.Duration, f func() (bool, error)) error {
|
|
|
|
var lastErr string
|
|
|
|
timeup := time.After(timeout)
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timeup:
|
|
|
|
return fmt.Errorf("Time limit exceeded. Last error: %s", lastErr)
|
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
stop, err := f()
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
|
|
|
lastErr = err.Error()
|
|
|
|
}
|
|
|
|
|
|
|
|
time.Sleep(interval)
|
|
|
|
}
|
|
|
|
}
|