Created
March 18, 2018 22:13
-
-
Save scionwest/1cb843cbb0e27395c3c1900ebf381931 to your computer and use it in GitHub Desktop.
This file contains hidden or 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 = function() { | |
var azure = require('azure'); | |
var topic = "foo-bar"; | |
var connectionString = "Endpoint=sb://"; | |
console.log("Configuring Service Bus."); | |
var serviceBusService = azure.createServiceBusService(connectionString); | |
var movies = []; | |
for(count = 0; count < 5000; count++) { | |
movies.push({id: count}); | |
} | |
var promise = new Promise(function(resolve, reject) { | |
serviceBusService.createTopicIfNotExists(topic, function(error) { | |
if (error) { | |
console.log('failed to get the service bus topic'); | |
reject(); | |
} | |
var messages = []; | |
for(index = 0; index < movies.length; index++) { | |
var message = { | |
body: JSON.stringify(movies[index]), | |
customProperties: { | |
messageNumber: index | |
} | |
} | |
messages.push(message); | |
} | |
resolve(messages); | |
}); | |
}); | |
promise.then(messages => { | |
var promises = []; | |
while (messages.length > 0) { | |
for(index = 0; index < 1000; index++) { | |
promises.push(sendMessage(serviceBusService, topic, messages[index])); | |
} | |
messages.splice(0, 1000); | |
Promise.all(promises).then(() => { | |
promises = [] | |
}); | |
} | |
}); | |
} | |
function sendMessage(serviceBusService, topic, message) { | |
return new Promise((resolve, reject) => { | |
serviceBusService.sendTopicMessage(topic, message, (error, result) => { | |
if (error) { | |
console.log(error); | |
reject(); | |
} | |
resolve(); | |
}); | |
}); | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment