@@ -170,8 +170,8 @@ module.exports = function(robot) {
170
170
} )
171
171
. catch ( function ( err ) {
172
172
robot . logger . error ( 'Failed to authenticate: ' + err . message ) ;
173
-
174
- throw err ;
173
+ robot . logger . error ( err . stack ) ;
174
+ stop ( { exit : true } ) ;
175
175
} ) ;
176
176
}
177
177
@@ -199,9 +199,11 @@ module.exports = function(robot) {
199
199
// handler to manage per adapter message post-ing.
200
200
var postDataHandler = postData . getDataPostHandler ( robot . adapterName , robot , formatter ) ;
201
201
202
- var loadCommands = function ( ) {
202
+ var loadCommands = function ( opts ) {
203
203
robot . logger . info ( 'Loading commands....' ) ;
204
204
205
+ var opts = Object . assign ( { exitOnFailure : false } , opts ) ;
206
+
205
207
api . actionAlias . list ( )
206
208
. then ( function ( aliases ) {
207
209
// Remove all the existing commands
@@ -238,6 +240,9 @@ module.exports = function(robot) {
238
240
. catch ( function ( err ) {
239
241
var error_msg = 'Failed to retrieve commands from "%s": %s' ;
240
242
robot . logger . error ( util . format ( error_msg , env . ST2_API_URL , err . message ) ) ;
243
+ if ( opts . exitOnFailure ) {
244
+ stop ( { exit : true } ) ;
245
+ }
241
246
} ) ;
242
247
} ;
243
248
@@ -409,22 +414,29 @@ module.exports = function(robot) {
409
414
}
410
415
} ) ;
411
416
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
-
415
417
// 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 ) ) ;
417
422
418
423
// Install SIGUSR2 handler which reloads the command
419
424
install_sigusr2_handler ( ) ;
420
425
}
421
426
422
- function stop ( ) {
427
+ function stop ( opts ) {
428
+ var opts = Object . assign ( { exit : false } , opts ) ;
429
+
423
430
clearInterval ( commands_load_interval ) ;
424
431
api . stream . listen ( ) . then ( function ( source ) {
425
432
source . removeAllListeners ( ) ;
426
433
source . close ( ) ;
427
434
} ) ;
435
+
436
+ if ( opts . exit ) {
437
+ robot . server . close ( ) ;
438
+ robot . shutdown ( ) ;
439
+ }
428
440
}
429
441
430
442
function install_sigusr2_handler ( ) {
@@ -434,7 +446,7 @@ module.exports = function(robot) {
434
446
}
435
447
436
448
// 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.
438
450
return promise . then ( function ( ) {
439
451
start ( ) ;
440
452
return stop ;
0 commit comments