[SQS example] #tags: sqs
const _ = require('lodash');
const castHelper = require('@/helpers/cast');
const sqsHelper = require('@/helpers/sqs');
const isJSON = str => !_.isError(_.attempt(JSON.parse, str));
const castHandler = async (event, context, callback) => {
try {
const messageStr = event.Records[0].body;
const { receiptHandle } = event.Records[0];
if (!isJSON(messageStr)) {
callback(new Error('Invalid message format'));
}
const message = JSON.parse(messageStr);
castHelper.executeCommand(message);
// const { app, command, ...params } = message;
// if (app === 'cast' && _.keys(castCommands).includes(command)) {
// castCommands[command](params);
// }
sqsHelper.deleteMessage(receiptHandle);
context.succeed();
} catch (err) {
context.fail(err);
}
};
module.exports = {
castHandler,
};
const AWS = require('aws-sdk');
const config = require('@config');
AWS.config.update({
region: config.sqs.region,
});
const sqs = new AWS.SQS({
apiVersion: config.sqs.version,
// endpoint: config.sqs.url.cast,
});
module.exports = {
addCastCommandToQueue(obj) {
return sqs.sendMessage({
MessageBody: JSON.stringify(obj),
QueueUrl: config.sqs.url.cast,
}).promise();
},
deleteMessage(receiptHandle) {
const deleteParams = {
QueueUrl: config.sqs.url.cast,
ReceiptHandle: receiptHandle,
};
return sqs.deleteMessage(deleteParams).promise();
},
};