@@ -690,7 +690,7 @@ ProcessProxy.prototype._initAutoInvalidation = function() {
690690 var self = this ;
691691
692692 // the below will run on an interval
693- setInterval ( function ( ) {
693+ this . _autoInvalidationInterval = setInterval ( function ( ) {
694694
695695 // #1 build list of commands
696696 var commandsToExec = [ ] ;
@@ -796,14 +796,14 @@ ProcessProxy.prototype.executeCommand = function(command) {
796796
797797 self . executeCommands ( [ command ] )
798798
799- . then ( function ( cmdResults ) {
799+ . then ( function ( cmdResults ) {
800800
801- fulfill ( cmdResults [ 0 ] ) ;
801+ fulfill ( cmdResults [ 0 ] ) ;
802802
803803
804- } ) . catch ( function ( error ) {
805- reject ( error ) ;
806- } ) ;
804+ } ) . catch ( function ( error ) {
805+ reject ( error ) ;
806+ } ) ;
807807
808808 } ) ;
809809
@@ -907,7 +907,7 @@ ProcessProxy.prototype.executeCommands = function(commands) {
907907 // write the command, followed by this echo
908908 // marker so we know that the command is done
909909 self . _process . stdin . write ( command + '\n' +
910- 'echo ' + MARKER_DONE + '\n' ) ;
910+ 'echo ' + MARKER_DONE + '\n' ) ;
911911
912912
913913 }
@@ -920,6 +920,28 @@ ProcessProxy.prototype.executeCommands = function(commands) {
920920
921921} ;
922922
923+ /**
924+ * Called by shutdown to do the actual destruction of
925+ * this object
926+ *
927+ */
928+ ProcessProxy . prototype . _destroySelf = function ( ) {
929+ try {
930+ if ( this . _autoInvalidationConfig ) {
931+ clearInterval ( this . _autoInvalidationInterval ) ;
932+ }
933+ } catch ( error ) {
934+ this . _log ( 'error' , 'shutdown - error cleaning _autoInvalidationInterval..' + error ) ;
935+ }
936+
937+ try { this . _process . stdin . end ( ) ; } catch ( error ) {
938+ this . _log ( 'error' , 'shutdown - error _process.stdin.end()..' + error ) ;
939+ }
940+ try { this . _process . kill ( ) ; } catch ( error ) {
941+ this . _log ( 'error' , 'shutdown - error _process.kill()..' + error ) ;
942+ }
943+ }
944+
923945/**
924946* shutdown() - shuts down the ProcessProxy w/ optional shutdown commands
925947* and returns a Promise, when fulfilled contains the results
@@ -945,34 +967,30 @@ ProcessProxy.prototype.shutdown = function(shutdownCommands) {
945967
946968 self . _executeCommands ( shutdownCommands , false ) // skip black/whitelists
947969
948- . then ( function ( cmdResults ) {
970+ . then ( function ( cmdResults ) {
949971
950- self . _process . stdin . end ( ) ;
951- self . _process . kill ( ) ;
972+ self . _destroySelf ( ) ;
973+ fulfill ( cmdResults ) ; // invoke when done!
952974
953- fulfill ( cmdResults ) ; // invoke when done!
975+ } ) . catch ( function ( exception ) {
976+ self . _log ( 'error' , "shutdown - shutdownCommands, " +
977+ " exception thrown: " + exception ) ;
954978
955- } ) . catch ( function ( exception ) {
956- self . _log ( 'error' , "shutdown - shutdownCommands, " +
957- " exception thrown: " + exception ) ;
958- self . _process . stdin . end ( ) ;
959- self . _process . kill ( ) ;
960- reject ( exception ) ;
961- } ) ;
979+ self . _destroySelf ( ) ;
980+ reject ( exception ) ;
981+ } ) ;
962982
963983
964- // we are done, no init commands to run...
984+ // we are done, no shutdown commands to run...
965985 } else {
966- self . _process . stdin . end ( ) ;
967- self . _process . kill ( ) ;
986+ self . _destroySelf ( ) ;
968987 fulfill ( null ) ;
969988 }
970989
971990
972991 } catch ( exception ) {
973992 self . _log ( 'error' , "shutdown, exception thrown: " + exception ) ;
974- self . _process . stdin . end ( ) ;
975- self . _process . kill ( ) ;
993+ self . _destroySelf ( ) ;
976994 reject ( exception ) ;
977995 }
978996 } ) ;
0 commit comments