Skip to content

Commit e46ab53

Browse files
committed
fix: Improve debug
1 parent f931025 commit e46ab53

File tree

3 files changed

+73
-26
lines changed

3 files changed

+73
-26
lines changed

src/MongoMemoryServer.js

+14-12
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import type { ChildProcess } from 'child_process';
44
import uuid from 'uuid/v4';
55
import tmp from 'tmp';
66
import getport from 'get-port';
7+
import Debug from 'debug';
78
import MongoInstance from './util/MongoInstance';
89

910
tmp.setGracefulCleanup();
@@ -13,15 +14,15 @@ export type MongoMemoryServerOptsT = {
1314
port?: ?number,
1415
dbPath?: string,
1516
storageEngine?: string,
16-
debug?: boolean,
17+
debug?: boolean | Function,
1718
},
1819
binary: {
1920
version?: string,
2021
downloadDir?: string,
2122
platform?: string,
2223
arch?: string,
2324
http?: any,
24-
debug?: boolean,
25+
debug?: boolean | Function,
2526
},
2627
debug?: boolean,
2728
spawn: any,
@@ -49,27 +50,26 @@ export default class MongoMemoryServer {
4950
isRunning: boolean = false;
5051
runningInstance: ?Promise<MongoInstanceDataT>;
5152
opts: MongoMemoryServerOptsT;
53+
debug: Function;
5254

5355
constructor(opts?: $Shape<MongoMemoryServerOptsT> = {}) {
5456
this.opts = opts;
5557
if (!this.opts.instance) this.opts.instance = {};
5658
if (!this.opts.binary) this.opts.binary = {};
5759

60+
this.debug = (msg: string) => {
61+
if (this.opts.debug) {
62+
console.log(msg);
63+
}
64+
};
65+
5866
// autoStart by default
5967
if (!opts.hasOwnProperty('autoStart') || opts.autoStart) {
60-
if (opts.debug) {
61-
console.log('Autostarting MongoDB instance...');
62-
}
68+
this.debug('Autostarting MongoDB instance...');
6369
this.start();
6470
}
6571
}
6672

67-
debug(msg: string) {
68-
if (this.opts.debug) {
69-
console.log(msg);
70-
}
71-
}
72-
7373
async start(): Promise<boolean> {
7474
if (this.runningInstance) {
7575
throw new Error(
@@ -104,6 +104,8 @@ export default class MongoMemoryServer {
104104

105105
const instOpts = this.opts.instance;
106106
data.port = await getport(instOpts.port);
107+
this.debug = Debug(`Mongo[${data.port}]`);
108+
this.debug.enabled = !!this.opts.debug;
107109
data.uri = await generateConnectionString(data.port);
108110
data.storageEngine = instOpts.storageEngine || 'ephemeralForTest';
109111
if (instOpts.dbPath) {
@@ -126,7 +128,7 @@ export default class MongoMemoryServer {
126128
},
127129
binary: this.opts.binary,
128130
spawn: this.opts.spawn,
129-
debug: this.opts.debug,
131+
debug: this.debug,
130132
});
131133
data.childProcess = childProcess;
132134
data.tmpDir = tmpDir;

src/util/MongoBinary.js

+38-9
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ export type MongoBinaryOpts = {
1717
platform?: string,
1818
arch?: string,
1919
http?: any,
20-
debug?: boolean,
20+
debug?: boolean | Function,
2121
};
2222

2323
export default class MongoBinary {
@@ -32,7 +32,20 @@ export default class MongoBinary {
3232
http = {},
3333
} = opts;
3434

35-
if (!this.cache[version]) {
35+
let debug;
36+
if (opts.debug) {
37+
if (opts.debug.call && typeof opts.debug === 'function' && opts.debug.apply) {
38+
debug = opts.debug;
39+
} else {
40+
debug = console.log.bind(null);
41+
}
42+
} else {
43+
debug = (msg: string) => {}; // eslint-disable-line
44+
}
45+
46+
if (this.cache[version]) {
47+
debug(`MongoBinary: found cached binary path for ${version}`);
48+
} else {
3649
await new Promise((resolve, reject) => {
3750
mkdirp(downloadDir, err => {
3851
if (err) reject(err);
@@ -48,6 +61,8 @@ export default class MongoBinary {
4861
retries: { retries: 180, factor: 1, minTimeout: 1000 },
4962
},
5063
(err, releaseLock) => {
64+
debug('MongoBinary: Download lock created');
65+
5166
if (err) {
5267
reject(err);
5368
return;
@@ -61,22 +76,36 @@ export default class MongoBinary {
6176
http,
6277
});
6378

64-
if (opts.debug) {
65-
downloader.debug = console.log.bind(null);
66-
}
79+
downloader.debug = debug;
6780

6881
downloader
6982
.downloadAndExtract()
7083
.then(releaseDir => {
71-
releaseLock();
72-
resolve(this.findBinPath(releaseDir));
84+
releaseLock(e => {
85+
debug(
86+
e
87+
? `MongoBinary: Error when removing download lock ${e}`
88+
: `MongoBinary: Download lock removed`
89+
);
90+
});
91+
return this.findBinPath(releaseDir);
7392
})
74-
.catch(e => reject(e));
93+
.then(binPath => {
94+
resolve(binPath);
95+
})
96+
.catch(e => {
97+
debug(`MongoBinary: Error with mongod binary path: ${e}`);
98+
reject(e);
99+
});
75100
}
76101
);
77102
});
78103
}
79-
return this.cache[version];
104+
105+
return this.cache[version].then(binPath => {
106+
debug(`MongoBinary: Mongod binary path: ${binPath}`);
107+
return binPath;
108+
});
80109
}
81110

82111
static findBinPath(releaseDir: string): Promise<string> {

src/util/MongoInstance.js

+21-5
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export type MongodOps = {
1313
port: number,
1414
storageEngine?: string,
1515
dbPath: string,
16-
debug?: boolean,
16+
debug?: boolean | Function,
1717
},
1818

1919
// mongo binary options
@@ -31,7 +31,7 @@ export type MongodOps = {
3131
shell?: boolean | string,
3232
},
3333

34-
debug?: boolean,
34+
debug?: boolean | Function,
3535
};
3636

3737
export default class MongodbInstance {
@@ -59,7 +59,19 @@ export default class MongodbInstance {
5959
this.opts.binary.debug = this.opts.debug;
6060
}
6161

62-
this.debug = this.opts.instance && this.opts.instance.debug ? console.log.bind(null) : () => {};
62+
if (this.opts.instance && this.opts.instance.debug) {
63+
if (
64+
this.opts.instance.debug.call &&
65+
typeof this.opts.instance.debug === 'function' &&
66+
this.opts.instance.debug.apply
67+
) {
68+
this.debug = this.opts.instance.debug;
69+
} else {
70+
this.debug = console.log.bind(null);
71+
}
72+
} else {
73+
this.debug = () => {};
74+
}
6375
}
6476

6577
prepareCommandArgs(): string[] {
@@ -76,8 +88,12 @@ export default class MongodbInstance {
7688

7789
async run(): Promise<ChildProcess> {
7890
const launch = new Promise((resolve, reject) => {
79-
this.instanceReady = resolve;
91+
this.instanceReady = () => {
92+
this.debug('MongodbInstance: is ready!');
93+
resolve(this.childProcess);
94+
};
8095
this.instanceFailed = err => {
96+
this.debug(`MongodbInstance: is failed: ${err.toString()}`);
8197
if (this.killerProcess) this.killerProcess.kill();
8298
reject(err);
8399
};
@@ -131,7 +147,7 @@ export default class MongodbInstance {
131147

132148
const log: string = message.toString();
133149
if (/waiting for connections on port/i.test(log)) {
134-
this.instanceReady(true);
150+
this.instanceReady();
135151
} else if (/addr already in use/i.test(log)) {
136152
this.instanceFailed(`Port ${this.opts.instance.port} already in use`);
137153
} else if (/mongod instance already running/i.test(log)) {

0 commit comments

Comments
 (0)