2019-03-11 16:56:48 +00:00
|
|
|
package wait
|
2016-03-11 02:52:46 +00:00
|
|
|
|
|
|
|
import (
|
2021-05-14 15:37:45 +00:00
|
|
|
"errors"
|
2016-03-11 02:52:46 +00:00
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-09-24 19:07:20 +00:00
|
|
|
|
2020-09-02 01:20:01 +00:00
|
|
|
"github.com/go-acme/lego/v4/log"
|
2016-03-11 02:52:46 +00:00
|
|
|
)
|
|
|
|
|
2018-12-06 21:50:17 +00:00
|
|
|
// For polls the given function 'f', once every 'interval', up to 'timeout'.
|
2018-12-21 23:53:05 +00:00
|
|
|
func For(msg string, timeout, interval time.Duration, f func() (bool, error)) error {
|
|
|
|
log.Infof("Wait for %s [timeout: %s, interval: %s]", msg, timeout, interval)
|
2018-09-24 19:07:20 +00:00
|
|
|
|
2020-02-27 18:14:46 +00:00
|
|
|
var lastErr error
|
2018-10-09 17:03:07 +00:00
|
|
|
timeUp := time.After(timeout)
|
2016-03-11 02:52:46 +00:00
|
|
|
for {
|
|
|
|
select {
|
2018-10-09 17:03:07 +00:00
|
|
|
case <-timeUp:
|
2021-05-14 15:37:45 +00:00
|
|
|
if lastErr == nil {
|
|
|
|
return errors.New("time limit exceeded")
|
|
|
|
}
|
2020-02-27 18:14:46 +00:00
|
|
|
return fmt.Errorf("time limit exceeded: last error: %w", lastErr)
|
2016-03-11 02:52:46 +00:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
stop, err := f()
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-02-27 18:14:46 +00:00
|
|
|
lastErr = err
|
2016-03-11 02:52:46 +00:00
|
|
|
}
|
|
|
|
|
2016-03-11 03:51:02 +00:00
|
|
|
time.Sleep(interval)
|
2016-03-11 02:52:46 +00:00
|
|
|
}
|
|
|
|
}
|