Skip to content

Commit c26c02e

Browse files
authored
feat(inference): add sweeper and waiter to v1 (scaleway#2532)
1 parent 7fceb66 commit c26c02e

File tree

2 files changed

+103
-0
lines changed

2 files changed

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

0 commit comments

Comments
 (0)