-
Notifications
You must be signed in to change notification settings - Fork 19
/
request.go
177 lines (141 loc) · 3.46 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
package cmd
import (
"fmt"
"math/rand"
"sync"
"time"
"github.com/exoscale/egoscale"
"github.com/vbauerster/mpb"
"github.com/vbauerster/mpb/decor"
)
const (
parallelTask = 20
)
type task struct {
egoscale.AsyncCommand
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 := 10
var taskWG sync.WaitGroup
p := mpb.New(mpb.WithWaitGroup(&taskWG), mpb.WithContext(gContext), mpb.WithWidth(40))
taskWG.Add(len(tasks))
var workerWG sync.WaitGroup
workerWG.Add(len(tasks))
workerSem := make(chan int, parallelTask)
//exec task and init bars
for i, task := range tasks {
c := make(chan taskStatus)
go execTask(task, i, c, &responses[i], workerSem, &workerWG)
taskBars[i] = p.AddBar(int64(maximum),
mpb.PrependDecorators(
// simple name decorator
decor.Name(task.string),
// decor.DSyncWidth bit enables column width synchronization
decor.Percentage(decor.WCSyncSpace),
),
)
taskSem := make(chan int, parallelTask)
//listen for bar progress
go func(chanel chan taskStatus, sem chan int) {
defer taskWG.Done()
defer close(chanel)
sem <- 1
max := 100 * time.Millisecond
count := 1
for status := range chanel {
start := time.Now()
time.Sleep(time.Duration(rand.Intn(10)+1) * max / 10)
if status.jobStatus == egoscale.Pending {
if count < maximum {
taskBars[status.id].IncrBy(1, time.Since(start))
}
} else {
taskBars[status.id].IncrBy(maximum, time.Since(start))
return
}
count++
}
<-sem
}(c, taskSem)
}
workerWG.Wait()
p.Wait()
return responses
}
func execTask(task task, id int, c chan taskStatus, resp *taskResponse, sem chan int, wg *sync.WaitGroup) {
defer wg.Done()
sem <- 1
response := cs.Response(task.AsyncCommand)
var errorReq error
cs.AsyncRequestWithContext(gContext, task.AsyncCommand, 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", task.string, 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
}
// asyncRequest if no response expected send nil
func asyncRequest(cmd egoscale.AsyncCommand, msg string) (interface{}, error) {
response := cs.Response(cmd)
fmt.Print(msg)
var errorReq error
cs.AsyncRequestWithContext(gContext, cmd, func(jobResult *egoscale.AsyncJobResult, err error) bool {
fmt.Print(".")
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
}
fmt.Println(" success.")
return false
})
if errorReq != nil {
fmt.Println(" failure!")
}
return response, errorReq
}