forked from NdoleStudio/httpsms
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgoogle_cloud_push_queue_service.go
More file actions
98 lines (83 loc) · 2.58 KB
/
google_cloud_push_queue_service.go
File metadata and controls
98 lines (83 loc) · 2.58 KB
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
package services
import (
"context"
"fmt"
"net/http"
"time"
cloudtasks "cloud.google.com/go/cloudtasks/apiv2"
"github.com/NdoleStudio/httpsms/pkg/telemetry"
"github.com/palantir/stacktrace"
taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2"
"google.golang.org/protobuf/types/known/timestamppb"
)
type googlePushQueue struct {
queueConfig PushQueueConfig
logger telemetry.Logger
tracer telemetry.Tracer
client *cloudtasks.Client
}
// NewGooglePushQueue creates a new googlePushQueue
func NewGooglePushQueue(
logger telemetry.Logger,
tracer telemetry.Tracer,
client *cloudtasks.Client,
queueConfig PushQueueConfig,
) PushQueue {
return &googlePushQueue{
client: client,
tracer: tracer,
logger: logger,
queueConfig: queueConfig,
}
}
// Enqueue a task to the queue
func (queue *googlePushQueue) Enqueue(ctx context.Context, task *PushQueueTask, timeout time.Duration) error {
ctx, span := queue.tracer.Start(ctx)
defer span.End()
ctxLogger := queue.tracer.CtxLogger(queue.logger, span)
headers := map[string]string{"Content-Type": "application/json"}
for key, value := range task.Headers {
headers[key] = value
}
// Build the Task payload.
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#CreateTaskRequest
req := &taskspb.CreateTaskRequest{
Parent: queue.queueConfig.Name,
Task: &taskspb.Task{
// https://godoc.org/google.golang.org/genproto/googleapis/cloud/tasks/v2#HttpRequest
MessageType: &taskspb.Task_HttpRequest{
HttpRequest: &taskspb.HttpRequest{
Headers: headers,
HttpMethod: queue.httpMethodToProtoHTTPMethod(task.Method),
Url: task.URL,
},
},
ScheduleTime: timestamppb.New(time.Now().UTC().Add(timeout)),
},
}
// Add a payload message if one is present.
req.Task.GetHttpRequest().Body = task.Body
queueTask, err := queue.client.CreateTask(ctx, req)
if err != nil {
msg := fmt.Sprintf("cannot schedule task %s to URL: %s", string(task.Body), task.URL)
return queue.tracer.WrapErrorSpan(span, stacktrace.Propagate(err, msg))
}
ctxLogger.Info(fmt.Sprintf(
"item added to [%s] queue with id [%s] and schedule [%s]",
queue.queueConfig.Name,
queueTask.Name,
queueTask.ScheduleTime,
))
return nil
}
func (queue *googlePushQueue) httpMethodToProtoHTTPMethod(httpMethod string) taskspb.HttpMethod {
method, ok := map[string]taskspb.HttpMethod{
http.MethodGet: taskspb.HttpMethod_GET,
http.MethodPost: taskspb.HttpMethod_POST,
http.MethodPut: taskspb.HttpMethod_PUT,
}[httpMethod]
if !ok {
return taskspb.HttpMethod_POST
}
return method
}