Last active
August 29, 2015 21:13
-
-
Save shamasis/86bf5c7076c9eff97bbd to your computer and use it in GitHub Desktop.
SlackBootstrap for SailsJS
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
/** | |
* Bootstrap module for Slack notification on server startup | |
* | |
* Expects: config/slack.js | |
* - enabled | |
* - organisation | |
* - key | |
* - messages.spawn | |
* - payload | |
*/ | |
module.exports = function (callback) { | |
// call slack hook for enabled environments only | |
if (!sails.config.slack || sails.config.slack.enabled !== true) { | |
return callback(); | |
} | |
var Slack = require('node-slack'), | |
slack, | |
slackPayload; | |
// put communication in try block to avoid server bootup issues | |
try { | |
slack = new Slack(sails.config.slack.organisation, sails.config.slack.key); | |
slackPayload = _.cloneDeep(sails.config.slack.payload); | |
// set appropriate message on slack payload and send it. | |
slackPayload.text = sails.config.slack.messages.spawn; | |
// in case environment is development, do not call slack, but log output | |
if (sails.config.environment === 'development') { | |
sails.log.info('\nslack payload: start\n', slackPayload, '\nslack payload: end'); | |
} | |
else { | |
// be double sure, hence pack this in try block | |
slack.send(slackPayload); | |
} | |
} | |
catch (e) { | |
// if slack initialisation has error, no point proceeding, | |
sails.log.error('slack-bootstrap:', e.message || e); | |
return callback(); // but do not stop boot, which means don't pass error to callback | |
} | |
callback(); | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment