-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstate.go
57 lines (46 loc) · 1.32 KB
/
state.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package client
import (
"fmt"
"time"
)
// stateRefreshFunc is a function responsible for refreshing the item being watched for a state change.
type stateRefreshFunc[T any] func() (result *T, state string, err error)
type StateChangeOps[T any] struct {
Pending []string
Target []string
Refresh stateRefreshFunc[T]
Timeout time.Duration
Delay time.Duration
}
// WaitForState polls a resource until it reaches the desired state.
// This is useful for resources whose creation is followed by an async process
// such as Data Pool connections or Job completions.
func WaitForState[T any](ops StateChangeOps[T]) (*T, error) {
start := time.Now()
for {
elapsed := time.Since(start)
if elapsed.Milliseconds() > ops.Timeout.Milliseconds() {
return nil, fmt.Errorf("timeout waiting for state to change to %q", ops.Target)
}
res, currentState, err := ops.Refresh()
if err != nil {
return nil, err
}
for _, targetState := range ops.Target {
if currentState == targetState {
return res, nil
}
}
pendingFound := false
for _, pendingState := range ops.Pending {
if currentState == pendingState {
pendingFound = true
break
}
}
if !pendingFound {
return nil, fmt.Errorf("received an unexpected state %s. Expected states are %q", currentState, ops.Target)
}
time.Sleep(ops.Delay)
}
}