Created
February 4, 2019 13:29
-
-
Save guikaercher/4a184102a70586bf4e0a53c708bac71c to your computer and use it in GitHub Desktop.
SQS helper for Node.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module.exports = (sqs) => { | |
const createQueue = async (queueName) => { | |
return await new Promise((resolve, reject) => { | |
const createQueueParams = { | |
QueueName: queueName, | |
Attributes: { | |
'DelaySeconds': '0', | |
'MessageRetentionPeriod': '86400' | |
} | |
}; | |
sqs.createQueue(createQueueParams, (err, data) => { | |
if (err) reject(err) | |
else resolve(data.QueueUrl) | |
}); | |
}) | |
} | |
const deleteQueue = async(queueUrl) => { | |
return await new Promise ((resolve, reject) => { | |
const params = {QueueUrl: queueUrl}; | |
sqs.deleteQueue(params, function(err, data) { | |
/* istanbul ignore next */ | |
if (err) reject(err) | |
resolve(data); | |
}) | |
}) | |
} | |
const listQueuesUrls = async () => { | |
return await new Promise((resolve, reject) => { | |
sqs.listQueues({}, (err, data) => { | |
if (err) reject(err) | |
else resolve(data.QueueUrls) | |
}); | |
}) | |
} | |
const _sqsSendMessage = async (message, queueURLToSendMessage) => { | |
return await new Promise((resolve, reject) => { | |
const sendMsgParams = { | |
MessageBody: message, /* required */ | |
QueueUrl: queueURLToSendMessage, /* required */ | |
DelaySeconds: 0, | |
MessageAttributes: { | |
'messageName': { | |
DataType: 'String', /* required */ | |
StringValue: 'STRING_VALUE' | |
} | |
} | |
}; | |
sqs.sendMessage(sendMsgParams, function(err, data) { | |
/* istanbul ignore next */ | |
if (err) reject(err) | |
resolve(data) | |
}); | |
}) | |
} | |
const deleteMessage = async (queueURL, data) => { | |
return await new Promise ((resolve, reject)=> { | |
const deleteParams = { | |
QueueUrl: queueURL, | |
ReceiptHandle: data.Messages[0].ReceiptHandle | |
}; | |
sqs.deleteMessage(deleteParams, function(err, data) { | |
/* istanbul ignore next */ | |
if (err) reject(err) | |
else resolve(data) | |
}) | |
}) | |
} | |
const pullMessageFromQueueAndDelete = async (queueURL) => { | |
return await new Promise ((resolve, reject) => { | |
const receiveMsgParams = { | |
AttributeNames: ["SentTimestamp"], | |
MaxNumberOfMessages: 1, | |
MessageAttributeNames: [ | |
"All" | |
], | |
QueueUrl: queueURL, | |
VisibilityTimeout: 20, | |
WaitTimeSeconds: 0 | |
}; | |
sqs.receiveMessage(receiveMsgParams, (err, data) => { | |
if (err) reject(err) | |
const message = data.Messages | |
deleteMessage(queueURL, data) | |
resolve(message) | |
}); | |
}) | |
} | |
const findQueueUrlByName = async (queueName) => { | |
const queuesAvailable = await listQueuesUrls() | |
return queuesAvailable.filter(queue => queue.includes(queueName))[0] | |
} | |
const sendMessage = async(messsage, queueName) => { | |
const queuesAvailable = await listQueuesUrls() | |
const queueUrl = await findQueueUrlByName(queueName) | |
if (!queuesAvailable || !queueUrl) await createQueue(queueName) | |
const queueURLToSendMessage = queueUrl | |
const messageSentStatus = await _sqsSendMessage(messsage, queueURLToSendMessage) | |
return messageSentStatus | |
} | |
return { | |
createQueue, | |
listQueuesUrls, | |
findQueueUrlByName, | |
sendMessage, | |
pullMessageFromQueueAndDelete, | |
deleteQueue, | |
deleteMessage | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment