forked from aertje/cloud-tasks-emulator
-
Notifications
You must be signed in to change notification settings - Fork 0
/
task.go
411 lines (323 loc) · 11.4 KB
/
task.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
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
package main
import (
"bytes"
"fmt"
"log"
"math/rand"
"net/http"
"net/url"
"os"
"regexp"
"strconv"
"sync"
"time"
"github.com/golang/protobuf/proto"
"github.com/golang/protobuf/ptypes"
pduration "github.com/golang/protobuf/ptypes/duration"
ptimestamp "github.com/golang/protobuf/ptypes/timestamp"
tasks "google.golang.org/genproto/googleapis/cloud/tasks/v2"
rpcstatus "google.golang.org/genproto/googleapis/rpc/status"
)
var r *regexp.Regexp
func init() {
// Format requirements as per https://cloud.google.com/tasks/docs/reference/rest/v2/projects.locations.queues.tasks#Task.FIELDS.name
r = regexp.MustCompile("projects/([a-zA-Z0-9:.-]+)/locations/([a-zA-Z0-9-]+)/queues/([a-zA-Z0-9-]+)/tasks/([a-zA-Z0-9_-]+)")
}
func parseTaskName(task *tasks.Task) TaskNameParts {
matches := r.FindStringSubmatch(task.GetName())
return TaskNameParts{
project: matches[1],
location: matches[2],
queueId: matches[3],
taskId: matches[4],
}
}
func isValidTaskName(name string) bool {
return r.MatchString(name)
}
type TaskNameParts struct {
project string
location string
queueId string
taskId string
}
// Task holds all internals for a task
type Task struct {
queue *Queue
state *tasks.Task
cancel chan bool
onDone func(*Task)
stateMutex sync.Mutex
cancelOnce sync.Once
}
// NewTask creates a new task for the specified queue
func NewTask(queue *Queue, taskState *tasks.Task, onDone func(task *Task)) *Task {
setInitialTaskState(taskState, queue.name)
task := &Task{
queue: queue,
state: taskState,
onDone: onDone,
cancel: make(chan bool, 1), // Buffered in case cancel comes when task is not scheduled
}
return task
}
func setInitialTaskState(taskState *tasks.Task, queueName string) {
if taskState.GetName() == "" {
taskID := strconv.FormatUint(uint64(rand.Uint64()), 10)
taskState.Name = queueName + "/tasks/" + taskID
}
taskState.CreateTime = ptypes.TimestampNow()
// For some reason the cloud does not set nanos
taskState.CreateTime.Nanos = 0
if taskState.GetScheduleTime() == nil {
taskState.ScheduleTime = ptypes.TimestampNow()
}
if taskState.GetDispatchDeadline() == nil {
taskState.DispatchDeadline = &pduration.Duration{Seconds: 600}
}
// This should probably be set somewhere else?
taskState.View = tasks.Task_BASIC
httpRequest := taskState.GetHttpRequest()
if httpRequest != nil {
if httpRequest.GetHttpMethod() == tasks.HttpMethod_HTTP_METHOD_UNSPECIFIED {
httpRequest.HttpMethod = tasks.HttpMethod_POST
}
if httpRequest.GetHeaders() == nil {
httpRequest.Headers = make(map[string]string)
}
// Override
httpRequest.Headers["User-Agent"] = "Google-Cloud-Tasks"
}
appEngineHTTPRequest := taskState.GetAppEngineHttpRequest()
if appEngineHTTPRequest != nil {
if appEngineHTTPRequest.GetHttpMethod() == tasks.HttpMethod_HTTP_METHOD_UNSPECIFIED {
appEngineHTTPRequest.HttpMethod = tasks.HttpMethod_POST
}
if appEngineHTTPRequest.GetHeaders() == nil {
appEngineHTTPRequest.Headers = make(map[string]string)
}
appEngineHTTPRequest.Headers["User-Agent"] = "AppEngine-Google; (+http://code.google.com/appengine)"
if appEngineHTTPRequest.GetBody() != nil {
if _, ok := appEngineHTTPRequest.GetHeaders()["Content-Type"]; !ok {
appEngineHTTPRequest.Headers["Content-Type"] = "application/octet-stream"
}
}
if appEngineHTTPRequest.GetAppEngineRouting() == nil {
appEngineHTTPRequest.AppEngineRouting = &tasks.AppEngineRouting{}
}
if appEngineHTTPRequest.GetAppEngineRouting().Host == "" {
var host, domainSeparator string
emulatorHost := os.Getenv("APP_ENGINE_EMULATOR_HOST")
if emulatorHost == "" {
// TODO: the new route format for appengine is <PROJECT_ID>.<REGION_ID>.r.appspot.com
// TODO: support custom domains
// https://cloud.google.com/appengine/docs/standard/python/how-requests-are-routed
host = "https://" + parseTaskName(taskState).project + ".appspot.com"
domainSeparator = "-dot-"
} else {
host = emulatorHost
domainSeparator = "."
}
hostURL, err := url.Parse(host)
if err != nil {
panic(err)
}
if appEngineHTTPRequest.GetAppEngineRouting().GetService() != "" {
hostURL.Host = appEngineHTTPRequest.GetAppEngineRouting().GetService() + domainSeparator + hostURL.Host
}
if appEngineHTTPRequest.GetAppEngineRouting().GetVersion() != "" {
hostURL.Host = appEngineHTTPRequest.GetAppEngineRouting().GetVersion() + domainSeparator + hostURL.Host
}
if appEngineHTTPRequest.GetAppEngineRouting().GetInstance() != "" {
hostURL.Host = appEngineHTTPRequest.GetAppEngineRouting().GetInstance() + domainSeparator + hostURL.Host
}
appEngineHTTPRequest.GetAppEngineRouting().Host = hostURL.String()
}
if appEngineHTTPRequest.GetRelativeUri() == "" {
appEngineHTTPRequest.RelativeUri = "/"
}
}
}
func updateStateForReschedule(task *Task) *tasks.Task {
// The lock is to ensure a consistent state when updating
task.stateMutex.Lock()
taskState := task.state
queueState := task.queue.state
retryConfig := queueState.GetRetryConfig()
minBackoff, _ := ptypes.Duration(retryConfig.GetMinBackoff())
maxBackoff, _ := ptypes.Duration(retryConfig.GetMaxBackoff())
doubling := taskState.GetDispatchCount() - 1
if doubling > retryConfig.MaxDoublings {
doubling = retryConfig.MaxDoublings
}
backoff := minBackoff * time.Duration(1<<uint32(doubling))
if backoff > maxBackoff {
backoff = maxBackoff
}
protoBackoff := ptypes.DurationProto(backoff)
prevScheduleTime := taskState.GetScheduleTime()
// Avoid int32 nanos overflow
scheduleNanos := int64(prevScheduleTime.GetNanos()) + int64(protoBackoff.GetNanos())
scheduleSeconds := prevScheduleTime.GetSeconds() + protoBackoff.GetSeconds()
if scheduleNanos >= 1e9 {
scheduleSeconds++
scheduleNanos -= 1e9
}
taskState.ScheduleTime = &ptimestamp.Timestamp{
Nanos: int32(scheduleNanos),
Seconds: scheduleSeconds,
}
frozenTaskState := proto.Clone(taskState).(*tasks.Task)
task.stateMutex.Unlock()
return frozenTaskState
}
func updateStateForDispatch(task *Task) *tasks.Task {
task.stateMutex.Lock()
taskState := task.state
dispatchTime := ptypes.TimestampNow()
taskState.LastAttempt = &tasks.Attempt{
ScheduleTime: &ptimestamp.Timestamp{
Nanos: taskState.GetScheduleTime().GetNanos(),
Seconds: taskState.GetScheduleTime().GetSeconds(),
},
DispatchTime: dispatchTime,
}
taskState.DispatchCount++
if taskState.GetFirstAttempt() == nil {
taskState.FirstAttempt = &tasks.Attempt{
DispatchTime: dispatchTime,
}
}
frozenTaskState := proto.Clone(taskState).(*tasks.Task)
task.stateMutex.Unlock()
return frozenTaskState
}
func updateStateAfterDispatch(task *Task, statusCode int) *tasks.Task {
task.stateMutex.Lock()
taskState := task.state
rpcCode := toRPCStatusCode(statusCode)
rpcCodeName := toCodeName(rpcCode)
lastAttempt := taskState.GetLastAttempt()
lastAttempt.ResponseTime = ptypes.TimestampNow()
lastAttempt.ResponseStatus = &rpcstatus.Status{
Code: rpcCode,
Message: fmt.Sprintf("%s(%d): HTTP status code %d", rpcCodeName, rpcCode, statusCode),
}
taskState.ResponseCount++
frozenTaskState := proto.Clone(taskState).(*tasks.Task)
task.stateMutex.Unlock()
return frozenTaskState
}
func (task *Task) reschedule(retry bool, statusCode int) {
if statusCode >= 200 && statusCode <= 299 {
log.Println("Task done")
task.onDone(task)
} else {
log.Println("Task exec error with status " + strconv.Itoa(statusCode))
if retry {
retryConfig := task.queue.state.GetRetryConfig()
if task.state.DispatchCount >= retryConfig.GetMaxAttempts() {
log.Println("Ran out of attempts")
} else {
updateStateForReschedule(task)
task.Schedule()
}
}
}
}
func dispatch(retry bool, taskState *tasks.Task) int {
client := &http.Client{}
client.Timeout, _ = ptypes.Duration(taskState.GetDispatchDeadline())
var req *http.Request
var headers map[string]string
httpRequest := taskState.GetHttpRequest()
appEngineHTTPRequest := taskState.GetAppEngineHttpRequest()
scheduled, _ := ptypes.Timestamp(taskState.GetScheduleTime())
nameParts := parseTaskName(taskState)
headerQueueName := nameParts.queueId
headerTaskName := nameParts.taskId
headerTaskRetryCount := fmt.Sprintf("%v", taskState.GetDispatchCount()-1)
headerTaskExecutionCount := fmt.Sprintf("%v", taskState.GetResponseCount())
headerTaskETA := fmt.Sprintf("%f", float64(scheduled.UnixNano())/1e9)
if httpRequest != nil {
method := toHTTPMethod(httpRequest.GetHttpMethod())
req, _ = http.NewRequest(method, httpRequest.GetUrl(), bytes.NewBuffer(httpRequest.GetBody()))
headers = httpRequest.GetHeaders()
if auth := httpRequest.GetOidcToken(); auth != nil {
tokenStr := createOIDCToken(auth.ServiceAccountEmail, httpRequest.GetUrl())
headers["Authorization"] = "Bearer " + tokenStr
}
// Headers as per https://cloud.google.com/tasks/docs/creating-http-target-tasks#handler
// TODO: optional headers
headers["X-CloudTasks-QueueName"] = headerQueueName
headers["X-CloudTasks-TaskName"] = headerTaskName
headers["X-CloudTasks-TaskExecutionCount"] = headerTaskExecutionCount
headers["X-CloudTasks-TaskRetryCount"] = headerTaskRetryCount
headers["X-CloudTasks-TaskETA"] = headerTaskETA
} else if appEngineHTTPRequest != nil {
method := toHTTPMethod(appEngineHTTPRequest.GetHttpMethod())
host := appEngineHTTPRequest.GetAppEngineRouting().GetHost()
url := host + appEngineHTTPRequest.GetRelativeUri()
req, _ = http.NewRequest(method, url, bytes.NewBuffer(appEngineHTTPRequest.GetBody()))
headers = appEngineHTTPRequest.GetHeaders()
// These headers are only set on dispatch, see https://cloud.google.com/tasks/docs/reference/rpc/google.cloud.tasks.v2#google.cloud.tasks.v2.AppEngineHttpRequest
// TODO: optional headers
headers["X-AppEngine-QueueName"] = headerQueueName
headers["X-AppEngine-TaskName"] = headerTaskName
headers["X-AppEngine-TaskRetryCount"] = headerTaskRetryCount
headers["X-AppEngine-TaskExecutionCount"] = headerTaskExecutionCount
headers["X-AppEngine-TaskETA"] = headerTaskETA
}
for k, v := range headers {
// Uses a direct set to maintain capitalization
// TODO: figure out a way to test these, as the Go net/http client lib overrides the incoming header capitalization
req.Header[k] = []string{v}
}
resp, err := client.Do(req)
if err != nil {
fmt.Fprintf(os.Stderr, "%v\n", err)
return -1
}
defer resp.Body.Close()
return resp.StatusCode
}
func (task *Task) doDispatch(retry bool) {
respCode := dispatch(retry, task.state)
updateStateAfterDispatch(task, respCode)
task.reschedule(retry, respCode)
}
// Attempt tries to execute a task
func (task *Task) Attempt() {
updateStateForDispatch(task)
task.doDispatch(true)
}
// Run runs the task outside of the normal queueing mechanism.
// This method is called directly by request.
func (task *Task) Run() *tasks.Task {
taskState := updateStateForDispatch(task)
go task.doDispatch(false)
return taskState
}
// Delete cancels the task if it is queued for execution.
// This method is called directly by request.
func (task *Task) Delete() {
task.cancelOnce.Do(func() {
task.cancel <- true
})
}
// Schedule schedules the task for execution.
// It is initially called by the queue, later by the task reschedule.
func (task *Task) Schedule() {
scheduled, _ := ptypes.Timestamp(task.state.GetScheduleTime())
fromNow := time.Until(scheduled)
go func() {
select {
case <-time.After(fromNow):
task.queue.fire <- task
return
case <-task.cancel:
task.onDone(task)
return
}
}()
}