forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
388 lines (361 loc) · 10.6 KB
/
message.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
package gocelery
import (
"encoding/base64"
"fmt"
"github.com/Danceiny/go.fastjson"
"log"
"reflect"
"sync"
"time"
)
// GLOBAL 替换掉 encoding/json
var (
json = fastjson.FastJson
ISO8601 = "2006-01-02T15:04:05"
)
// CeleryMessage is actual message to be sent to Redis
// 参考:http://docs.celeryproject.org/en/latest/internals/protocol.html#definition
// example:
/*
{
"body": "W1tdLCB7InkiOiAyODc4LCAieCI6IDU0NTZ9LCB7ImNob3JkIjogbnVsbCwgImNhbGxiYWNrcyI6IG51bGwsICJlcnJiYWNrcyI6IG51bGwsICJjaGFpbiI6IG51bGx9XQ==",
"headers": {
"origin": "gen66194@DanceinydeMacBook-Pro.local",
"root_id": "25abb5e6-d8c3-4b20-8dfb-7dc1be9ecf8f",
"expires": null,
"shadow": null,
"id": "25abb5e6-d8c3-4b20-8dfb-7dc1be9ecf8f",
"kwargsrepr": "{'y': 2878, 'x': 5456}",
"lang": "py",
"retries": 0,
"task": "worker.add_reflect",
"group": null,
"timelimit": [null, null],
"parent_id": null,
"argsrepr": "()",
"eta": null
},
"properties": {
"priority": 0,
"body_encoding": "base64",
"correlation_id": "25abb5e6-d8c3-4b20-8dfb-7dc1be9ecf8f",
"reply_to": "2f6f7ea8-dcc3-30a7-ae0c-4eb03ae4910c",
"delivery_info": {
"routing_key": "celery",
"exchange": ""
},
"delivery_mode": 2,
"delivery_tag": "a18604c0-5422-4592-877b-72e106744981"
},
"content-type": "application/json",
"content-encoding": "utf-8"
}
*/
type CeleryMessage struct {
// body是语言相关的,比如可以使用thrift
// object[] args,
// Mapping kwargs,
// Mapping embed {
// 'callbacks': Signature[] callbacks,
// 'errbacks': Signature[] errbacks,
// 'chain': Signature[] chain,
// 'chord': Signature chord_callback,
// }
Body string `json:"body"`
Headers ST_Headers `json:"headers"`
Properties ST_Properties `json:"properties"`
ContentType string `json:"content-type"`
ContentEncoding string `json:"content-encoding"`
}
/*
"properties": {
"priority": 0,
"body_encoding": "base64",
"correlation_id": "25abb5e6-d8c3-4b20-8dfb-7dc1be9ecf8f",
"reply_to": "2f6f7ea8-dcc3-30a7-ae0c-4eb03ae4910c",
"delivery_info": {
"routing_key": "celery",
"exchange": ""
},
"delivery_mode": 2,
"delivery_tag": "a18604c0-5422-4592-877b-72e106744981"
},
*/
type ST_Properties struct {
// ContentEncoding string `json:"content_encoding"` // 事实上该字段移动到与properties并列的层级了
// ContentType string `json:"content_type"` // 事实上该字段移动到与properties并列的层级了
CorrelationID string `json:"correlation_id"`
ReplyTo string `json:"replay_to"`
// 下面的在Celery文档中未曾提及
BodyEncoding string `json:"body_encoding"`
Priority int `json:"priority"`
DeliveryInfo CeleryDeliveryInfo `json:"delivery_info"`
DeliveryMode int `json:"delivery_mode"`
DeliveryTag string `json:"delivery_tag"`
CorrelationId string `json:"correlation_id"`
}
/*
真实json示例:
"headers": {
"origin": "gen66194@DanceinydeMacBook-Pro.local",
"root_id": "25abb5e6-d8c3-4b20-8dfb-7dc1be9ecf8f",
"expires": null,
"shadow": null,
"id": "25abb5e6-d8c3-4b20-8dfb-7dc1be9ecf8f",
"kwargsrepr": "{'y': 2878, 'x': 5456}",
"lang": "py",
"retries": 0,
"task": "worker.add_reflect",
"group": null,
"timelimit": [null, null],
"parent_id": null,
"argsrepr": "()",
"eta": null
},
*/
type ST_Headers struct {
Lang string `json:"lang"`
Task string `json:"task"` // task name
TaskId string `json:"id"` // uuid
RootId string `json:"root_id"` // uuid
ParentId string `json:"parent_id"` // uuid
Group string `json:"group"` // uuid group_id
Retries int `json:"retries"`
ETA time.Time `json:"eta" time_format:"2006-01-02T15:04:05"`
Expires time.Time `json:"expires" time_format:"2006-01-02T15:04:05"`
Origin string `json:"origin"` // optional
Shadow string `json:"shadow"` // alias_name, optional
ArgsRepr string `json:"argsrepr"`
KwargsRepr string `json:"kwargsrepr"`
TimeLimit [2]string `json:"timelimit"`
/*TODO
'meth': string method_name,
*/
}
func (cm *CeleryMessage) reset() {
cm.Headers = ST_Headers{}
cm.Body = ""
cm.Properties.CorrelationID = generateUUID()
cm.Properties.ReplyTo = generateUUID()
cm.Properties.DeliveryTag = generateUUID()
}
// TODO: support customized delivery_info property
var celeryMessagePool = sync.Pool{
New: func() interface{} {
return &CeleryMessage{
Body: "",
Headers: ST_Headers{
Lang: "golang",
},
ContentType: "application/json",
ContentEncoding: "utf-8",
Properties: ST_Properties{
BodyEncoding: "base64",
// optional
ReplyTo: generateUUID(),
// DeliveryMode: 2,
// DeliveryTag: generateUUID(),
},
}
},
}
func getDefaultCeleryDeliveryInfo() *CeleryDeliveryInfo {
return &CeleryDeliveryInfo{
RoutingKey: "celery",
Exchange: "",
}
}
func NewCeleryDeliveryInfo(routingKey string, exchange string) *CeleryDeliveryInfo {
return &CeleryDeliveryInfo{
RoutingKey: routingKey,
Exchange: exchange,
}
}
func getCeleryMessage(encodedTaskMessage string, deliveryInfo *CeleryDeliveryInfo) *CeleryMessage {
msg := celeryMessagePool.Get().(*CeleryMessage)
msg.Body = encodedTaskMessage
if deliveryInfo == nil {
deliveryInfo = getDefaultCeleryDeliveryInfo()
}
msg.Properties.DeliveryInfo = *deliveryInfo
return msg
}
/**
CeleryTask -> CeleryMessage
*/
func Task2Msg(task *CeleryTask) *CeleryMessage {
msg := celeryMessagePool.Get().(*CeleryMessage)
msg.Body = task.EncodeBody()
msg.Properties.DeliveryInfo = *getDefaultCeleryDeliveryInfo()
msg.Properties.Priority = task.Priority
msg.Properties.CorrelationID = task.Id
msg.Headers.RootId = task.Id
msg.Headers.TaskId = task.Id
msg.Headers.Task = task.Task
msg.Headers.ETA = task.ETA
msg.Headers.Expires = task.Expires
msg.Headers.Retries = task.Retries
return msg
}
func Msg2Task(msg *CeleryMessage) *CeleryTask {
// ensure content-type is 'application/json'
if msg.ContentType != "application/json" {
log.Println("unsupported content type " + msg.ContentType)
return nil
}
// ensure body encoding is base64
if msg.Properties.BodyEncoding != "base64" {
log.Println("unsupported body encoding " + msg.Properties.BodyEncoding)
return nil
}
var task = CeleryTask{}
task.Id = msg.Headers.TaskId
task.ETA = msg.Headers.ETA
task.Task = msg.Headers.Task
task.Priority = msg.Properties.Priority
// TODO: task.Args = msg.Headers.ArgsRepr
// TODO: task.Kwargs = msg.Headers.kwargsRepr
// decode body
var body = DecodeBody(msg.Body)
if body == nil {
log.Println("failed to decode task message")
return nil
}
if body.Args != nil {
task.Args = body.Args
}
if body.Kwargs != nil {
task.Kwargs = body.Kwargs
}
return &task
}
func releaseCeleryMessage(v *CeleryMessage) {
v.reset()
celeryMessagePool.Put(v)
}
// CeleryDeliveryInfo represents deliveryinfo json
type CeleryDeliveryInfo struct {
RoutingKey string `json:"routing_key"`
Exchange string `json:"exchange"`
}
// 按照Celery现有的python实现,不是将CeleryTask直接进行json序列化
type CeleryTask struct {
Id string `json:"id"`
Task string `json:"task"`
Args []interface{} `json:"args"` // argsrepr
Kwargs map[string]interface{} `json:"kwargs"` // kwargsrepr
Retries int `json:"retries"`
// Protocol 2: ISO8601,格式:"2006-01-02T15:04:05", 与RFC3339: "2006-01-02T15:04:05Z07:00"不同
ETA time.Time `json:"eta" time_format:"2006-01-02T15:04:05"`
Expires time.Time `json:"expires" time_format:"2006-01-02T15:04:05"`
Priority int `json:"priority"`
Embed map[string]interface{} `json:"embed"`
}
func (tm *CeleryTask) reset() {
tm.Id = generateUUID()
tm.Task = ""
tm.Args = nil
tm.Kwargs = nil
}
var taskMessagePool = sync.Pool{
New: func() interface{} {
return &CeleryTask{
Id: generateUUID(),
Retries: 0,
}
},
}
func getTaskObj(task string) *CeleryTask {
msg := taskMessagePool.Get().(*CeleryTask)
msg.Task = task
msg.Args = make([]interface{}, 0)
msg.Kwargs = make(map[string]interface{})
msg.Embed = make(map[string]interface{})
msg.Expires = time.Now().AddDate(1, 0, 0)
return msg
}
func releaseTaskMessage(v *CeleryTask) {
v.reset()
taskMessagePool.Put(v)
}
type PythonBody struct {
Args []interface{}
Kwargs map[string]interface{}
Embed map[string]interface{}
}
// http://eagain.net/articles/go-json-array-to-struct/
func (body *PythonBody) UnmarshalJSON(buf []byte) error {
tmp := []interface{}{&body.Args, &body.Kwargs, &body.Embed}
wangLen := len(tmp)
if err := json.Unmarshal(buf, &tmp); err != nil {
return err
}
if g, e := len(tmp), wangLen; g != e {
return fmt.Errorf("wrong number of fields in Notification: %d != %d", g, e)
}
return nil
}
// DecodeTaskMessage decodes base64 encrypted body
func DecodeBody(encodedBody string) *PythonBody {
body, err := base64.StdEncoding.DecodeString(encodedBody)
if err != nil {
return nil
}
/**
[[], {"y": 2878, "x": 5456}, {"chord": null, "callbacks": null, "errbacks": null, "chain": null}]
*/
// var payloadStrArr = make([]string, 3)
var pybody PythonBody
json.Unmarshal(body, &pybody)
return &pybody
}
// EncodeBody returns base64 json encoded string
func (tm *CeleryTask) EncodeBody() string {
// python兼容版本
// args, kwargs, embed = self._payload # _payload is body
var payloadList = make([]interface{}, 3)
payloadList[0] = tm.Args
payloadList[1] = tm.Kwargs
payloadList[2] = tm.Embed
var jsonData, err = json.Marshal(payloadList)
log.Printf("json body: %s", jsonData)
if err != nil {
log.Fatalf("celery message encode failed: %s", err.Error())
}
encodedData := base64.StdEncoding.EncodeToString(jsonData)
return encodedData
}
// ResultMessage is return message received from broker
type ResultMessage struct {
ID string `json:"task_id"`
Status string `json:"status"`
Traceback interface{} `json:"traceback"`
Result interface{} `json:"result"`
Children []interface{} `json:"children"`
}
func (rm *ResultMessage) reset() {
rm.Result = nil
}
var resultMessagePool = sync.Pool{
New: func() interface{} {
return &ResultMessage{
Status: "SUCCESS",
Traceback: nil,
Children: nil,
}
},
}
func getResultMessage(val interface{}) *ResultMessage {
msg := resultMessagePool.Get().(*ResultMessage)
msg.Result = val
return msg
}
func getReflectionResultMessage(val *reflect.Value) *ResultMessage {
msg := resultMessagePool.Get().(*ResultMessage)
msg.Result = GetRealValue(val)
return msg
}
func releaseResultMessage(v *ResultMessage) {
v.reset()
resultMessagePool.Put(v)
}