Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QueuerUtil with BatchSend capability #347

Open
wants to merge 1 commit into
base: update-integration-router-plus-options-and-formatting
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 4 additions & 0 deletions packages/core/queues/index.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
const { QueuerUtil } = require('./queuer-util');
module.exports = {
QueuerUtil,
};
61 changes: 61 additions & 0 deletions packages/core/queues/queuer-util.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
const { v4: uuid } = require('uuid');
const AWS = require('aws-sdk');
const awsConfigOptions = () => {
const config = {};
if (process.env.IS_OFFLINE) {
console.log('Running in offline mode');
}
if (process.env.AWS_ENDPOINT) {
config.endpoint = process.env.AWS_ENDPOINT;
}
return config;
};
AWS.config.update(awsConfigOptions());
const sqs = new AWS.SQS();

const QueuerUtil = {
batchSend: async (entries = [], queueUrl) => {
console.log(
`Enqueuing ${entries.length} entries on SQS to queue ${queueUrl}`
);
const buffer = [];
const batchSize = 10;

for (const entry of entries) {
buffer.push({
Id: uuid(),
MessageBody: JSON.stringify(entry),
});
// Sends 10, then purges the buffer
if (buffer.length === batchSize) {
console.log('Buffer at 10, sending batch');
await sqs
.sendMessageBatch({
Entries: buffer,
QueueUrl: queueUrl,
})
.promise();
// Purge the buffer
buffer.splice(0, buffer.length);
}
}
console.log('Buffer at end, sending final batch');

// If any remaining entries under 10 are left in the buffer, send and return
if (buffer.length > 0) {
console.log(buffer);
return sqs
.sendMessageBatch({
Entries: buffer,
QueueUrl: queueUrl,
})
.promise();
}

// If we're exact... just return an empty object for now

return {};
},
};

module.exports = { QueuerUtil };
Loading