-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathjob-queue.js
70 lines (62 loc) · 2.04 KB
/
job-queue.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
var AWS = require('aws-sdk');
AWS.config.loadFromPath('./s3config.json');
var sqs = new AWS.SQS();
var queueUrl = 'https://sqs.eu-west-1.amazonaws.com/342335610929/eagerbeaver';
exports.SendMessage = SendMessage;
exports.ReceiveMessage = ReceiveMessage;
function SendMessage(messageBody, callback) {
console.log('sendMessage called with messageBody', messageBody);
var params = {
MessageBody: messageBody,
QueueUrl: queueUrl,
DelaySeconds: 0,
};
sqs.sendMessage(params, function(err, data) {
if (err) {
console.error('sendMessage error:', err, err.stack);
}
else {
console.log('sendMessage done:', data);
}
callback(err, data);
});
}
function ReceiveMessage(callback) {
var params = {
QueueUrl: queueUrl, /* required */
AttributeNames: [
'Policy | VisibilityTimeout | MaximumMessageSize | MessageRetentionPeriod | ApproximateNumberOfMessages | ApproximateNumberOfMessagesNotVisible | CreatedTimestamp | LastModifiedTimestamp | QueueArn | ApproximateNumberOfMessagesDelayed | DelaySeconds | ReceiveMessageWaitTimeSeconds | RedrivePolicy',
/* more items */
],
MaxNumberOfMessages: 1,
};
sqs.receiveMessage(params, function(err, data) {
if (err) {
console.error(err, err.stack); // an error occurred
callback(err, null);
}
else {
var messages = data.Messages;
if(messages) {
var firstMessage = messages[0];
console.log('Received message: ' + JSON.stringify(firstMessage));
deleteMessage(firstMessage.ReceiptHandle); //TODO: Remove this, make it app responsibilty
callback(null, firstMessage);
}
}
});
}
function deleteMessage(receiptHandle) {
var params = {
QueueUrl: queueUrl, /* required */
ReceiptHandle: receiptHandle /* required */
};
sqs.deleteMessage(params, function(err, data) {
if (err) {
console.error('Error deleting message:', err, err.stack); // an error occurred
}
else {
console.log('Message deleted: ', data); // successful response
}
});
};