-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathcloudtasksappengine.go
171 lines (146 loc) · 4.31 KB
/
cloudtasksappengine.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
package appwrap
import (
"fmt"
"net/http"
"time"
taskspb "google.golang.org/genproto/googleapis/cloud/tasks/v2"
timestamp "google.golang.org/protobuf/types/known/timestamppb"
)
type cloudTaskAppEngineImpl struct {
task *taskspb.Task
}
func newAppEngineCloudTask() AppEngineTask {
return &cloudTaskAppEngineImpl{
task: &taskspb.Task{
MessageType: &taskspb.Task_AppEngineHttpRequest{
AppEngineHttpRequest: &taskspb.AppEngineHttpRequest{
AppEngineRouting: &taskspb.AppEngineRouting{},
},
},
},
}
}
func (t cloudTaskAppEngineImpl) Copy() CloudTask {
innerCopy := *t.task.GetAppEngineHttpRequest()
bodyCopy := make([]byte, len(innerCopy.Body))
copy(bodyCopy, innerCopy.Body)
headerCopy := make(map[string]string, len(innerCopy.Headers))
for k, v := range innerCopy.Headers {
headerCopy[k] = v
}
taskCopy := &cloudTaskAppEngineImpl{
task: &taskspb.Task{
MessageType: &taskspb.Task_AppEngineHttpRequest{
AppEngineHttpRequest: &taskspb.AppEngineHttpRequest{
AppEngineRouting: &taskspb.AppEngineRouting{
Service: innerCopy.AppEngineRouting.Service,
Version: innerCopy.AppEngineRouting.Version,
Instance: innerCopy.AppEngineRouting.Instance,
Host: innerCopy.AppEngineRouting.Host,
},
HttpMethod: innerCopy.HttpMethod,
Headers: headerCopy,
Body: bodyCopy,
RelativeUri: innerCopy.RelativeUri,
},
},
},
}
return taskCopy
}
func (t *cloudTaskAppEngineImpl) isTask() {}
func (t *cloudTaskAppEngineImpl) Delay() (delay time.Duration) {
if sched := t.task.ScheduleTime; sched == nil {
} else {
delay = time.Unix(sched.Seconds, int64(sched.Nanos)).Sub(time.Now())
}
if delay < 0 {
return time.Duration(0)
}
return
}
func (t *cloudTaskAppEngineImpl) SetDelay(delay time.Duration) {
eta := time.Now().Add(delay)
t.SetEta(eta)
}
func (t *cloudTaskAppEngineImpl) SetEta(eta time.Time) {
t.task.ScheduleTime = ×tamp.Timestamp{
Seconds: eta.Unix(),
Nanos: int32(eta.Nanosecond()),
}
}
func (t *cloudTaskAppEngineImpl) Name() string {
return t.task.Name
}
func (t *cloudTaskAppEngineImpl) SetName(name string) {
t.task.Name = name
}
func (t *cloudTaskAppEngineImpl) RetryCount() int32 {
return t.task.DispatchCount
}
func (t *cloudTaskAppEngineImpl) SetRetryCount(count int32) {
t.task.DispatchCount = count
}
func (t *cloudTaskAppEngineImpl) Header() http.Header {
req := t.task.GetAppEngineHttpRequest()
header := make(http.Header, len(req.Headers))
if req.Headers == nil {
return nil
}
for key, value := range req.Headers {
header[key] = []string{value}
}
return header
}
func (t *cloudTaskAppEngineImpl) SetHeader(header http.Header) {
req := t.task.GetAppEngineHttpRequest()
reqHeader := make(map[string]string, len(header))
for key, value := range header {
reqHeader[key] = value[0]
}
req.Headers = reqHeader
}
func (t *cloudTaskAppEngineImpl) Method() string {
req := t.task.GetAppEngineHttpRequest()
return taskspb.HttpMethod_name[int32(req.HttpMethod)]
}
func (t *cloudTaskAppEngineImpl) SetMethod(method string) {
if val := taskspb.HttpMethod_value[method]; val != 0 {
req := t.task.GetAppEngineHttpRequest()
req.HttpMethod = taskspb.HttpMethod(val)
} else {
panic(fmt.Sprintf("invalid task method: %s", method))
}
}
func (t *cloudTaskAppEngineImpl) Path() (path string) {
req := t.task.GetAppEngineHttpRequest()
return req.RelativeUri
}
func (t *cloudTaskAppEngineImpl) SetPath(path string) {
req := t.task.GetAppEngineHttpRequest()
req.RelativeUri = path
}
func (t *cloudTaskAppEngineImpl) Payload() []byte {
req := t.task.GetAppEngineHttpRequest()
return req.Body
}
func (t *cloudTaskAppEngineImpl) SetPayload(payload []byte) {
req := t.task.GetAppEngineHttpRequest()
req.Body = payload
}
func (t *cloudTaskAppEngineImpl) Service() (service string) {
routing := t.task.GetAppEngineHttpRequest().GetAppEngineRouting()
return routing.Service
}
func (t *cloudTaskAppEngineImpl) SetService(service string) {
routing := t.task.GetAppEngineHttpRequest().GetAppEngineRouting()
routing.Service = service
}
func (t *cloudTaskAppEngineImpl) Version() string {
routing := t.task.GetAppEngineHttpRequest().GetAppEngineRouting()
return routing.Version
}
func (t *cloudTaskAppEngineImpl) SetVersion(version string) {
routing := t.task.GetAppEngineHttpRequest().GetAppEngineRouting()
routing.Version = version
}