2024-06-17 18:51:02 +00:00
|
|
|
package daemon
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"time"
|
2024-07-06 13:36:48 +00:00
|
|
|
|
|
|
|
"dev.mediocregopher.com/mediocre-go-lib.git/mlog"
|
2024-06-17 18:51:02 +00:00
|
|
|
)
|
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
// until keeps trying fn until it returns nil, returning true. If the context is
|
|
|
|
// canceled then until returns false.
|
|
|
|
func until(
|
|
|
|
ctx context.Context,
|
|
|
|
logger *mlog.Logger,
|
|
|
|
descr string,
|
|
|
|
fn func(context.Context) error,
|
|
|
|
) bool {
|
2024-06-17 18:51:02 +00:00
|
|
|
for {
|
2024-07-06 13:36:48 +00:00
|
|
|
logger.Info(ctx, descr)
|
|
|
|
err := fn(ctx)
|
|
|
|
if err == nil {
|
|
|
|
return true
|
2024-06-17 18:51:02 +00:00
|
|
|
} else if ctxErr := ctx.Err(); ctxErr != nil {
|
2024-07-06 13:36:48 +00:00
|
|
|
return false
|
2024-06-17 18:51:02 +00:00
|
|
|
}
|
|
|
|
|
2024-07-06 13:36:48 +00:00
|
|
|
logger.Warn(ctx, descr+" failed, retrying in one second", err)
|
2024-06-17 18:51:02 +00:00
|
|
|
time.Sleep(1 * time.Second)
|
|
|
|
}
|
|
|
|
}
|