-
Notifications
You must be signed in to change notification settings - Fork 20
/
request.go
230 lines (190 loc) · 4.87 KB
/
request.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
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package cmd
import (
"fmt"
"os"
"sync"
"time"
"github.com/exoscale/egoscale"
"github.com/hashicorp/go-multierror"
"github.com/vbauerster/mpb/v4"
"github.com/vbauerster/mpb/v4/decor"
)
const (
parallelTask = 20
)
type task struct {
egoscale.Command
string
}
type taskStatus struct {
id int
jobStatus egoscale.JobStatusType
}
type taskResponse struct {
resp interface{}
error
}
func asyncTasks(tasks []task) []taskResponse {
//init results
responses := make([]taskResponse, len(tasks))
//create task Progress
taskBars := make([]*mpb.Bar, len(tasks))
maximum := 1 << 30
var taskWG sync.WaitGroup
progress := mpb.NewWithContext(gContext,
mpb.WithOutput(os.Stderr),
mpb.WithWaitGroup(&taskWG),
mpb.ContainerOptOnCond(mpb.WithOutput(nil), func() bool { return gQuiet }),
)
taskWG.Add(len(tasks))
var workerWG sync.WaitGroup
workerWG.Add(len(tasks))
workerSem := make(chan int, parallelTask)
max := 50 * time.Millisecond
//exec task and init bars
for i, task := range tasks {
c := make(chan taskStatus)
switch cmd := task.Command.(type) {
case egoscale.AsyncCommand:
go execTask(cmd, task.string, i, c, &responses[i], workerSem, &workerWG)
default:
go execSyncTask(task, i, c, &responses[i], workerSem, &workerWG)
}
taskBars[i] = progress.AddSpinner(int64(maximum),
mpb.SpinnerOnLeft,
mpb.PrependDecorators(
// simple name decorator
decor.Name(task.string),
),
mpb.AppendDecorators(decor.OnComplete(decor.Elapsed(decor.ET_STYLE_GO), "done")),
)
//listen for bar progress
go func(channel chan taskStatus, idx int) {
defer taskWG.Done()
defer close(channel)
start := time.Now()
// for select + sleep
for {
select {
case status := <-channel:
if status.jobStatus != egoscale.Pending {
taskBars[idx].IncrBy(maximum, time.Since(start))
return
}
case <-time.After(max):
// do nothing
}
taskBars[idx].IncrBy(1, time.Since(start))
}
}(c, i)
}
workerWG.Wait()
progress.Wait()
return responses
}
func execTask(task egoscale.AsyncCommand, message string, id int, c chan taskStatus, resp *taskResponse, sem chan int, wg *sync.WaitGroup) {
defer wg.Done()
sem <- 1
response := cs.Response(task)
var errorReq error
cs.AsyncRequestWithContext(gContext, task, func(jobResult *egoscale.AsyncJobResult, err error) bool {
if err != nil {
errorReq = err
return false
}
if jobResult.JobStatus != egoscale.Pending {
if errR := jobResult.Result(response); errR != nil {
errorReq = errR
}
return false
}
c <- taskStatus{id, egoscale.Pending}
return true
})
if errorReq == nil {
(*resp).resp = response
c <- taskStatus{id, egoscale.Success}
} else {
c <- taskStatus{id, egoscale.Failure}
(*resp).error = fmt.Errorf("failure %s: %s", message, errorReq)
}
<-sem
}
// filterErrors return all task with an error
func filterErrors(tasks []taskResponse) []error {
var r []error
for _, task := range tasks {
if task.error != nil {
r = append(r, task.error)
}
}
return r
}
func execSyncTask(task task, id int, c chan taskStatus, resp *taskResponse, sem chan int, wg *sync.WaitGroup) {
defer wg.Done()
sem <- 1
_, ok := cs.Response(task.Command).(*egoscale.BooleanResponse)
if ok {
if err := cs.BooleanRequestWithContext(gContext, task.Command); err != nil {
c <- taskStatus{id, egoscale.Failure}
(*resp).error = fmt.Errorf("failure %s: %s", task.string, err)
return
}
c <- taskStatus{id, egoscale.Success}
<-sem
return
}
result, err := cs.RequestWithContext(gContext, task.Command)
if err != nil {
c <- taskStatus{id, egoscale.Failure}
(*resp).error = fmt.Errorf("failure %s: %s", task.string, err)
return
}
(*resp).resp = result
c <- taskStatus{id, egoscale.Success}
<-sem
}
// asyncRequest if no response expected send nil
func asyncRequest(cmd egoscale.AsyncCommand, msg string) (interface{}, error) {
response := cs.Response(cmd)
if !gQuiet {
fmt.Fprint(os.Stderr, msg)
}
var errorReq error
cs.AsyncRequestWithContext(gContext, cmd, func(jobResult *egoscale.AsyncJobResult, err error) bool {
if !gQuiet {
fmt.Fprint(os.Stderr, ".")
}
if err != nil {
errorReq = err
return false
}
if jobResult.JobStatus == egoscale.Pending {
return true
}
if errR := jobResult.Result(response); errR != nil {
errorReq = errR
return false
}
if !gQuiet {
fmt.Fprintln(os.Stderr, " success")
}
return false
})
if errorReq != nil && !gQuiet {
fmt.Fprintln(os.Stderr, " failure")
}
return response, errorReq
}
// forEachZone executes the function f for each specified zone, and return a multierror.Error containing all
// errors that may have occurred during execution.
func forEachZone(zones []string, f func(zone string) error) error {
var meg = new(multierror.Group)
for _, zone := range zones {
zone := zone
meg.Go(func() error {
return f(zone)
})
}
return meg.Wait().ErrorOrNil()
}