2019-03-11 18:56:48 +02:00
|
|
|
package wait
|
2016-03-11 04:52:46 +02:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"time"
|
2018-09-24 21:07:20 +02:00
|
|
|
|
2020-09-02 03:20:01 +02:00
|
|
|
"github.com/go-acme/lego/v4/log"
|
2016-03-11 04:52:46 +02:00
|
|
|
)
|
|
|
|
|
2018-12-06 23:50:17 +02:00
|
|
|
// For polls the given function 'f', once every 'interval', up to 'timeout'.
|
2018-12-22 01:53:05 +02: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 21:07:20 +02:00
|
|
|
|
2020-02-27 20:14:46 +02:00
|
|
|
var lastErr error
|
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:
|
2021-05-14 17:37:45 +02:00
|
|
|
if lastErr == nil {
|
2023-05-05 09:49:38 +02:00
|
|
|
return fmt.Errorf("%s: time limit exceeded", msg)
|
2021-05-14 17:37:45 +02:00
|
|
|
}
|
2023-05-05 09:49:38 +02:00
|
|
|
return fmt.Errorf("%s: time limit exceeded: last error: %w", msg, lastErr)
|
2016-03-11 04:52:46 +02:00
|
|
|
default:
|
|
|
|
}
|
|
|
|
|
|
|
|
stop, err := f()
|
|
|
|
if stop {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if err != nil {
|
2020-02-27 20:14:46 +02:00
|
|
|
lastErr = err
|
2016-03-11 04:52:46 +02:00
|
|
|
}
|
|
|
|
|
2016-03-11 05:51:02 +02:00
|
|
|
time.Sleep(interval)
|
2016-03-11 04:52:46 +02:00
|
|
|
}
|
|
|
|
}
|