forked from gocelery/gocelery
-
Notifications
You must be signed in to change notification settings - Fork 0
/
message.go
221 lines (197 loc) · 5.67 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
// Copyright (c) 2019 Sick Yoon
// This file is part of gocelery which is released under MIT license.
// See file LICENSE for full license details.
package gocelery
import (
"encoding/base64"
"encoding/json"
"log"
"reflect"
"sync"
"time"
uuid "github.com/satori/go.uuid"
)
// CeleryMessage is actual message to be sent to Redis
type CeleryMessage struct {
Body string `json:"body"`
Headers map[string]interface{} `json:"headers,omitempty"`
ContentType string `json:"content-type"`
Properties CeleryProperties `json:"properties"`
ContentEncoding string `json:"content-encoding"`
}
func (cm *CeleryMessage) reset() {
cm.Headers = nil
cm.Body = ""
cm.Properties.CorrelationID = uuid.Must(uuid.NewV4()).String()
cm.Properties.ReplyTo = uuid.Must(uuid.NewV4()).String()
cm.Properties.DeliveryTag = uuid.Must(uuid.NewV4()).String()
}
var celeryMessagePool = sync.Pool{
New: func() interface{} {
return &CeleryMessage{
Body: "",
Headers: nil,
ContentType: "application/json",
Properties: CeleryProperties{
BodyEncoding: "base64",
CorrelationID: uuid.Must(uuid.NewV4()).String(),
ReplyTo: uuid.Must(uuid.NewV4()).String(),
DeliveryInfo: CeleryDeliveryInfo{
Priority: 0,
RoutingKey: "celery",
Exchange: "celery",
},
DeliveryMode: 2,
DeliveryTag: uuid.Must(uuid.NewV4()).String(),
},
ContentEncoding: "utf-8",
}
},
}
func getCeleryMessage(encodedTaskMessage string) *CeleryMessage {
msg := celeryMessagePool.Get().(*CeleryMessage)
msg.Body = encodedTaskMessage
return msg
}
func releaseCeleryMessage(v *CeleryMessage) {
v.reset()
celeryMessagePool.Put(v)
}
// CeleryProperties represents properties json
type CeleryProperties struct {
BodyEncoding string `json:"body_encoding"`
CorrelationID string `json:"correlation_id"`
ReplyTo string `json:"reply_to"`
DeliveryInfo CeleryDeliveryInfo `json:"delivery_info"`
DeliveryMode int `json:"delivery_mode"`
DeliveryTag string `json:"delivery_tag"`
}
// CeleryDeliveryInfo represents deliveryinfo json
type CeleryDeliveryInfo struct {
Priority int `json:"priority"`
RoutingKey string `json:"routing_key"`
Exchange string `json:"exchange"`
}
// GetTaskMessage retrieve and decode task messages from broker
func (cm *CeleryMessage) GetTaskMessage() *TaskMessage {
// ensure content-type is 'application/json'
if cm.ContentType != "application/json" {
log.Println("unsupported content type " + cm.ContentType)
return nil
}
// ensure body encoding is base64
if cm.Properties.BodyEncoding != "base64" {
log.Println("unsupported body encoding " + cm.Properties.BodyEncoding)
return nil
}
// ensure content encoding is utf-8
if cm.ContentEncoding != "utf-8" {
log.Println("unsupported encoding " + cm.ContentEncoding)
return nil
}
// decode body
taskMessage, err := DecodeTaskMessage(cm.Body)
if err != nil {
log.Println("failed to decode task message")
return nil
}
return taskMessage
}
// TaskMessage is celery-compatible message
type TaskMessage struct {
ID string `json:"id"`
Task string `json:"task"`
Args []interface{} `json:"args"`
Kwargs map[string]interface{} `json:"kwargs"`
Retries int `json:"retries"`
ETA *string `json:"eta"`
Expires *time.Time `json:"expires"`
}
func (tm *TaskMessage) reset() {
tm.ID = uuid.Must(uuid.NewV4()).String()
tm.Task = ""
tm.Args = nil
tm.Kwargs = nil
}
var taskMessagePool = sync.Pool{
New: func() interface{} {
eta := time.Now().Format(time.RFC3339)
return &TaskMessage{
ID: uuid.Must(uuid.NewV4()).String(),
Retries: 0,
Kwargs: nil,
ETA: &eta,
}
},
}
func getTaskMessage(task string) *TaskMessage {
msg := taskMessagePool.Get().(*TaskMessage)
msg.Task = task
msg.Args = make([]interface{}, 0)
msg.Kwargs = make(map[string]interface{})
msg.ETA = nil
return msg
}
func releaseTaskMessage(v *TaskMessage) {
v.reset()
taskMessagePool.Put(v)
}
// DecodeTaskMessage decodes base64 encrypted body and return TaskMessage object
func DecodeTaskMessage(encodedBody string) (*TaskMessage, error) {
body, err := base64.StdEncoding.DecodeString(encodedBody)
if err != nil {
return nil, err
}
message := taskMessagePool.Get().(*TaskMessage)
err = json.Unmarshal(body, message)
if err != nil {
return nil, err
}
return message, nil
}
// Encode returns base64 json encoded string
func (tm *TaskMessage) Encode() (string, error) {
if tm.Args == nil {
tm.Args = make([]interface{}, 0)
}
jsonData, err := json.Marshal(tm)
if err != nil {
return "", err
}
encodedData := base64.StdEncoding.EncodeToString(jsonData)
return encodedData, err
}
// 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)
}