-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathindex.js
71 lines (56 loc) · 2.17 KB
/
index.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
71
'use strict';
var azure = require('azure-storage');
var Base64Encoder = azure.QueueMessageEncoder.TextBase64QueueMessageEncoder;
var config = require('./config.json');
var storageAccount = process.env.AZURE_STORAGE_ACCOUNT || config.azureStorageAccount;
var accessKey = process.env.AZURE_STORAGE_ACCESS_KEY || config.azureStorageAccessKey;
var queueName = process.env.AZURE_QUEUE_NAME || config.azureQueueName;
var queueService = azure.createQueueService(storageAccount, accessKey);
queueService.messageEncoder = new Base64Encoder(); // default is TextXMLQueueMessageEncoder, set to null if you want to use plain text
var i = 0;
(function pushAndPop (i) {
pushMessage(i, function () {
popMessages();
if (i < 5) { setTimeout(pushAndPop.bind(this, i + 1), 3000); }
});
})(i);
function popMessages () {
var options = {
numOfMessages: 1, // up to 32 messages, default is 1
visibilityTimeout: 20 // delay in sec before the message is visible again, default is 30
};
queueService.getMessages(queueName, options, function(error, serverMessages) {
var message;
var content;
if (error) {
console.error('unable to get message from the queue', error);
return;
}
if (serverMessages.length === 0) {
console.info('no message in the queue');
return;
}
message = serverMessages[0];
content = JSON.parse(message.messageText);
console.log('message %s retrieved: %s', content.counter, content.message);
queueService.deleteMessage(queueName, message.messageId, message.popReceipt, function (error) {
if (error) {
console.error('unable to delete message %s', content.counter, error);
return;
}
console.log('message %s deleted', content.counter);
});
});
}
function pushMessage (counter, callback) {
// Message can only be a string, so we stringifiy our object
var msg = JSON.stringify({message: 'Hello World ' + counter, counter: counter});
queueService.createMessage(queueName, msg, function (error) {
if (error) {
console.error('unable to push message %s', counter, error);
return;
}
console.log('message %s pushed in the queue', counter);
callback();
});
}