Skip to content

Commit 8f7c74e

Browse files
committed
Exit st2chatop from st2client failures.
Fixes StackStorm/st2chatops#124 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 8f7c74e

File tree

2 files changed

+22
-9
lines changed

2 files changed

+22
-9
lines changed

CHANGELOG.rst

+1
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+
* Allow `st2chatops` to exit from `st2client` failures.
1112

1213
0.9.2
1314
-----

scripts/stackstorm.js

+21-9
Original file line numberDiff line numberDiff line change
@@ -170,8 +170,8 @@ module.exports = function(robot) {
170170
})
171171
.catch(function (err) {
172172
robot.logger.error('Failed to authenticate: ' + err.message);
173-
174-
throw err;
173+
robot.logger.error(err.stack);
174+
stop({exit: true});
175175
});
176176
}
177177

@@ -199,9 +199,11 @@ module.exports = function(robot) {
199199
// handler to manage per adapter message post-ing.
200200
var postDataHandler = postData.getDataPostHandler(robot.adapterName, robot, formatter);
201201

202-
var loadCommands = function() {
202+
var loadCommands = function(opts) {
203203
robot.logger.info('Loading commands....');
204204

205+
var opts = Object.assign({exitOnFailure: false}, opts);
206+
205207
api.actionAlias.list()
206208
.then(function (aliases) {
207209
// Remove all the existing commands
@@ -238,6 +240,9 @@ module.exports = function(robot) {
238240
.catch(function (err) {
239241
var error_msg = 'Failed to retrieve commands from "%s": %s';
240242
robot.logger.error(util.format(error_msg, env.ST2_API_URL, err.message));
243+
if (opts.exitOnFailure) {
244+
stop({exit: true});
245+
}
241246
});
242247
};
243248

@@ -409,22 +414,29 @@ module.exports = function(robot) {
409414
}
410415
});
411416

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-
415417
// Initial command loading
416-
loadCommands();
418+
loadCommands({exitOnFailure: true});
419+
420+
// Add an interval which tries to re-load the commands
421+
commands_load_interval = setInterval(loadCommands.bind(), (env.ST2_COMMANDS_RELOAD_INTERVAL * 1000));
417422

418423
// Install SIGUSR2 handler which reloads the command
419424
install_sigusr2_handler();
420425
}
421426

422-
function stop() {
427+
function stop(opts) {
428+
var opts = Object.assign({exit: false}, opts);
429+
423430
clearInterval(commands_load_interval);
424431
api.stream.listen().then(function (source) {
425432
source.removeAllListeners();
426433
source.close();
427434
});
435+
436+
if (opts.exit) {
437+
robot.server.close();
438+
robot.shutdown();
439+
}
428440
}
429441

430442
function install_sigusr2_handler() {
@@ -434,7 +446,7 @@ module.exports = function(robot) {
434446
}
435447

436448
// Authenticate with StackStorm backend and then call start.
437-
// On a failure to authenticate log the error but do not quit.
449+
// On a failure to authenticate log the error and quit.
438450
return promise.then(function () {
439451
start();
440452
return stop;

0 commit comments

Comments
 (0)