Skip to content

Commit

Permalink
fix(MongoBinary): it does not unlock dir if got DB from cache at seco…
Browse files Browse the repository at this point in the history
…nd time
  • Loading branch information
nodkz committed Jun 7, 2017
1 parent 97e1b6c commit e293efb
Showing 1 changed file with 28 additions and 43 deletions.
71 changes: 28 additions & 43 deletions src/util/MongoBinary.js
Original file line number Diff line number Diff line change
Expand Up @@ -46,12 +46,15 @@ export default class MongoBinary {
if (this.cache[version]) {
debug(`MongoBinary: found cached binary path for ${version}`);
} else {
// create downloadDir if not exists
await new Promise((resolve, reject) => {
mkdirp(downloadDir, err => {
if (err) reject(err);
else resolve();
});
});

// wait lock
await new Promise((resolve, reject) => {
lockFile.lock(
downloadDir,
Expand All @@ -60,52 +63,34 @@ export default class MongoBinary {
// try to get lock every second, give up after 3 minutes
retries: { retries: 180, factor: 1, minTimeout: 1000 },
},
(err, releaseLock) => {
debug('MongoBinary: Download lock created');

// cache may be populated by previous process
// check again
if (this.cache[version]) {
debug(`MongoBinary: found cached binary path for ${version}`);
resolve(this.cache[version]);
}

if (err) {
reject(err);
return;
}
err => {
if (err) reject(err);
else resolve();
}
);
});

const downloader = new MongoDBDownload({
downloadDir,
platform,
arch,
version,
http,
});
// again check cache, maybe other instance resolve it
if (!this.cache[version]) {
const downloader = new MongoDBDownload({
downloadDir,
platform,
arch,
version,
http,
});

downloader.debug = debug;
downloader.debug = debug;
const releaseDir = await downloader.downloadAndExtract();
this.cache[version] = await this.findBinPath(releaseDir);
}

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

0 comments on commit e293efb

Please sign in to comment.