vtlab-generic-functions 1.0.29 → 1.0.31
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/sqs/index.js +63 -2
package/package.json
CHANGED
package/sqs/index.js
CHANGED
|
@@ -1,4 +1,4 @@
|
|
|
1
|
-
const { SQSClient, SendMessageCommand } = require("@aws-sdk/client-sqs");
|
|
1
|
+
const { SQSClient, SendMessageCommand, SendMessageBatchCommand } = require("@aws-sdk/client-sqs");
|
|
2
2
|
const config = require('../config');
|
|
3
3
|
const validator = require('../validator');
|
|
4
4
|
|
|
@@ -110,6 +110,67 @@ const sendZplSqsMessage = async ({s3PathOrigin, s3PathDestination, type = "pdf",
|
|
|
110
110
|
return await sendSQSMessage({payload, stageVariables}, queue, true);
|
|
111
111
|
}
|
|
112
112
|
|
|
113
|
+
/**
|
|
114
|
+
* Sends multiple messages to an SQS queue in a single batch.
|
|
115
|
+
*
|
|
116
|
+
* @param {Array<Object>} messages - Array of messages to be sent to the queue.
|
|
117
|
+
* @param {string} queueUrl - The URL of the SQS queue.
|
|
118
|
+
* @param {boolean} [isFIFOQueue=true] - Specifies if the queue is a FIFO queue.
|
|
119
|
+
* @returns {Promise<Object>} - A promise that resolves to the response from SQS.
|
|
120
|
+
* @throws {Error} - Throws an error if sending the messages fails.
|
|
121
|
+
*/
|
|
122
|
+
let batchSQSClient;
|
|
123
|
+
const sendSQSMessageBatch = async (messages, queueUrl, isFIFOQueue = true) => {
|
|
124
|
+
try {
|
|
125
|
+
const environment = process.env.NODE_ENV || 'develop';
|
|
126
|
+
|
|
127
|
+
if (!batchSQSClient) {
|
|
128
|
+
const awsCredentials = await config.get('awsCredentials');
|
|
129
|
+
batchSQSClient = new SQSClient({
|
|
130
|
+
region: awsCredentials.region,
|
|
131
|
+
credentials: {
|
|
132
|
+
accessKeyId: awsCredentials.accessKeyId,
|
|
133
|
+
secretAccessKey: awsCredentials.secretAccessKey
|
|
134
|
+
},
|
|
135
|
+
maxAttempts: 3,
|
|
136
|
+
requestHandler: {
|
|
137
|
+
keepAlive: true,
|
|
138
|
+
maxSockets: 50, // Limit concurrent connections
|
|
139
|
+
keepAliveMsecs: 1000
|
|
140
|
+
}
|
|
141
|
+
});
|
|
142
|
+
}
|
|
143
|
+
|
|
144
|
+
// Split messages into chunks of 10 (SQS batch limit)
|
|
145
|
+
const chunkSize = 10;
|
|
146
|
+
const messageChunks = [];
|
|
147
|
+
for (let i = 0; i < messages.length; i += chunkSize) {
|
|
148
|
+
messageChunks.push(messages.slice(i, i + chunkSize));
|
|
149
|
+
}
|
|
150
|
+
|
|
151
|
+
const results = [];
|
|
152
|
+
for (const chunk of messageChunks) {
|
|
153
|
+
const entries = chunk.map((message, index) => ({
|
|
154
|
+
Id: `msg-${index}`, // Unique ID for each message in the batch
|
|
155
|
+
MessageBody: JSON.stringify(message),
|
|
156
|
+
...(isFIFOQueue && { MessageGroupId: environment })
|
|
157
|
+
}));
|
|
158
|
+
|
|
159
|
+
const command = new SendMessageBatchCommand({
|
|
160
|
+
QueueUrl: queueUrl,
|
|
161
|
+
Entries: entries
|
|
162
|
+
});
|
|
163
|
+
|
|
164
|
+
const response = await sqsClient.send(command);
|
|
165
|
+
results.push(response);
|
|
166
|
+
}
|
|
167
|
+
|
|
168
|
+
return results;
|
|
169
|
+
} catch (error) {
|
|
170
|
+
throw error;
|
|
171
|
+
}
|
|
172
|
+
};
|
|
173
|
+
|
|
113
174
|
module.exports = {
|
|
114
|
-
saveJobEvent, saveIntegrationMessages, sendZplSqsMessage
|
|
175
|
+
saveJobEvent, saveIntegrationMessages, sendZplSqsMessage,sendSQSMessageBatch
|
|
115
176
|
};
|