-
Notifications
You must be signed in to change notification settings - Fork 116
/
cloudbackup.go
64 lines (57 loc) · 1.64 KB
/
cloudbackup.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package volume
import (
"fmt"
"time"
"github.com/cenkalti/backoff"
"github.com/libopenstorage/openstorage/api"
)
var (
cloudsnapBackoffInitialInterval = 5 * time.Second
cloudsnapBackoffDefaultMultiplier = float64(1)
cloudsnapBackoffMaxInterval = 2 * time.Minute
// Wait indefinitely
cloudsnapBackoffMaxElapsedTime = 0 * time.Second
)
func CloudBackupWaitForCompletion(
cl CloudBackupDriver,
taskId string,
opType api.CloudBackupOpType,
) error {
cloudsnapBackoff := backoff.NewExponentialBackOff()
cloudsnapBackoff.InitialInterval = cloudsnapBackoffInitialInterval
cloudsnapBackoff.Multiplier = cloudsnapBackoffDefaultMultiplier
cloudsnapBackoff.MaxInterval = cloudsnapBackoffMaxInterval
cloudsnapBackoff.MaxElapsedTime = cloudsnapBackoffMaxElapsedTime
var opError error
err := backoff.Retry(func() error {
response, err := cl.CloudBackupStatus(&api.CloudBackupStatusRequest{
ID: taskId,
})
if err != nil {
return err
}
csStatus, present := response.Statuses[taskId]
if !present {
opError = fmt.Errorf("failed to get cloudsnap status for volume: %s", taskId)
return nil
}
err = fmt.Errorf("CloudBackup operation %v for %v in state %v", opType, taskId, csStatus.Status)
switch csStatus.Status {
case api.CloudBackupStatusFailed, api.CloudBackupStatusAborted, api.CloudBackupStatusStopped:
opError = err
return nil
case api.CloudBackupStatusDone:
opError = nil
return nil
case api.CloudBackupStatusNotStarted, api.CloudBackupStatusActive, api.CloudBackupStatusPaused:
return err
default:
opError = err
return nil
}
}, cloudsnapBackoff)
if err != nil {
return err
}
return opError
}