Skip to content

Commit

Permalink
fix: Improve debug
Browse files Browse the repository at this point in the history
  • Loading branch information
nodkz committed Jun 7, 2017
1 parent f931025 commit e46ab53
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 26 deletions.
26 changes: 14 additions & 12 deletions src/MongoMemoryServer.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import type { ChildProcess } from 'child_process';
import uuid from 'uuid/v4';
import tmp from 'tmp';
import getport from 'get-port';
import Debug from 'debug';
import MongoInstance from './util/MongoInstance';

tmp.setGracefulCleanup();
Expand All @@ -13,15 +14,15 @@ export type MongoMemoryServerOptsT = {
port?: ?number,
dbPath?: string,
storageEngine?: string,
debug?: boolean,
debug?: boolean | Function,
},
binary: {
version?: string,
downloadDir?: string,
platform?: string,
arch?: string,
http?: any,
debug?: boolean,
debug?: boolean | Function,
},
debug?: boolean,
spawn: any,
Expand Down Expand Up @@ -49,27 +50,26 @@ export default class MongoMemoryServer {
isRunning: boolean = false;
runningInstance: ?Promise<MongoInstanceDataT>;
opts: MongoMemoryServerOptsT;
debug: Function;

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

this.debug = (msg: string) => {
if (this.opts.debug) {
console.log(msg);
}
};

// autoStart by default
if (!opts.hasOwnProperty('autoStart') || opts.autoStart) {
if (opts.debug) {
console.log('Autostarting MongoDB instance...');
}
this.debug('Autostarting MongoDB instance...');
this.start();
}
}

debug(msg: string) {
if (this.opts.debug) {
console.log(msg);
}
}

async start(): Promise<boolean> {
if (this.runningInstance) {
throw new Error(
Expand Down Expand Up @@ -104,6 +104,8 @@ export default class MongoMemoryServer {

const instOpts = this.opts.instance;
data.port = await getport(instOpts.port);
this.debug = Debug(`Mongo[${data.port}]`);
this.debug.enabled = !!this.opts.debug;
data.uri = await generateConnectionString(data.port);
data.storageEngine = instOpts.storageEngine || 'ephemeralForTest';
if (instOpts.dbPath) {
Expand All @@ -126,7 +128,7 @@ export default class MongoMemoryServer {
},
binary: this.opts.binary,
spawn: this.opts.spawn,
debug: this.opts.debug,
debug: this.debug,
});
data.childProcess = childProcess;
data.tmpDir = tmpDir;
Expand Down
47 changes: 38 additions & 9 deletions src/util/MongoBinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ export type MongoBinaryOpts = {
platform?: string,
arch?: string,
http?: any,
debug?: boolean,
debug?: boolean | Function,
};

export default class MongoBinary {
Expand All @@ -32,7 +32,20 @@ export default class MongoBinary {
http = {},
} = opts;

if (!this.cache[version]) {
let debug;
if (opts.debug) {
if (opts.debug.call && typeof opts.debug === 'function' && opts.debug.apply) {
debug = opts.debug;
} else {
debug = console.log.bind(null);
}
} else {
debug = (msg: string) => {}; // eslint-disable-line
}

if (this.cache[version]) {
debug(`MongoBinary: found cached binary path for ${version}`);
} else {
await new Promise((resolve, reject) => {
mkdirp(downloadDir, err => {
if (err) reject(err);
Expand All @@ -48,6 +61,8 @@ export default class MongoBinary {
retries: { retries: 180, factor: 1, minTimeout: 1000 },
},
(err, releaseLock) => {
debug('MongoBinary: Download lock created');

if (err) {
reject(err);
return;
Expand All @@ -61,22 +76,36 @@ export default class MongoBinary {
http,
});

if (opts.debug) {
downloader.debug = console.log.bind(null);
}
downloader.debug = debug;

downloader
.downloadAndExtract()
.then(releaseDir => {
releaseLock();
resolve(this.findBinPath(releaseDir));
releaseLock(e => {
debug(
e
? `MongoBinary: Error when removing download lock ${e}`
: `MongoBinary: Download lock removed`
);
});
return this.findBinPath(releaseDir);
})
.catch(e => reject(e));
.then(binPath => {
resolve(binPath);
})
.catch(e => {
debug(`MongoBinary: Error with mongod binary path: ${e}`);
reject(e);
});
}
);
});
}
return this.cache[version];

return this.cache[version].then(binPath => {
debug(`MongoBinary: Mongod binary path: ${binPath}`);
return binPath;
});
}

static findBinPath(releaseDir: string): Promise<string> {
Expand Down
26 changes: 21 additions & 5 deletions src/util/MongoInstance.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ export type MongodOps = {
port: number,
storageEngine?: string,
dbPath: string,
debug?: boolean,
debug?: boolean | Function,
},

// mongo binary options
Expand All @@ -31,7 +31,7 @@ export type MongodOps = {
shell?: boolean | string,
},

debug?: boolean,
debug?: boolean | Function,
};

export default class MongodbInstance {
Expand Down Expand Up @@ -59,7 +59,19 @@ export default class MongodbInstance {
this.opts.binary.debug = this.opts.debug;
}

this.debug = this.opts.instance && this.opts.instance.debug ? console.log.bind(null) : () => {};
if (this.opts.instance && this.opts.instance.debug) {
if (
this.opts.instance.debug.call &&
typeof this.opts.instance.debug === 'function' &&
this.opts.instance.debug.apply
) {
this.debug = this.opts.instance.debug;
} else {
this.debug = console.log.bind(null);
}
} else {
this.debug = () => {};
}
}

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

async run(): Promise<ChildProcess> {
const launch = new Promise((resolve, reject) => {
this.instanceReady = resolve;
this.instanceReady = () => {
this.debug('MongodbInstance: is ready!');
resolve(this.childProcess);
};
this.instanceFailed = err => {
this.debug(`MongodbInstance: is failed: ${err.toString()}`);
if (this.killerProcess) this.killerProcess.kill();
reject(err);
};
Expand Down Expand Up @@ -131,7 +147,7 @@ export default class MongodbInstance {

const log: string = message.toString();
if (/waiting for connections on port/i.test(log)) {
this.instanceReady(true);
this.instanceReady();
} else if (/addr already in use/i.test(log)) {
this.instanceFailed(`Port ${this.opts.instance.port} already in use`);
} else if (/mongod instance already running/i.test(log)) {
Expand Down

0 comments on commit e46ab53

Please sign in to comment.