vtlab-generic-functions 1.0.30 → 1.0.32

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.
Files changed (2) hide show
  1. package/package.json +1 -1
  2. package/sqs/index.js +93 -2
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "vtlab-generic-functions",
3
- "version": "1.0.30",
3
+ "version": "1.0.32",
4
4
  "scripts": {
5
5
  "test": "jest"
6
6
  },
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,97 @@ 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
+
174
+ /**
175
+ * Saves multiple integration messages to an SQS queue in batch.
176
+ *
177
+ * @param {Array<Object>} documents - Array of integration message documents to be saved.
178
+ * @returns {Promise<Object>} - A promise that resolves to the response from SQS.
179
+ * @throws {Error} - Throws an error if saving the integration messages fails.
180
+ */
181
+ const saveIntegrationMessagesBatch = async (documents = []) => {
182
+ try {
183
+ if (!Array.isArray(documents)) {
184
+ throw new Error('Documents must be an array');
185
+ }
186
+
187
+ if (documents.length === 0) {
188
+ return [];
189
+ }
190
+
191
+ // Validate each document
192
+ const validatedMessages = documents.map(doc =>
193
+ validator.validate(doc, 'integrationMessages')
194
+ );
195
+
196
+ const queue = await config.get('microservices.sqs.integrationMessage');
197
+ return sendSQSMessageBatch(validatedMessages, queue, false);
198
+ } catch (error) {
199
+ throw error;
200
+ }
201
+ };
202
+
203
+
113
204
  module.exports = {
114
- saveJobEvent, saveIntegrationMessages, sendZplSqsMessage
205
+ saveJobEvent, saveIntegrationMessages, saveIntegrationMessagesBatch, sendZplSqsMessage,sendSQSMessageBatch
115
206
  };