Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
59 changes: 59 additions & 0 deletions api/inference/v1/inference_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,59 @@
package inference

import (
"time"

"github.com/scaleway/scaleway-sdk-go/errors"
"github.com/scaleway/scaleway-sdk-go/internal/async"
"github.com/scaleway/scaleway-sdk-go/scw"
)

const (
defaultRetryInterval = 15 * time.Second
defaultTimeout = 30 * time.Minute
)

type WaitForDeploymentRequest struct {
DeploymentID string
Region scw.Region
Status DeploymentStatus
Timeout *time.Duration
RetryInterval *time.Duration
}

func (s *API) WaitForDeployment(req *WaitForDeploymentRequest, opts ...scw.RequestOption) (*Deployment, error) {
timeout := defaultTimeout
if req.Timeout != nil {
timeout = *req.Timeout
}
retryInterval := defaultRetryInterval
if req.RetryInterval != nil {
retryInterval = *req.RetryInterval
}

terminalStatus := map[DeploymentStatus]struct{}{
DeploymentStatusReady: {},
DeploymentStatusError: {},
DeploymentStatusLocked: {},
}

deployment, err := async.WaitSync(&async.WaitSyncConfig{
Get: func() (interface{}, bool, error) {
deployment, err := s.GetDeployment(&GetDeploymentRequest{
Region: req.Region,
DeploymentID: req.DeploymentID,
}, opts...)
if err != nil {
return nil, false, err
}
_, isTerminal := terminalStatus[deployment.Status]
return deployment, isTerminal, nil
},
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
Timeout: timeout,
})
if err != nil {
return nil, errors.Wrap(err, "waiting for deployment failed")
}
return deployment.(*Deployment), nil
}
44 changes: 44 additions & 0 deletions api/inference/v1/sweepers/sweepers.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,44 @@
package sweepers

import (
"fmt"

"github.com/scaleway/scaleway-sdk-go/api/inference/v1"
"github.com/scaleway/scaleway-sdk-go/logger"
"github.com/scaleway/scaleway-sdk-go/scw"
)

func SweepDeployment(scwClient *scw.Client, region scw.Region) error {
inferenceAPI := inference.NewAPI(scwClient)
logger.Warningf("sweeper: destroying the inference deployments in (%s)", region)
listDeployments, err := inferenceAPI.ListDeployments(
&inference.ListDeploymentsRequest{
Region: region,
}, scw.WithAllPages())
if err != nil {
return fmt.Errorf("error listing deployment in (%s) in sweeper: %s", region, err)
}

for _, deployment := range listDeployments.Deployments {
_, err := inferenceAPI.DeleteDeployment(&inference.DeleteDeploymentRequest{
DeploymentID: deployment.ID,
Region: region,
})
if err != nil {
return fmt.Errorf("error deleting deployment in sweeper: %s", err)
}
}

return nil
}

func SweepAllLocalities(scwClient *scw.Client) error {
for _, locality := range (&inference.API{}).Regions() {
err := SweepDeployment(scwClient, locality)
if err != nil {
return err
}
}

return nil
}