dispatch/vendor/github.com/xenolf/lego/platform/wait/wait.go

34 lines
610 B
Go
Raw Normal View History

package wait
import (
"fmt"
"time"
2018-11-10 07:04:36 +00:00
"github.com/xenolf/lego/log"
)
// For polls the given function 'f', once every 'interval', up to 'timeout'.
2019-01-23 07:52:17 +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-11-10 07:04:36 +00:00
var lastErr string
2018-11-10 07:04:36 +00:00
timeUp := time.After(timeout)
for {
select {
2018-11-10 07:04:36 +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()
}
time.Sleep(interval)
}
}