Skip to content

Commit 76889a6

Browse files
hasezoeynodkz
authored andcommitted
fix(MongoInstance): de-duplicate killer code & actually log output from "mongo_killer"
- de-duplicate "MongoInstance.kill" code - add check that "childProcess.pid" is not undefined before returning "MongoInstance._launchMongod" - actually log output from "mongo_killer" in "MongoInstance._launchKiller" - add custom message if "libcurl4" is missing
1 parent 2dd1fae commit 76889a6

File tree

2 files changed

+50
-25
lines changed

2 files changed

+50
-25
lines changed

packages/mongodb-memory-server-core/src/util/MongoInstance.ts

Lines changed: 42 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import {
1111
EmptyVoidCallback,
1212
} from '../types';
1313
import debug from 'debug';
14+
import { isNullOrUndefined } from './db_util';
1415

1516
const 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 (/CURL_OPENSSL_4.*not found/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 (/shutting down with code/i.test(line)) {
268285
// if mongod started succesfully then no error on shutdown!
269286
if (!this.isInstanceReady) {

packages/mongodb-memory-server-core/src/util/db_util.ts

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,4 +23,12 @@ export function getUriBase(host: string, port: number, dbName: string): string {
2323
return `mongodb://${host}:${port}/${dbName}?`;
2424
}
2525

26+
/**
27+
* Because since node 4.0.0 the internal util.is* functions got deprecated
28+
* @param val Any value to test if null or undefined
29+
*/
30+
export function isNullOrUndefined(val: unknown): val is null | undefined {
31+
return val === null || val === undefined;
32+
}
33+
2634
export default generateDbName;

0 commit comments

Comments
 (0)