Skip to content

Commit

Permalink
feat(inference): add waiter (#2086)
Browse files Browse the repository at this point in the history
  • Loading branch information
Laure-di authored May 27, 2024
1 parent 26f8c2e commit 99d7702
Showing 1 changed file with 58 additions and 0 deletions.
58 changes: 58 additions & 0 deletions api/inference/v1beta1/inference_utils.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
package inference

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

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
}

0 comments on commit 99d7702

Please sign in to comment.