Created
December 11, 2014 22:11
-
-
Save TheRyanBurke/f567981a5d7d168b3c4a to your computer and use it in GitHub Desktop.
visualize-lambda-via-thingfabric.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
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