Skip to content

Commit a4ff441

Browse files
committed
Exit st2chatop from st2client failures.
Create a new environment variable `EXIT_ON_FAILURES` with default value `false`. If user is not explicitly setup, it will work as today. If `EXIT_ON_FAILURES` is setup, for the case if `st2chatops` encounters authenticate or unresolved application URL issues with Stackstorm, instead it's being unresponsive/hanging/waiting in a running state, `st2chatops` will be exit.
1 parent 014ae91 commit a4ff441

File tree

3 files changed

+25
-9
lines changed

3 files changed

+25
-9
lines changed

CHANGELOG.rst

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ in development
88
-----
99
* Add initial support for Microsoft Teams via BotFramework (new feature)
1010
* Add unit tests for all adapters (improvement)
11+
* Add environment variable `EXIT_ON_FAILURES` to allow `st2chatops` to exit from `st2client` failures.
1112

1213
0.9.2
1314
-----

README.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ specified when running hubot:
7777
* `ST2_SLACK_FAIL_COLOR` - Slack attachement color for failures either be one of good, warning, danger, or any hex color code (optional).
7878
* `ST2_ROCKETCHAT_SUCCESS_COLOR` - RocketChat attachement color for success, can either be one of good, warning, danger, or any hex color code (optional).
7979
* `ST2_ROCKETCHAT_FAIL_COLOR` - RocketChat attachement color for failures either be one of good, warning, danger, or any hex color code (optional).
80+
* `EXIT_ON_FAILURES` - Exit from st2chatops for st2client failures. Default is `false` (optional).
8081

8182
Note: ``ST2_ROUTE`` environment variable mentioned below should only be
8283
specified if you modified the rule which comes with a ``hubot`` pack to use a

scripts/stackstorm.js

Lines changed: 23 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,9 @@ env.ST2_COMMANDS_RELOAD_INTERVAL = parseInt(env.ST2_COMMANDS_RELOAD_INTERVAL ||
7575
// Cap message length to a certain number of characters.
7676
env.ST2_MAX_MESSAGE_LENGTH = parseInt(env.ST2_MAX_MESSAGE_LENGTH || 500, 10);
7777

78+
// Exit from st2chatops for st2client failures
79+
env.EXIT_ON_FAILURES = env.EXIT_ON_FAILURES || null;
80+
7881
// Constants
7982
// Fun human-friendly commands. Use %s for payload output.
8083
var START_MESSAGES = [
@@ -171,7 +174,11 @@ module.exports = function(robot) {
171174
.catch(function (err) {
172175
robot.logger.error('Failed to authenticate: ' + err.message);
173176

174-
throw err;
177+
if (env.EXIT_ON_FAILURES) {
178+
stop({exit: true});
179+
} else {
180+
throw err;
181+
}
175182
});
176183
}
177184

@@ -199,7 +206,7 @@ module.exports = function(robot) {
199206
// handler to manage per adapter message post-ing.
200207
var postDataHandler = postData.getDataPostHandler(robot.adapterName, robot, formatter);
201208

202-
var loadCommands = function() {
209+
var loadCommands = function(exitOnFailure) {
203210
robot.logger.info('Loading commands....');
204211

205212
api.actionAlias.list()
@@ -238,6 +245,9 @@ module.exports = function(robot) {
238245
.catch(function (err) {
239246
var error_msg = 'Failed to retrieve commands from "%s": %s';
240247
robot.logger.error(util.format(error_msg, env.ST2_API_URL, err.message));
248+
if (exitOnFailure) {
249+
stop({exit: true});
250+
}
241251
});
242252
};
243253

@@ -409,32 +419,36 @@ module.exports = function(robot) {
409419
}
410420
});
411421

412-
// Add an interval which tries to re-load the commands
413-
commands_load_interval = setInterval(loadCommands.bind(self), (env.ST2_COMMANDS_RELOAD_INTERVAL * 1000));
414-
415422
// Initial command loading
416-
loadCommands();
423+
loadCommands(env.EXIT_ON_FAILURES);
417424

418425
// Install SIGUSR2 handler which reloads the command
419426
install_sigusr2_handler();
427+
428+
// Add an interval which tries to re-load the commands
429+
commands_load_interval = setInterval(loadCommands.bind(self, false), (env.ST2_COMMANDS_RELOAD_INTERVAL * 1000));
420430
}
421431

422-
function stop() {
432+
function stop(exitFromProcess) {
423433
clearInterval(commands_load_interval);
424434
api.stream.listen().then(function (source) {
425435
source.removeAllListeners();
426436
source.close();
427437
});
438+
439+
if (exitFromProcess.exit) {
440+
process.exit(1)
441+
}
428442
}
429443

430444
function install_sigusr2_handler() {
431445
process.on('SIGUSR2', function() {
432-
loadCommands();
446+
loadCommands(false);
433447
});
434448
}
435449

436450
// Authenticate with StackStorm backend and then call start.
437-
// On a failure to authenticate log the error but do not quit.
451+
// On a failure to authenticate log the error.
438452
return promise.then(function () {
439453
start();
440454
return stop;

0 commit comments

Comments
 (0)