-
Notifications
You must be signed in to change notification settings - Fork 414
/
client_queue.go
286 lines (250 loc) · 8.58 KB
/
client_queue.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
package cdsclient
import (
"bytes"
"context"
"encoding/json"
"fmt"
"net/url"
"strconv"
"time"
"github.com/ovh/cds/sdk"
)
// shrinkQueue is used to shrink the polled queue 200% of the channel capacity (l)
// it returns as reference date the date of the last element in the shrinkked queue
func shrinkQueue(queue *sdk.WorkflowQueue, nbJobsToKeep int) time.Time {
if len(*queue) == 0 {
return time.Time{}
}
if nbJobsToKeep < 1 {
nbJobsToKeep = 1
}
// nbJobsToKeep is by default the concurrent max worker provisionning.
// we keep 2x this number
nbJobsToKeep = nbJobsToKeep * 2
queue.Sort()
if len(*queue) > nbJobsToKeep {
newQueue := (*queue)[:nbJobsToKeep]
*queue = newQueue
}
t0 := time.Now()
for _, q := range *queue {
if q.Queued.Before(t0) {
t0 = q.Queued
}
}
return t0
}
func (c *client) QueuePolling(ctx context.Context, goRoutines *sdk.GoRoutines, jobs chan<- sdk.WorkflowNodeJobRun, errs chan<- error, delay time.Duration, ms ...RequestModifier) error {
jobsTicker := time.NewTicker(delay)
// This goroutine call the SSE route
chanMessageReceived := make(chan sdk.WebsocketEvent, 10)
chanMessageToSend := make(chan []sdk.WebsocketFilter, 10)
goRoutines.Exec(ctx, "RequestWebsocket", func(ctx context.Context) {
c.WebsocketEventsListen(ctx, goRoutines, chanMessageToSend, chanMessageReceived, errs)
})
chanMessageToSend <- []sdk.WebsocketFilter{{
Type: sdk.WebsocketFilterTypeQueue,
}}
for {
select {
case <-ctx.Done():
jobsTicker.Stop()
if jobs != nil {
close(jobs)
}
return ctx.Err()
case wsEvent := <-chanMessageReceived:
if jobs == nil {
continue
}
if wsEvent.Event.EventType == "sdk.EventRunWorkflowJob" && wsEvent.Event.Status == sdk.StatusWaiting {
var jobEvent sdk.EventRunWorkflowJob
if err := sdk.JSONUnmarshal(wsEvent.Event.Payload, &jobEvent); err != nil {
errs <- newError(fmt.Errorf("unable to unmarshal job %v: %v", wsEvent.Event.Payload, err))
continue
}
job, err := c.QueueJobInfo(ctx, strconv.FormatInt(jobEvent.ID, 10))
// Do not log the error if the job does not exist
if sdk.ErrorIs(err, sdk.ErrWorkflowNodeRunJobNotFound) {
continue
}
if err != nil {
errs <- newError(fmt.Errorf("unable to get job %v info: %v", jobEvent.ID, err))
continue
}
// push the job in the channel
if job.Status == sdk.StatusWaiting && job.BookedBy.Name == "" {
job.Header["WS"] = "true"
jobs <- *job
}
}
case <-jobsTicker.C:
if c.config.Verbose {
fmt.Println("jobsTicker")
}
if jobs == nil {
continue
}
ctxt, cancel := context.WithTimeout(ctx, 10*time.Second)
queue := sdk.WorkflowQueue{}
if _, err := c.GetJSON(ctxt, "/queue/workflows", &queue, ms...); err != nil && !sdk.ErrorIs(err, sdk.ErrUnauthorized) {
errs <- newError(fmt.Errorf("unable to load jobs: %v", err))
cancel()
continue
} else if sdk.ErrorIs(err, sdk.ErrUnauthorized) {
cancel()
continue
}
cancel()
if c.config.Verbose {
fmt.Println("Jobs Queue size: ", len(queue))
}
shrinkQueue(&queue, cap(jobs))
for _, j := range queue {
jobs <- j
}
}
}
}
func (c *client) QueueWorkflowNodeJobRun(ms ...RequestModifier) ([]sdk.WorkflowNodeJobRun, error) {
wJobs := []sdk.WorkflowNodeJobRun{}
url, _ := url.Parse("/queue/workflows")
if _, err := c.GetJSON(context.Background(), url.String(), &wJobs, ms...); err != nil {
return nil, err
}
return wJobs, nil
}
func (c *client) QueueCountWorkflowNodeJobRun(since *time.Time, until *time.Time, modelType string) (sdk.WorkflowNodeJobRunCount, error) {
if since == nil {
since = new(time.Time)
}
if until == nil {
now := time.Now()
until = &now
}
url, _ := url.Parse("/queue/workflows/count")
q := url.Query()
if modelType != "" {
q.Add("modelType", modelType)
}
url.RawQuery = q.Encode()
countWJobs := sdk.WorkflowNodeJobRunCount{}
_, _, err := c.GetJSONWithHeaders(url.String(),
&countWJobs,
SetHeader(RequestedIfModifiedSinceHeader, since.Format(time.RFC1123)),
SetHeader("X-CDS-Until", until.Format(time.RFC1123)))
return countWJobs, err
}
func (c *client) QueueTakeJob(ctx context.Context, job sdk.WorkflowNodeJobRun) (*sdk.WorkflowNodeJobRunData, error) {
path := fmt.Sprintf("/queue/workflows/%d/take", job.ID)
var info sdk.WorkflowNodeJobRunData
if _, err := c.PostJSON(ctx, path, nil, &info); err != nil {
return nil, err
}
return &info, nil
}
// QueueJobInfo returns information about a job
func (c *client) QueueJobInfo(ctx context.Context, id string) (*sdk.WorkflowNodeJobRun, error) {
path := fmt.Sprintf("/queue/workflows/%s/infos", id)
var job sdk.WorkflowNodeJobRun
if _, err := c.GetJSON(context.Background(), path, &job); err != nil {
return nil, err
}
return &job, nil
}
// QueueJobSendSpawnInfo sends a spawn info on a job
func (c *client) QueueJobSendSpawnInfo(ctx context.Context, id string, in []sdk.SpawnInfo) error {
path := fmt.Sprintf("/queue/workflows/%s/spawn/infos", id)
_, err := c.PostJSON(ctx, path, &in, nil)
return err
}
// QueueJobBook books a job for a Hatchery
func (c *client) QueueJobBook(ctx context.Context, id string) (sdk.WorkflowNodeJobRunBooked, error) {
var resp sdk.WorkflowNodeJobRunBooked
path := fmt.Sprintf("/queue/workflows/%s/book", id)
_, err := c.PostJSON(ctx, path, nil, &resp)
return resp, err
}
func (c *client) QueueWorkflowRunResultsAdd(ctx context.Context, jobID int64, addRequest sdk.WorkflowRunResult) error {
uri := fmt.Sprintf("/queue/workflows/%d/run/results", jobID)
if _, err := c.PostJSON(ctx, uri, addRequest, nil); err != nil {
return err
}
return nil
}
func (c *client) QueueWorkflowRunResultCheck(ctx context.Context, jobID int64, runResultCheck sdk.WorkflowRunResultCheck) (int, error) {
uri := fmt.Sprintf("/queue/workflows/%d/run/results/check", jobID)
code, err := c.PostJSON(ctx, uri, runResultCheck, nil)
return code, err
}
// QueueJobRelease release a job for a worker
func (c *client) QueueJobRelease(ctx context.Context, id string) error {
path := fmt.Sprintf("/queue/workflows/%s/book", id)
_, err := c.DeleteJSON(context.Background(), path, nil)
return err
}
func (c *client) QueueSendResult(ctx context.Context, id int64, res sdk.Result) error {
path := fmt.Sprintf("/queue/workflows/%d/result", id)
b, err := json.Marshal(res)
if err != nil {
return newError(err)
}
result, _, code, err := c.Stream(ctx, c.HTTPNoTimeoutClient(), "POST", path, bytes.NewBuffer(b), nil)
if err != nil {
return err
}
defer result.Close()
if code >= 300 {
return newError(fmt.Errorf("unable to send job result. HTTP code error : %d", code))
}
return nil
}
func (c *client) QueueSendUnitTests(ctx context.Context, id int64, report sdk.JUnitTestsSuites) error {
path := fmt.Sprintf("/queue/workflows/%d/test", id)
_, err := c.PostJSON(ctx, path, report, nil)
return err
}
func (c *client) QueueSendVulnerability(ctx context.Context, id int64, report sdk.VulnerabilityWorkerReport) error {
path := fmt.Sprintf("/queue/workflows/%d/vulnerability", id)
_, err := c.PostJSON(ctx, path, report, nil)
return err
}
func (c *client) QueueSendStepResult(ctx context.Context, id int64, res sdk.StepStatus) error {
path := fmt.Sprintf("/queue/workflows/%d/step", id)
_, err := c.PostJSON(ctx, path, res, nil)
return err
}
func (c *client) QueueWorkerCacheLink(ctx context.Context, jobID int64, tag string) (sdk.CDNItemLinks, error) {
var result sdk.CDNItemLinks
path := fmt.Sprintf("/queue/workflows/%d/cache/%s/links", jobID, tag)
_, err := c.GetJSON(ctx, path, &result, nil)
return result, err
}
func (c *client) QueueJobTag(ctx context.Context, jobID int64, tags []sdk.WorkflowRunTag) error {
path := fmt.Sprintf("/queue/workflows/%d/tag", jobID)
_, err := c.PostJSON(ctx, path, tags, nil)
return err
}
func (c *client) QueueJobSetVersion(ctx context.Context, jobID int64, version sdk.WorkflowRunVersion) error {
path := fmt.Sprintf("/queue/workflows/%d/version", jobID)
_, err := c.PostJSON(ctx, path, version, nil)
return err
}
func (c *client) QueueWorkflowRunResultsRelease(ctx context.Context, permJobID int64, runResultIDs []string, to string) error {
req := sdk.WorkflowRunResultPromotionRequest{
IDs: runResultIDs,
ToMaturity: to,
}
uri := fmt.Sprintf("/queue/workflows/%d/run/results/release", permJobID)
_, err := c.PostJSON(ctx, uri, req, nil)
return err
}
func (c *client) QueueWorkflowRunResultsPromote(ctx context.Context, permJobID int64, runResultIDs []string, to string) error {
req := sdk.WorkflowRunResultPromotionRequest{
IDs: runResultIDs,
ToMaturity: to,
}
uri := fmt.Sprintf("/queue/workflows/%d/run/results/promote", permJobID)
_, err := c.PostJSON(ctx, uri, req, nil)
return err
}