lego/platform/wait/wait.go

34 lines
611 B
Go
Raw Normal View History

2019-03-11 16:56:48 +00:00
package wait
import (
"fmt"
"time"
2019-03-11 16:56:48 +00:00
"github.com/go-acme/lego/log"
)
// 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)
var lastErr string
2018-10-09 17:03:07 +00:00
timeUp := time.After(timeout)
for {
select {
2018-10-09 17:03:07 +00:00
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()
}
2016-03-11 03:51:02 +00:00
time.Sleep(interval)
}
}