|
| 1 | +package edge_services //nolint:revive |
| 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 = 5 * time.Second |
| 13 | + defaultTimeout = 5 * time.Minute |
| 14 | +) |
| 15 | + |
| 16 | +// WaitForPipelineRequest is used by WaitForPipeline method. |
| 17 | +type WaitForPipelineRequest struct { |
| 18 | + PipelineID string |
| 19 | + Timeout *time.Duration |
| 20 | + RetryInterval *time.Duration |
| 21 | +} |
| 22 | + |
| 23 | +// WaitForPipeline wait for a pipeline to be in a "terminal state" before returning. |
| 24 | +func (s *API) WaitForPipeline(req *WaitForPipelineRequest, opts ...scw.RequestOption) (*Pipeline, 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[PipelineStatus]struct{}{ |
| 35 | + PipelineStatusReady: {}, |
| 36 | + PipelineStatusError: {}, |
| 37 | + PipelineStatusUnknownStatus: {}, |
| 38 | + PipelineStatusWarning: {}, |
| 39 | + } |
| 40 | + |
| 41 | + res, err := async.WaitSync(&async.WaitSyncConfig{ |
| 42 | + Get: func() (interface{}, bool, error) { |
| 43 | + pipeline, err := s.GetPipeline(&GetPipelineRequest{ |
| 44 | + PipelineID: req.PipelineID, |
| 45 | + }, opts...) |
| 46 | + if err != nil { |
| 47 | + return nil, false, err |
| 48 | + } |
| 49 | + |
| 50 | + _, isTerminal := terminalStatus[pipeline.Status] |
| 51 | + return pipeline, isTerminal, nil |
| 52 | + }, |
| 53 | + Timeout: timeout, |
| 54 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 55 | + }) |
| 56 | + if err != nil { |
| 57 | + return nil, errors.Wrap(err, "waiting for pipeline failed") |
| 58 | + } |
| 59 | + |
| 60 | + return res.(*Pipeline), nil |
| 61 | +} |
| 62 | + |
| 63 | +// WaitForPurgeRequestRequest is used by WaitForPurgeRequest method. |
| 64 | +type WaitForPurgeRequestRequest struct { |
| 65 | + PurgeRequestID string |
| 66 | + Timeout *time.Duration |
| 67 | + RetryInterval *time.Duration |
| 68 | +} |
| 69 | + |
| 70 | +// WaitForPurgeRequest wait for a purge request to be in a "terminal state" before returning. |
| 71 | +func (s *API) WaitForPurgeRequest(req *WaitForPurgeRequestRequest, opts ...scw.RequestOption) (*PurgeRequest, error) { |
| 72 | + timeout := defaultTimeout |
| 73 | + if req.Timeout != nil { |
| 74 | + timeout = *req.Timeout |
| 75 | + } |
| 76 | + retryInterval := defaultRetryInterval |
| 77 | + if req.RetryInterval != nil { |
| 78 | + retryInterval = *req.RetryInterval |
| 79 | + } |
| 80 | + |
| 81 | + terminalStatus := map[PurgeRequestStatus]struct{}{ |
| 82 | + PurgeRequestStatusDone: {}, |
| 83 | + PurgeRequestStatusError: {}, |
| 84 | + PurgeRequestStatusUnknownStatus: {}, |
| 85 | + } |
| 86 | + |
| 87 | + res, err := async.WaitSync(&async.WaitSyncConfig{ |
| 88 | + Get: func() (interface{}, bool, error) { |
| 89 | + purgeRequest, err := s.GetPurgeRequest(&GetPurgeRequestRequest{ |
| 90 | + PurgeRequestID: req.PurgeRequestID, |
| 91 | + }, opts...) |
| 92 | + if err != nil { |
| 93 | + return nil, false, err |
| 94 | + } |
| 95 | + |
| 96 | + _, isTerminal := terminalStatus[purgeRequest.Status] |
| 97 | + return purgeRequest, isTerminal, nil |
| 98 | + }, |
| 99 | + Timeout: timeout, |
| 100 | + IntervalStrategy: async.LinearIntervalStrategy(retryInterval), |
| 101 | + }) |
| 102 | + if err != nil { |
| 103 | + return nil, errors.Wrap(err, "waiting for purge request failed") |
| 104 | + } |
| 105 | + |
| 106 | + return res.(*PurgeRequest), nil |
| 107 | +} |
0 commit comments