-
Notifications
You must be signed in to change notification settings - Fork 3
/
gf_aws_sqs.go
333 lines (268 loc) · 10.2 KB
/
gf_aws_sqs.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
/*
GloFlow application and media management/publishing platform
Copyright (C) 2021 Ivan Trajkovic
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation; either version 2 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
*/
package gf_aws
import (
// "fmt"
"context"
"encoding/json"
log "github.com/sirupsen/logrus"
"github.com/aws/aws-sdk-go-v2/aws"
"github.com/aws/aws-sdk-go-v2/config"
"github.com/aws/aws-sdk-go-v2/service/sqs"
"github.com/aws/aws-sdk-go-v2/service/sqs/types"
"github.com/gloflow/gloflow/go/gf_core"
// "github.com/davecgh/go-spew/spew"
)
//-------------------------------------------------------------
type GF_SQS_queue struct {
Name_str string
AWS_url_str string
}
//-------------------------------------------------------------
// INIT
func SQSinit(pRuntimeSys *gf_core.RuntimeSys) (*sqs.Client, *gf_core.GFerror) {
cfg, err := config.LoadDefaultConfig(context.TODO())
if err != nil {
gfErr := gf_core.ErrorCreate("failed to create AWS API session",
"aws_client_v2_create",
map[string]interface{}{},
err, "gf_aws", pRuntimeSys)
return nil, gfErr
}
client := sqs.NewFromConfig(cfg)
return client, nil
/*sess, err := session.NewSession()
if err != nil {
gfErr := gf_core.ErrorCreate("failed to create AWS API session",
"aws_session_create",
map[string]interface{}{},
err, "gf_aws", pRuntimeSys)
return nil, gfErr
}
svc := sqs.New(sess)
return svc, nil*/
}
//-------------------------------------------------------------
func SQSgetQueueInfo(pSQSqueueNameStr string,
pSQSclient *sqs.Client,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (*GF_SQS_queue, *gf_core.GFerror) {
sqs_queue_url_str, gfErr := SQSqueueGetURL(pSQSqueueNameStr,
pSQSclient,
pCtx,
pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
queue_info := &GF_SQS_queue{
Name_str: pSQSqueueNameStr,
AWS_url_str: sqs_queue_url_str,
}
return queue_info, nil
}
//-------------------------------------------------------------
// QUEUE_GET_URL
func SQSqueueGetURL(pSQSqueueNameStr string,
pSQSclient *sqs.Client,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (string, *gf_core.GFerror) {
result, err := pSQSclient.GetQueueUrl(pCtx, &sqs.GetQueueUrlInput{
QueueName: aws.String(pSQSqueueNameStr),
})
if err != nil {
gfErr := gf_core.ErrorCreate("failed to get AWS SQS queue URL",
"aws_sqs_queue_get_url_error",
map[string]interface{}{
"sqs_queue_name_str": pSQSqueueNameStr,
},
err, "gf_aws", pRuntimeSys)
return "", gfErr
}
sqs_queue_url_str := *result.QueueUrl
return sqs_queue_url_str, nil
}
//-------------------------------------------------------------
// QUEUE_CREATE
func SQSqueueCreate(pSQSqueueNameStr string,
pSQSclient *sqs.Client,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (*GF_SQS_queue, *gf_core.GFerror) {
log.WithFields(log.Fields{"name": pSQSqueueNameStr,}).Info("AWS_SQS - creating new queue")
// CREATE
_, err := pSQSclient.CreateQueue(pCtx, &sqs.CreateQueueInput{
QueueName: aws.String(pSQSqueueNameStr),
})
if err != nil {
gfErr := gf_core.ErrorCreate("failed to create AWS SQS queue",
"aws_sqs_queue_create_error",
map[string]interface{}{
"sqs_queue_name_str": pSQSqueueNameStr,
},
err, "gf_aws", pRuntimeSys)
return nil, gfErr
}
// URL
queue_url_str, gfErr := SQSqueueGetURL(pSQSqueueNameStr, pSQSclient, pCtx, pRuntimeSys)
if gfErr != nil {
return nil, gfErr
}
queue := &GF_SQS_queue{
Name_str: pSQSqueueNameStr,
AWS_url_str: queue_url_str,
}
return queue, nil
}
//-------------------------------------------------------------
// QUEUE_DELETE
func SQSqueueDelete(pSQSqueueNameStr string,
pAWSclient *sqs.Client,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
return nil
}
//-------------------------------------------------------------
func SQSmsgPull(pQueueInfo *GF_SQS_queue,
pAWSclient *sqs.Client,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) (map[string]interface{}, *gf_core.GFerror) {
log.WithFields(log.Fields{"name": pQueueInfo.Name_str,}).Info("AWS_SQS - pull message from queue")
// Must be >= 0 and <= 20, if provided
timeout_sec_int := 20
// SQS_RECEIVE_MESSAGE
result, err := pAWSclient.ReceiveMessage(pCtx, &sqs.ReceiveMessageInput{
QueueUrl: aws.String(pQueueInfo.AWS_url_str),
AttributeNames: []types.QueueAttributeName{
"SentTimestamp",
},
MaxNumberOfMessages: 1,
MessageAttributeNames: []string { // []types.QueueAttributeName{
"All",
},
// The duration (in seconds) for which the call waits for a message to arrive
// in the queue before returning. If a message is available, the call returns
// sooner than WaitTimeSeconds. If no messages are available and the wait time
// expires, the call returns successfully with an empty list of messages.
WaitTimeSeconds: int32(timeout_sec_int),
})
if err != nil {
gfErr := gf_core.ErrorCreate("failed to receive a message from SQS queue durring msg_pull function",
"aws_sqs_queue_receive_msg_error",
map[string]interface{}{
"sqs_queue_name_str": pQueueInfo.Name_str,
"sqs_queue_url_str": pQueueInfo.AWS_url_str,
},
err, "gf_aws", pRuntimeSys)
return nil, gfErr
}
// fmt.Println("*******************************************")
// spew.Dump(result.Messages)
if result.Messages != nil {
msg := result.Messages[0]
msg_body_str := *msg.Body
log.WithFields(log.Fields{"name": pQueueInfo.Name_str, "msg_id": *msg.MessageId, "msg_body": msg_body_str}).Info("AWS_SQS - pull message from queue - OK")
//--------------------------
// DECODE_MESSAGE
msg_map := map[string]interface{}{}
if err := json.Unmarshal([]byte(msg_body_str), &msg_map); err != nil {
gfErr := gf_core.ErrorCreate("failed to JSON decode a message body pulled from SQS",
"json_decode_error",
map[string]interface{}{
"sqs_queue_name_str": pQueueInfo.Name_str,
"sqs_queue_url_str": pQueueInfo.AWS_url_str,
},
err, "gf_aws", pRuntimeSys)
return nil, gfErr
}
//--------------------------
// DELETE_MESSAGE
log.WithFields(log.Fields{"name": pQueueInfo.Name_str, "msg_id": *msg.MessageId}).Info("AWS_SQS - delete message from queue")
msg_receipt_handle := msg.ReceiptHandle
// https://docs.aws.amazon.com/sdk-for-go/api/service/sqs/#SQS.DeleteMessage
_, err = pAWSclient.DeleteMessage(pCtx, &sqs.DeleteMessageInput{
QueueUrl: aws.String(pQueueInfo.AWS_url_str),
ReceiptHandle: msg_receipt_handle,
})
if err != nil {
gfErr := gf_core.ErrorCreate("failed to delete a message from SQS queue durring msg_pull function, after receiving it",
"aws_sqs_queue_delete_msg_error",
map[string]interface{}{
"sqs_queue_name_str": pQueueInfo.Name_str,
"sqs_queue_url_str": pQueueInfo.AWS_url_str,
},
err, "gf_aws", pRuntimeSys)
return nil, gfErr
}
//--------------------------
return msg_map, nil
}
return nil, nil
}
//-------------------------------------------------------------
// MSG_PUSH
func SQSmsgPush(p_msg interface{},
pQueueInfo *GF_SQS_queue,
pAWSclient *sqs.Client,
pCtx context.Context,
pRuntimeSys *gf_core.RuntimeSys) *gf_core.GFerror {
log.WithFields(log.Fields{"name": pQueueInfo.Name_str,}).Info("AWS_SQS - push message to queue")
msg_data_JSON_encoded, err := json.Marshal(p_msg)
if err != nil {
gfErr := gf_core.ErrorCreate("failed to JSON encode a message to send to SQS queue",
"json_encode_error",
map[string]interface{}{
"sqs_queue_name_str": pQueueInfo.Name_str,
"sqs_queue_url_str": pQueueInfo.AWS_url_str,
},
err, "gf_aws", pRuntimeSys)
return gfErr
}
_, err = pAWSclient.SendMessage(pCtx, &sqs.SendMessageInput{
MessageBody: aws.String(string(msg_data_JSON_encoded)),
QueueUrl: aws.String(pQueueInfo.AWS_url_str),
// DelaySeconds: 10,
/*MessageAttributes: map[string]types.MessageAttributeValue{
"time_sec": {
DataType: aws.String("String"),
StringValue: aws.String(fmt.Sprintf("%f", p_msg.TimeSec)),
},
"module": {
DataType: aws.String("String"),
StringValue: aws.String(p_msg.Module),
},
"type": {
DataType: aws.String("String"),
StringValue: aws.String(p_msg.Type),
},
"msg": {
DataType: aws.String("String"),
StringValue: aws.String(p_msg.Msg),
},
},*/
})
if err != nil {
gfErr := gf_core.ErrorCreate("failed to send a message to SQS queue",
"aws_sqs_queue_send_msg_error",
map[string]interface{}{
"sqs_queue_name_str": pQueueInfo.Name_str,
"sqs_queue_url_str": pQueueInfo.AWS_url_str,
},
err, "gf_aws", pRuntimeSys)
return gfErr
}
// msg_id_str := *result.MessageId
// fmt.Printf("Success - msg ID - %s\n", msg_id_str)
return nil
}