Skip to content

Commit adcf20a

Browse files
authored
feat: add waiters for snapshots (#2386)
1 parent 44bd554 commit adcf20a

File tree

1 file changed

+46
-0
lines changed

1 file changed

+46
-0
lines changed

api/rdb/v1/rdb_utils.go

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -222,3 +222,49 @@ func (s *API) FetchLatestEngineVersion(engineName string) (*EngineVersion, error
222222
}
223223
return latestEngineVersion, nil
224224
}
225+
226+
// WaitForSnapshotRequest is used by WaitForSnapshot method.
227+
type WaitForSnapshotRequest struct {
228+
SnapshotID string
229+
Region scw.Region
230+
Timeout *time.Duration
231+
RetryInterval *time.Duration
232+
}
233+
234+
func (s *API) WaitForSnapshot(req *WaitForSnapshotRequest, opts ...scw.RequestOption) (*Snapshot, error) {
235+
timeout := defaultTimeout
236+
if req.Timeout != nil {
237+
timeout = *req.Timeout
238+
}
239+
retryInterval := defaultRetryInterval
240+
if req.RetryInterval != nil {
241+
retryInterval = *req.RetryInterval
242+
}
243+
244+
terminalStatus := map[SnapshotStatus]struct{}{
245+
SnapshotStatusReady: {},
246+
SnapshotStatusError: {},
247+
SnapshotStatusLocked: {},
248+
}
249+
250+
snapshot, err := async.WaitSync(&async.WaitSyncConfig{
251+
Get: func() (interface{}, bool, error) {
252+
res, err := s.GetSnapshot(&GetSnapshotRequest{
253+
SnapshotID: req.SnapshotID,
254+
Region: req.Region,
255+
}, opts...)
256+
if err != nil {
257+
return nil, false, err
258+
}
259+
_, isTerminal := terminalStatus[res.Status]
260+
261+
return res, isTerminal, nil
262+
},
263+
Timeout: timeout,
264+
IntervalStrategy: async.LinearIntervalStrategy(retryInterval),
265+
})
266+
if err != nil {
267+
return nil, errors.Wrap(err, "waiting for snapshot failed")
268+
}
269+
return snapshot.(*Snapshot), nil
270+
}

0 commit comments

Comments
 (0)