@@ -11,6 +11,7 @@ import {
1111 EmptyVoidCallback ,
1212} from '../types' ;
1313import debug from 'debug' ;
14+ import { isNullOrUndefined } from './db_util' ;
1415
1516const log = debug ( 'MongoMS:MongoInstance' ) ;
1617
@@ -123,31 +124,31 @@ export default class MongoInstance {
123124
124125 async kill ( ) : Promise < MongoInstance > {
125126 this . debug ( 'Called MongoInstance.kill():' ) ;
126- if ( this . childProcess && ! this . childProcess . killed ) {
127+
128+ /**
129+ * Function to De-Duplicate Code
130+ * @param process The Process to kill
131+ * @param name the name used in the logs
132+ * @param debug the debug function
133+ */
134+ async function kill_internal ( process : ChildProcess , name : string , debug : DebugFn ) {
127135 await new Promise ( ( resolve ) => {
128- if ( this . childProcess ) {
129- this . childProcess . once ( `exit` , ( ) => {
130- this . debug ( ' - childProcess: got exit signal. Ok!' ) ;
131- resolve ( ) ;
132- } ) ;
133- this . childProcess . kill ( ) ;
134- this . debug ( ' - childProcess: send kill cmd...' ) ;
135- }
136+ process . once ( `exit` , ( ) => {
137+ debug ( ` - ${ name } : got exit signal. Ok!` ) ;
138+ resolve ( ) ;
139+ } ) ;
140+ debug ( ` - ${ name } : send kill cmd...` ) ;
141+ process . kill ( 'SIGINT' ) ;
136142 } ) ;
143+ }
144+
145+ if ( this . childProcess && ! this . childProcess . killed ) {
146+ await kill_internal ( this . childProcess , 'childProcess' , this . debug ) ;
137147 } else {
138148 this . debug ( ' - childProcess: nothing to kill, skipping.' ) ;
139149 }
140150 if ( this . killerProcess && ! this . killerProcess . killed ) {
141- await new Promise ( ( resolve ) => {
142- if ( this . killerProcess ) {
143- this . killerProcess . once ( `exit` , ( ) => {
144- this . debug ( ' - killerProcess: got exit signal. Ok!' ) ;
145- resolve ( ) ;
146- } ) ;
147- this . killerProcess . kill ( ) ;
148- this . debug ( ' - killerProcess: send kill cmd...' ) ;
149- }
150- } ) ;
151+ await kill_internal ( this . killerProcess , 'killerProcess' , this . debug ) ;
151152 } else {
152153 this . debug ( ' - killerProcess: nothing to kill, skipping.' ) ;
153154 }
@@ -182,15 +183,15 @@ export default class MongoInstance {
182183 if ( ! spawnOpts . stdio ) spawnOpts . stdio = 'pipe' ;
183184
184185 const childProcess = spawnChild ( mongoBin , this . prepareCommandArgs ( ) , spawnOpts ) ;
185- if ( childProcess . stderr ) {
186- childProcess . stderr . on ( 'data' , this . stderrHandler . bind ( this ) ) ;
187- }
188- if ( childProcess . stdout ) {
189- childProcess . stdout . on ( 'data' , this . stdoutHandler . bind ( this ) ) ;
190- }
186+ childProcess . stderr ?. on ( 'data' , this . stderrHandler . bind ( this ) ) ;
187+ childProcess . stdout ?. on ( 'data' , this . stdoutHandler . bind ( this ) ) ;
191188 childProcess . on ( 'close' , this . closeHandler . bind ( this ) ) ;
192189 childProcess . on ( 'error' , this . errorHandler . bind ( this ) ) ;
193190
191+ if ( isNullOrUndefined ( childProcess . pid ) ) {
192+ throw new Error ( 'Spawned Mongo Instance PID is undefined' ) ;
193+ }
194+
194195 return childProcess ;
195196 }
196197
@@ -212,6 +213,14 @@ export default class MongoInstance {
212213 { stdio : 'pipe' }
213214 ) ;
214215
216+ killer . stdout ?. on ( 'data' , ( data ) => {
217+ this . debug ( `[MongoKiller]: ${ data } ` ) ;
218+ } ) ;
219+
220+ killer . stderr ?. on ( 'data' , ( data ) => {
221+ this . debug ( `[MongoKiller]: ${ data } ` ) ;
222+ } ) ;
223+
215224 [ 'exit' , 'message' , 'disconnect' , 'error' ] . forEach ( ( type ) => {
216225 killer . on ( type , ( ...args ) => {
217226 this . debug ( `[MongoKiller]: ${ type } - ${ JSON . stringify ( args ) } ` ) ;
@@ -230,6 +239,9 @@ export default class MongoInstance {
230239 * @param code The Exit code
231240 */
232241 closeHandler ( code : number ) : void {
242+ if ( code != 0 ) {
243+ this . debug ( 'Mongod instance closed with an non-0 code!' ) ;
244+ }
233245 this . debug ( `CLOSE: ${ code } ` ) ;
234246 }
235247
@@ -264,6 +276,11 @@ export default class MongoInstance {
264276 'libcurl3 is not available on your system. Mongod requires it and cannot be started without it.\n' +
265277 'You should manually install libcurl3 or try to use an newer version of MongoDB\n'
266278 ) ;
279+ } else if ( / C U R L _ O P E N S S L _ 4 .* n o t f o u n d / i. test ( line ) ) {
280+ this . instanceFailed (
281+ 'libcurl4 is not available on your system. Mongod requires it and cannot be started without it.\n' +
282+ 'You need to manually install libcurl4\n'
283+ ) ;
267284 } else if ( / s h u t t i n g d o w n w i t h c o d e / i. test ( line ) ) {
268285 // if mongod started succesfully then no error on shutdown!
269286 if ( ! this . isInstanceReady ) {
0 commit comments