Skip to content

Instantly share code, notes, and snippets.

@csantanapr
Last active March 27, 2018 19:35
Show Gist options
  • Save csantanapr/ce5856f9ad9bb5a18a1882969569048d to your computer and use it in GitHub Desktop.
Save csantanapr/ce5856f9ad9bb5a18a1882969569048d to your computer and use it in GitHub Desktop.
Counts number of activations periodically and exists when no more found
function getAuthFromWskCLI(){
const { execSync } = require('child_process');
let auth = execSync('bx wsk property get --auth',{encoding:'utf8'}).split('whisk auth')[1].trim()
return auth;
}
let options = {
apihost: process.env['__OW_API_HOST'] || 'https://openwhisk.ng.bluemix.net',
api_key: process.env['__OW_API_KEY'] || getAuthFromWskCLI()
};
let ow = openwhisk(options);
let actionName = 'think2018';
let delay = 1800000; // 30 minutes (1800000) 1 minute (60000)
function findActivations(refInterval, since, result) {
console.log(`Found ${result.activations} activations for ${actionName} since ${new Date(since).toLocaleString('en-US')}`);
if (result.activations == 0) {
//code here to do when there is no more activity (i.e tear down external long running container handling websockets, or kafka producer broker for example)
clearInterval(refInterval)
}
}
let refInterval = setInterval(() => {
let since = new Date().getTime() - delay;
ow.activations.list({
name: actionName,
count: true,
since: since
}).then(findActivations.bind(null,refInterval,since))
}, delay)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment