-
Notifications
You must be signed in to change notification settings - Fork 0
/
queue.go
291 lines (251 loc) · 7.5 KB
/
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
287
288
289
290
291
// Copyright 2021 The Tiyo authors
//
// This Source Code Form is subject to the terms of the Mozilla Public
// License, v. 2.0. If a copy of the MPL was not distributed with this
// file, You can obtain one at https://mozilla.org/MPL/2.0/.
package flow
import (
"bytes"
"encoding/json"
"fmt"
"io/ioutil"
"net/http"
"path/filepath"
"time"
"github.com/notapipeline/tiyo/pkg/config"
"github.com/notapipeline/tiyo/pkg/pipeline"
"github.com/notapipeline/tiyo/pkg/server/api"
log "github.com/sirupsen/logrus"
)
// The queue structure for handling event scheduling into syphon executors
// MAXQUEUE defines how many events should be written into the queue every n seconds
const MAXQUEUE = 100000
// Queue : Defines the structure of the queue item
type Queue struct {
// The queue bucket to write to
QueueBucket string
// The files bucket to write to
FilesBucket string
// A bucket to store pod information in
PodBucket string
// A bucket to store non-filesystem events in
EventsBucket string
// The pipeline bucket to assign against
PipelineBucket string
// Configuration for the queue
Config *config.Config
// The pipeline used by the queue
Pipeline *pipeline.Pipeline
// HTTP client
Client *http.Client
// Is the queue stopped or not
Stopped bool
}
// NewQueue : Create a new queue instance
func NewQueue(config *config.Config, pipeline *pipeline.Pipeline, bucket string) *Queue {
log.Info("Initialising Queue system")
queue := Queue{
QueueBucket: "queue",
FilesBucket: "files",
PodBucket: "pods",
EventsBucket: "events",
PipelineBucket: bucket,
Config: config,
Pipeline: pipeline,
Client: &http.Client{
Timeout: config.TIMEOUT,
},
Stopped: true,
}
queue.createBuckets()
return &queue
}
// TODO: Split this so the api method sits in API and the
// queue management is here.
// Register : Registers a container into the queue executors
func (queue *Queue) Register(request map[string]interface{}) *api.Result {
var key string = request["container"].(string) + ":" + request["pod"].(string)
log.Infof("Recieved registration request from %s", key)
log.Debugf("Registration request body: %+v", request)
data := queue.jsonBody(queue.PodBucket, key, request["status"].(string))
result := queue.put(data)
if request["status"] == "Ready" {
var (
code int
message *api.QueueItem = nil
)
if !queue.Stopped {
code, message = queue.GetQueueItem(request["container"].(string), request["pod"].(string))
result.Code = code
}
if message != nil {
result.Message = *message
} else {
result.Message = ""
}
}
return result
}
// Stop : stops the current queue
func (queue *Queue) Stop() {
queue.Stopped = true
}
// Start : starts the current queue as a background process
func (queue *Queue) Start() {
go queue.perpetual()
}
// GetQueueItem : Get a command to execute
func (queue *Queue) GetQueueItem(container string, pod string) (int, *api.QueueItem) {
serverAddress := queue.Config.AssembleServer()
var key string = container + ":" + pod
log.Infof("Retrieving queue item for %s", key)
req, err := http.NewRequest(http.MethodGet,
serverAddress+"/api/v1/popqueue/"+queue.Pipeline.Name+"/"+key, nil)
if err != nil {
panic(err)
}
req.Header.Set("Accept", "application/json")
code, body := queue.makeRequest(req)
item := api.QueueItem{}
err = json.Unmarshal(body, &item)
if err != nil {
log.Error(err)
return code, nil
}
parentEnv := make([]string, 0)
upstreamContainer := queue.Pipeline.ContainerFromCommandID(item.Command.ID)
if upstreamContainer != nil {
// pipeline first, then override with kubernetes set type environment
parentEnv = append(queue.Pipeline.Environment, upstreamContainer.Environment...)
item.Command.Environment = append(parentEnv, item.Command.Environment...)
} else {
item.Command.Environment = append(queue.Pipeline.Environment, item.Command.Environment...)
}
return code, &item
}
// put : Put data into the bolt store
func (queue *Queue) put(request []byte) *api.Result {
result := api.NewResult()
result.Code = 204
result.Result = "No content"
result.Message = ""
serverAddress := queue.Config.AssembleServer()
req, err := http.NewRequest(
http.MethodPut,
fmt.Sprintf("%s/api/v1/bucket", serverAddress),
bytes.NewBuffer(request))
if err != nil {
log.Fatal(err)
}
req.Header.Set("Content-Type", "application/json; charset=utf-8")
req.Header.Set("Connection", "close")
req.Close = true
code, _ := queue.makeRequest(req)
result.Code = code
return result
}
// makeRequest : Retries a HTTP request up to 5 times
func (queue *Queue) makeRequest(request *http.Request) (int, []byte) {
var (
maxRetries int = 5
retries int = maxRetries
err error
response *http.Response
body []byte
)
for retries > 0 {
response, err = queue.Client.Do(request)
if err == nil {
break
}
retries--
}
if err != nil {
log.Error(err)
return 500, nil
}
defer response.Body.Close()
body, err = ioutil.ReadAll(response.Body)
if err != nil {
log.Error(err)
return response.StatusCode, nil
}
return response.StatusCode, body
}
// jsonBody : construct the jsonBody for writing information into an event bucket
func (queue *Queue) jsonBody(bucket string, key string, value string) []byte {
bucket = filepath.Base(bucket)
values := map[string]string{
"bucket": bucket,
"child": queue.PipelineBucket,
"key": key,
"value": value,
}
jsonValue, _ := json.Marshal(values)
return jsonValue
}
// createBuckets : Create any missing queue buckets for this pipeline
func (queue *Queue) createBuckets() {
buckets := []string{queue.PodBucket, queue.EventsBucket, queue.FilesBucket, queue.QueueBucket}
for _, bucket := range buckets {
content := make(map[string]string)
content["bucket"] = bucket
content["child"] = queue.PipelineBucket
body, err := json.Marshal(content)
if err != nil {
log.Error("Failed to create bucket ", bucket, "/", queue.PipelineBucket, " : ", err)
}
serverAddress := queue.Config.AssembleServer()
request, _ := http.NewRequest(http.MethodPost, serverAddress+"/api/v1/bucket", bytes.NewBuffer(body))
request.Header.Set("Content-Type", "application/json; charset=utf-8")
request.Header.Set("Connection", "close")
request.Close = true
queue.makeRequest(request)
}
}
// perpetual : run the queue every n seconds to move events from the
// event buckets to the queue bucket
// There is a better way than the time.sleep - see thor service
func (queue *Queue) perpetual() {
log.Info("Setting up perpetual queue for ", queue.Pipeline.Name)
var first bool = true
queue.Stopped = false
for {
if queue.Stopped {
break
}
if !first {
time.Sleep(10 * time.Second)
}
first = false
log.Info("Updating queue for ", queue.Pipeline.Name)
content := make(map[string]interface{})
content["pipeline"] = queue.Pipeline.Name
content["maxitems"] = MAXQUEUE
data, _ := json.Marshal(content)
serverAddress := queue.Config.AssembleServer()
request, err := http.NewRequest(
http.MethodPost,
serverAddress+"/api/v1/perpetualqueue",
bytes.NewBuffer(data))
if err != nil {
log.Error(err)
continue
}
request.Header.Set("Content-Type", "application/json; charset=utf-8")
request.Header.Set("Connection", "close")
request.Close = true
response, err := queue.Client.Do(request)
if err != nil {
log.Error(err)
continue
}
if response.StatusCode != http.StatusAccepted {
log.Error("Error during processing queue ", response)
continue
}
response.Body.Close()
}
log.Info("Queue terminated ", queue.Pipeline.Name)
queue.Stopped = true
}