Skip to content

Commit aa75e42

Browse files
committed
fix(utils): killProcess: use better logging
1 parent 00740ac commit aa75e42

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

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

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -358,13 +358,13 @@ export class MongoInstance extends EventEmitter implements ManagerBase {
358358
}
359359
}
360360

361-
await killProcess(this.mongodProcess, 'mongodProcess');
361+
await killProcess(this.mongodProcess, 'mongodProcess', this.instanceOpts.port);
362362
this.mongodProcess = undefined; // reset reference to the childProcess for "mongod"
363363
} else {
364364
this.debug('stop: mongodProcess: nothing to shutdown, skipping');
365365
}
366366
if (!isNullOrUndefined(this.killerProcess)) {
367-
await killProcess(this.killerProcess, 'killerProcess');
367+
await killProcess(this.killerProcess, 'killerProcess', this.instanceOpts.port);
368368
this.killerProcess = undefined; // reset reference to the childProcess for "mongo_killer"
369369
} else {
370370
this.debug('stop: killerProcess: nothing to shutdown, skipping');

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

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -67,11 +67,20 @@ export function assertion(cond: unknown, error?: Error): asserts cond {
6767
* Kill an ChildProcess
6868
* @param childprocess The Process to kill
6969
* @param name the name used in the logs
70+
* @param mongodPort the port for the mongod process (for easier logging)
7071
*/
71-
export async function killProcess(childprocess: ChildProcess, name: string): Promise<void> {
72+
export async function killProcess(
73+
childprocess: ChildProcess,
74+
name: string,
75+
mongodPort: number | undefined
76+
): Promise<void> {
77+
function ilog(msg: string) {
78+
log(`Mongo[${mongodPort || 'unknown'}] killProcess: ${msg}`);
79+
}
80+
7281
// check if the childProcess (via PID) is still alive (found thanks to https://github.com/nodkz/mongodb-memory-server/issues/411)
7382
if (!isAlive(childprocess.pid)) {
74-
log("killProcess: given childProcess's PID was not alive anymore");
83+
ilog("killProcess: given childProcess's PID was not alive anymore");
7584

7685
return;
7786
}
@@ -82,7 +91,7 @@ export async function killProcess(childprocess: ChildProcess, name: string): Pro
8291
const timeoutTime = 1000 * 10;
8392
await new Promise<void>((res, rej) => {
8493
let timeout = setTimeout(() => {
85-
log('killProcess: timeout triggered, trying SIGKILL');
94+
ilog('killProcess: timeout triggered, trying SIGKILL');
8695

8796
if (!debug.enabled('MongoMS:utils')) {
8897
console.warn(
@@ -93,16 +102,16 @@ export async function killProcess(childprocess: ChildProcess, name: string): Pro
93102

94103
childprocess.kill('SIGKILL');
95104
timeout = setTimeout(() => {
96-
log('killProcess: timeout triggered again, rejecting');
105+
ilog('killProcess: timeout triggered again, rejecting');
97106
rej(new Error(`Process "${name}" didnt exit, enable debug for more information.`));
98107
}, timeoutTime);
99108
}, timeoutTime);
100109
childprocess.once(`exit`, (code, signal) => {
101-
log(`killProcess: ${name}: got exit signal, Code: ${code}, Signal: ${signal}`);
110+
ilog(`killProcess: ${name}: got exit signal, Code: ${code}, Signal: ${signal}`);
102111
clearTimeout(timeout);
103112
res();
104113
});
105-
log(`killProcess: ${name}: sending "SIGINT"`);
114+
ilog(`killProcess: ${name}: sending "SIGINT"`);
106115
childprocess.kill('SIGINT');
107116
});
108117
}

0 commit comments

Comments
 (0)