-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathqueue.go
344 lines (304 loc) · 9.76 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
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
package cloudtasks
import (
"context"
"encoding/json"
"fmt"
"io"
"log/slog"
"net/http"
"net/url"
"os"
"path"
"strconv"
"strings"
"time"
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
pb "cloud.google.com/go/cloudtasks/apiv2/cloudtaskspb"
"cloud.google.com/go/compute/metadata"
"github.com/VictoriaMetrics/metrics"
"github.com/altipla-consulting/errors"
"google.golang.org/api/idtoken"
)
var (
allQueues []*gcloudQueue
)
// Queue abstract any remote or local system that can execute a task.
type Queue interface {
// Send a new task to the queue.
Send(ctx context.Context, task *Task) error
// SendExternal sends a new task to an external URL.
SendExternal(ctx context.Context, task *ExternalTask) error
}
// QueueOption configures queues when creating them.
type QueueOption func(*gcloudQueue)
// WithRegion configures a custom region for the queue. By default it will use the region of the Cloud Run service.
func WithRegion(region string) QueueOption {
return func(queue *gcloudQueue) {
queue.region = region
}
}
// WithHostname configures the queue for an application outside of Cloud Run. Pass only the hostname, without the
// protocol or trailing slash.
func WithHostname(hostname string) QueueOption {
return func(queue *gcloudQueue) {
queue.audience = fmt.Sprintf("https://%s", hostname)
}
}
type gcloudQueue struct {
name string
region string
audience string
initErr error
client *cloudtasks.Client
project, numericProject string
serviceAccountEmail string
}
// NewQueue initializes a new queue.
func NewQueue(name string, opts ...QueueOption) Queue {
// Local deployments won't have this environment variable defined.
if os.Getenv("K_SERVICE") == "" {
return &localQueue{name: name}
}
queue := &gcloudQueue{name: name}
allQueues = append(allQueues, queue)
for _, opt := range opts {
opt(queue)
}
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Second)
defer cancel()
var err error
queue.client, err = cloudtasks.NewClient(ctx)
if err != nil {
queue.initErr = fmt.Errorf("cloudtasks: cannot initialize remote client: %w", err)
return queue
}
queue.project, err = metadata.ProjectIDWithContext(ctx)
if err != nil {
queue.initErr = fmt.Errorf("cloudtasks: cannot get google project name: %w", err)
return queue
}
if queue.region == "" {
// Detect Cloud Run to read the region directly or extract it from the zone in other environments.
if os.Getenv("K_CONFIGURATION") != "" || os.Getenv("CLOUD_RUN_JOB") != "" {
region, err := metadata.GetWithContext(ctx, "instance/region")
if err != nil {
queue.initErr = fmt.Errorf("cloudtasks: cannot get google cloud run region: %w", err)
return queue
}
queue.region = path.Base(region)
} else {
zone, err := metadata.ZoneWithContext(ctx)
if err != nil {
queue.initErr = fmt.Errorf("cloudtasks: cannot get google zone: %w", err)
return queue
}
zone = path.Base(zone)
queue.region = zone[:strings.LastIndex(zone, "-")]
}
}
if queue.audience == "" {
queue.numericProject, err = metadata.NumericProjectIDWithContext(ctx)
if err != nil {
queue.initErr = fmt.Errorf("cloudtasks: cannot get google numeric project: %w", err)
return queue
}
queue.audience = fmt.Sprintf("https://%s-%s.%s.run.app", os.Getenv("K_SERVICE"), queue.numericProject, queue.region)
}
queue.serviceAccountEmail, err = metadata.EmailWithContext(ctx, "default")
if err != nil {
queue.initErr = fmt.Errorf("cloudtasks: cannot get default service account email: %w", err)
return queue
}
return queue
}
func (queue *gcloudQueue) Send(ctx context.Context, task *Task) error {
parent := strings.Join([]string{"projects", queue.project, "locations", queue.region, "queues", queue.name}, "/")
req := &pb.CreateTaskRequest{
Parent: parent,
Task: &pb.Task{
Name: generateTaskName(parent, task.name),
MessageType: &pb.Task_HttpRequest{
HttpRequest: &pb.HttpRequest{
HttpMethod: pb.HttpMethod_POST,
Url: fmt.Sprintf("%s/_cloudtasks/%s", queue.audience, queue.name),
Body: task.payload,
Headers: map[string]string{
"Content-Type": "application/json",
"Altipla-Task": task.key,
},
AuthorizationHeader: &pb.HttpRequest_OidcToken{
OidcToken: &pb.OidcToken{
ServiceAccountEmail: queue.serviceAccountEmail,
Audience: queue.audience,
},
},
},
},
},
}
var lastErr error
for i := 0; i < 3 && ctx.Err() == nil; i++ {
if err := queue.createTask(ctx, req); err != nil {
lastErr = fmt.Errorf("%w: %w", ErrCannotSendTask, err)
time.Sleep(1 * time.Second)
continue
}
break
}
if lastErr != nil {
return lastErr
}
metrics.GetOrCreateCounter(fmt.Sprintf("cloudtasks_sent_total{queue=%q,task=%q}", queue.name, task.key)).Inc()
return nil
}
func (queue *gcloudQueue) SendExternal(ctx context.Context, task *ExternalTask) error {
u, err := url.Parse(task.URL)
if err != nil {
return fmt.Errorf("cloudtasks: cannot parse external task URL: %w", err)
}
payload, err := json.Marshal(task.Payload)
if err != nil {
return fmt.Errorf("cloudtasks: cannot marshal task payload %T: %w", payload, err)
}
parent := strings.Join([]string{"projects", queue.project, "locations", queue.region, "queues", queue.name}, "/")
req := &pb.CreateTaskRequest{
Parent: parent,
Task: &pb.Task{
Name: generateTaskName(parent, task.name),
MessageType: &pb.Task_HttpRequest{
HttpRequest: &pb.HttpRequest{
HttpMethod: pb.HttpMethod_POST,
Url: task.URL,
Body: payload,
Headers: map[string]string{"Content-Type": "application/json"},
AuthorizationHeader: &pb.HttpRequest_OidcToken{
OidcToken: &pb.OidcToken{
ServiceAccountEmail: queue.serviceAccountEmail,
Audience: fmt.Sprintf("https://%s/", u.Hostname()),
},
},
},
},
},
}
var lastErr error
for i := 0; i < 3 && ctx.Err() == nil; i++ {
if err := queue.createTask(ctx, req); err != nil {
lastErr = fmt.Errorf("%w: %w", ErrCannotSendTask, err)
time.Sleep(1 * time.Second)
continue
}
break
}
if lastErr != nil {
return lastErr
}
return nil
}
func (queue *gcloudQueue) createTask(ctx context.Context, req *pb.CreateTaskRequest) error {
// Cloud Tasks enforces a timeout of less than 30 seconds server side. This will
// ensure all our calls are less than the limit to avoid the InvalidArgument error.
ctx, cancel := context.WithTimeout(ctx, 25*time.Second)
defer cancel()
_, err := queue.client.CreateTask(ctx, req)
return err
}
func (queue *gcloudQueue) taskHandler(w http.ResponseWriter, r *http.Request) error {
bearer := extractBearer(r.Header.Get("Authorization"))
if bearer == "" {
http.Error(w, fmt.Sprintf("cloudtasks: bad token format %q", r.Header.Get("Authorization")), http.StatusUnauthorized)
return nil
}
jwt, err := idtoken.Validate(r.Context(), bearer, queue.audience)
if err != nil {
http.Error(w, fmt.Sprintf("cloudtasks: bad token %q: %s", bearer, err), http.StatusUnauthorized)
return nil
}
email, _ := jwt.Claims["email"].(string)
if email != queue.serviceAccountEmail {
return errors.Errorf("cloudtasks: unexpected email %q in token %q", email, bearer)
}
key := r.Header.Get("Altipla-Task")
if key == "" {
key = r.Header.Get("X-Altipla-Task")
}
if key == "" {
return errors.Errorf("cloudtasks: missing task key")
}
payload, err := io.ReadAll(r.Body)
if err != nil {
return errors.Trace(err)
}
retries, err := strconv.ParseInt(r.Header.Get("X-CloudTasks-TaskRetryCount"), 10, 64)
if err != nil {
return errors.Trace(err)
}
task := &Task{
key: key,
name: r.Header.Get("X-CloudTasks-TaskName"),
payload: payload,
Retries: retries,
}
metrics.GetOrCreateCounter(fmt.Sprintf("cloudtasks_received_total{queue=%q,task=%q}", queue.name, task.key)).Inc()
start := time.Now()
if err := safeCall(r.Context(), key, task); err != nil {
slog.Error("cloudtasks: task failed",
slog.String("task", task.key),
slog.String("queue", queue.name),
slog.String("error", err.Error()),
slog.String("details", errors.Details(err)),
slog.Int64("retries", task.Retries),
)
metrics.GetOrCreateCounter(fmt.Sprintf("cloudtasks_failed_total{queue=%q,task=%q}", queue.name, task.key)).Inc()
return errors.Trace(err)
}
slog.Debug("cloudtasks: task completed",
slog.String("task", task.name),
slog.String("queue", queue.name),
slog.String("function", key),
slog.Int64("retries", task.Retries),
)
metrics.GetOrCreateCounter(fmt.Sprintf("cloudtasks_success_total{queue=%q,task=%q}", queue.name, task.key)).Inc()
metrics.GetOrCreateHistogram(fmt.Sprintf("cloudtasks_duration_seconds{queue=%q,task=%q}", queue.name, task.key)).UpdateDuration(start)
return nil
}
func extractBearer(authorization string) string {
parts := strings.SplitN(authorization, " ", 2)
if len(parts) != 2 || parts[0] != "Bearer" {
return ""
}
return parts[1]
}
func safeCall(ctx context.Context, key string, task *Task) (err error) {
defer func() {
if r := errors.Recover(recover()); r != nil {
err = r
}
}()
return funcs[key].fn(ctx, task)
}
type localQueue struct {
name string
}
func (queue *localQueue) Send(ctx context.Context, task *Task) error {
go func() {
if err := funcs[task.key].fn(context.Background(), task); err != nil {
slog.Error("cloudtasks: failed to execute simulated task",
slog.String("err", err.Error()),
slog.String("task", task.key),
)
fmt.Println(errors.Stack(err))
return
}
slog.Info("cloudtasks: task simulation completed",
slog.String("task", task.name),
slog.String("queue", queue.name),
slog.String("function", task.key),
)
}()
return nil
}
func (queue *localQueue) SendExternal(ctx context.Context, task *ExternalTask) error {
slog.DebugContext(ctx, "cloudtasks: simulated external task", "task", task)
return nil
}