Skip to content

Commit 6bebf90

Browse files
committed
Add a new function to wait for a file creation
Signed-off-by: German Maglione <[email protected]>
1 parent 7e608fa commit 6bebf90

File tree

1 file changed

+16
-0
lines changed

1 file changed

+16
-0
lines changed

pkg/utils/file.go

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,10 @@ package utils
33
import (
44
"bytes"
55
"errors"
6+
"fmt"
67
"os"
78
"strconv"
9+
"time"
810
)
911

1012
func ReadPidFile(pidFile string) (int, error) {
@@ -35,3 +37,17 @@ func FileExists(path string) (bool, error) {
3537
}
3638
return exists, err
3739
}
40+
41+
// WaitForFileWithBackoffs attempts to discover a file in maxBackoffs attempts
42+
func WaitForFileWithBackoffs(maxBackoffs int, backoff time.Duration, path string) error {
43+
backoffWait := backoff
44+
for i := 0; i < maxBackoffs; i++ {
45+
e, _ := FileExists(path)
46+
if e {
47+
return nil
48+
}
49+
time.Sleep(backoffWait)
50+
backoffWait *= 2
51+
}
52+
return fmt.Errorf("unable to find file at %q", path)
53+
}

0 commit comments

Comments
 (0)