-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathwait.go
146 lines (125 loc) · 3.45 KB
/
wait.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
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
// Proof of Concepts for the Cloud-Barista Multi-Cloud Project.
// * Cloud-Barista: https://github.com/cloud-barista
//
// This code is based on the following material: https://github.com/mindjiver/gopherstack (MIT License)
//
// KT Cloud SDK go
//
// by ETRI, 2021.07.
package ktcloudsdk
import (
"fmt"
"time"
"github.com/sirupsen/logrus"
cblog "github.com/cloud-barista/cb-log"
)
var cblogger *logrus.Logger
func init() {
// cblog is a global variable.
cblogger = cblog.GetLogger("KT Cloud SDK Go")
}
// Blocks until the the asynchronous job has executed or has timed out.
// time.Duration unit => 1 nanosecond. timeOut * 1,000,000,000 => 1 second
func (c KtCloudClient) WaitForAsyncJob(jobId string, timeOut time.Duration) error {
done := make(chan struct{})
defer close(done)
result := make(chan error, 1)
go func() {
attempts := 0
for {
attempts += 1
cblogger.Infof("Checking the async job status... (attempt: %d)", attempts)
response, err := c.QueryAsyncJobResult(jobId)
if err != nil {
result <- err
return
}
// Check status of the job
// 0 - Pending / In progress, Continue job
// 1 - Succeeded
// 2 - Failed
status := response.Queryasyncjobresultresponse.JobStatus
cblogger.Infof("The job status : %d", status)
switch status {
case 1:
result <- nil
return
case 2:
err := fmt.Errorf("WaitForAsyncJob() failed. : %s", response.Queryasyncjobresultresponse.JobResult.ErrorText)
result <- err
return
}
// Wait 3 seconds between requests
time.Sleep(3 * time.Second)
// Verify whether we shouldn't exit or ...
select {
case <-done:
// Finished, so just exit the goroutine
return
default:
// Keep going
}
}
}()
cblogger.Infof("# Waiting for up to %f seconds for async job : %s", timeOut.Seconds(), jobId)
select {
case err := <-result:
return err
case <-time.After(timeOut):
err := fmt.Errorf("Timeout while waiting to for the async job to finish")
return err
}
}
// WaitForVirtualMachineState simply blocks until the virtual machine is in the specified state.
func (c KtCloudClient) WaitForVirtualMachineState(zoneId string, vmId string, wantedState string, timeOut time.Duration) error {
vmListReqInfo := ListVMReqInfo{
ZoneId: zoneId,
VMId: vmId,
}
done := make(chan struct{})
defer close(done)
result := make(chan error, 1)
go func() {
attempts := 0
for {
attempts += 1
cblogger.Infof("Checking the VM state... (attempt: %d)", attempts)
response, err := c.ListVirtualMachines(vmListReqInfo)
if err != nil {
result <- err
return
}
count := response.Listvirtualmachinesresponse.Count
if count != 1 {
result <- err
return
}
currentState := response.Listvirtualmachinesresponse.Virtualmachine[0].State
// Check what the real state will be.
cblogger.Infof("Current state: %s", currentState)
cblogger.Infof("Wanted state: %s", wantedState)
if currentState == wantedState {
result <- nil
return
}
// Wait 3 seconds in between
time.Sleep(3 * time.Second)
// Verify whether we shouldn't exit or ...
select {
case <-done:
// Finished, so just exit the goroutine
return
default:
// Keep going
}
}
}()
cblogger.Infof("# Waiting for up to %f seconds for VM state to converge", timeOut.Seconds())
select {
case err := <-result:
return err
case <-time.After(timeOut):
err := fmt.Errorf("Timeout while waiting to for the VM to converge")
return err
}
}