-
Notifications
You must be signed in to change notification settings - Fork 0
/
index.js
63 lines (50 loc) · 1.6 KB
/
index.js
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
require('./tracing');
const AWS = require('aws-sdk');
const sqs = new AWS.SQS({ apiVersion: '2015-03-31' });
(() => {
const https = require('https');
const originalHttpsRequestFn = https.request;
https.request = (...args) => {
const outgoingReq = originalHttpsRequestFn(...args);
if (args[0]?.host === 'sqs.us-east-1.amazonaws.com' && args[0].method === 'POST') {
const outgoingReqOriginalEndFn = outgoingReq.end;
outgoingReq.end = function (payload, ...rest) {
if (typeof payload === 'string') {
console.debug('HTTP BODY DEBUG LOG ------>', payload);
}
return outgoingReqOriginalEndFn.call(this, payload, ...rest);
};
}
return outgoingReq;
};
})();
const receiveMessageConfig = {
QueueUrl: process.env.QUEUE_URL,
AttributeNames: ['SentTimestamp'],
MaxNumberOfMessages: 1,
MessageAttributeNames: ['All'],
VisibilityTimeout: 20,
WaitTimeSeconds: 10,
};
async function sqsConsumer() {
while (true) {
const resp = await sqs.receiveMessage(receiveMessageConfig).promise();
const messages = resp.Messages || [];
console.log(messages)
if (messages.length) {
received_message_ids = messages.map(({ ReceiptHandle }, k) => ({
Id: k.toString(),
ReceiptHandle,
}));
// do stuff
const deletionPromise = sqs
.deleteMessageBatch({
QueueUrl: receiveMessageConfig.QueueUrl,
Entries: received_message_ids,
})
.promise();
await deletionPromise;
}
}
}
sqsConsumer();