Skip to content

Instantly share code, notes, and snippets.

@TheRyanBurke
Created December 11, 2014 22:11
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save TheRyanBurke/f567981a5d7d168b3c4a to your computer and use it in GitHub Desktop.
Save TheRyanBurke/f567981a5d7d168b3c4a to your computer and use it in GitHub Desktop.
visualize-lambda-via-thingfabric.js
var request = require('request');
// http://2lemetry.com/2014/11/18/smarter-client-usage-pooling/
var getClientFromPool = function(poolSize, prefix) {
var pick = Math.floor((Math.random() * poolSize) + 1);
prefix = prefix || "client";
var clientId = prefix.concat(pick);
return clientId; // e.g. "client74"
};
var publishStatus = function(status, clientid, cb) {
var opts = {
url: 'https://q.thingfabric.com/3/publish',
qs: {
topic: 'your-thingfabric-project-topic',
qos: 0,
client: clientid
},
json: true,
body: {
status: status,
id: clientid
},
auth: {
username: 'credential-key',
password: 'credential-secret'
}
};
request.post(opts, cb);
};
var clientid = getClientFromPool(200,"lambda");
exports.handler = function(event, context) {
// publish start
publishStatus("starting " + event.value, clientid);
// do work
var randomSleep = Math.floor((Math.random() * 2800) + 100);
setTimeout(function() {
console.log("slept for " + randomSleep + " milliseconds");
// job complete, publish complete
publishStatus("done " + event.value, clientid, function() {
context.done(null, 'done'); // SUCCESS with message
});
}, randomSleep);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment