Skip to content

Commit 99d7702

Browse files
authored
feat(inference): add waiter (#2086)
1 parent 26f8c2e commit 99d7702

File tree

1 file changed

+58
-0
lines changed

1 file changed

+58
-0
lines changed
Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
package inference
2+
3+
import (
4+
"github.com/scaleway/scaleway-sdk-go/internal/async"
5+
"github.com/scaleway/scaleway-sdk-go/internal/errors"
6+
"github.com/scaleway/scaleway-sdk-go/scw"
7+
"time"
8+
)
9+
10+
const (
11+
defaultRetryInterval = 15 * time.Second
12+
defaultTimeout = 30 * time.Minute
13+
)
14+
15+
type WaitForDeploymentRequest struct {
16+
DeploymentId string
17+
Region scw.Region
18+
Status DeploymentStatus
19+
Timeout *time.Duration
20+
RetryInterval *time.Duration
21+
}
22+
23+
func (s *API) WaitForDeployment(req *WaitForDeploymentRequest, opts ...scw.RequestOption) (*Deployment, error) {
24+
timeout := defaultTimeout
25+
if req.Timeout != nil {
26+
timeout = *req.Timeout
27+
}
28+
retryInterval := defaultRetryInterval
29+
if req.RetryInterval != nil {
30+
retryInterval = *req.RetryInterval
31+
}
32+
33+
terminalStatus := map[DeploymentStatus]struct{}{
34+
DeploymentStatusReady: {},
35+
DeploymentStatusError: {},
36+
DeploymentStatusLocked: {},
37+
}
38+
39+
deployment, err := async.WaitSync(&async.WaitSyncConfig{
40+
Get: func() (interface{}, bool, error) {
41+
deployment, err := s.GetDeployment(&GetDeploymentRequest{
42+
Region: req.Region,
43+
DeploymentID: req.DeploymentId,
44+
}, opts...)
45+
if err != nil {
46+
return nil, false, err
47+
}
48+
_, isTerminal := terminalStatus[deployment.Status]
49+
return deployment, isTerminal, nil
50+
},
51+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
52+
Timeout: timeout,
53+
})
54+
if err != nil {
55+
return nil, errors.Wrap(err, "waiting for deployment failed")
56+
}
57+
return deployment.(*Deployment), nil
58+
}

0 commit comments

Comments
 (0)