Skip to content

Commit e293efb

Browse files
committed
fix(MongoBinary): it does not unlock dir if got DB from cache at second time
1 parent 97e1b6c commit e293efb

File tree

1 file changed

+28
-43
lines changed

1 file changed

+28
-43
lines changed

src/util/MongoBinary.js

+28-43
Original file line numberDiff line numberDiff line change
@@ -46,12 +46,15 @@ export default class MongoBinary {
4646
if (this.cache[version]) {
4747
debug(`MongoBinary: found cached binary path for ${version}`);
4848
} else {
49+
// create downloadDir if not exists
4950
await new Promise((resolve, reject) => {
5051
mkdirp(downloadDir, err => {
5152
if (err) reject(err);
5253
else resolve();
5354
});
5455
});
56+
57+
// wait lock
5558
await new Promise((resolve, reject) => {
5659
lockFile.lock(
5760
downloadDir,
@@ -60,52 +63,34 @@ export default class MongoBinary {
6063
// try to get lock every second, give up after 3 minutes
6164
retries: { retries: 180, factor: 1, minTimeout: 1000 },
6265
},
63-
(err, releaseLock) => {
64-
debug('MongoBinary: Download lock created');
65-
66-
// cache may be populated by previous process
67-
// check again
68-
if (this.cache[version]) {
69-
debug(`MongoBinary: found cached binary path for ${version}`);
70-
resolve(this.cache[version]);
71-
}
72-
73-
if (err) {
74-
reject(err);
75-
return;
76-
}
66+
err => {
67+
if (err) reject(err);
68+
else resolve();
69+
}
70+
);
71+
});
7772

78-
const downloader = new MongoDBDownload({
79-
downloadDir,
80-
platform,
81-
arch,
82-
version,
83-
http,
84-
});
73+
// again check cache, maybe other instance resolve it
74+
if (!this.cache[version]) {
75+
const downloader = new MongoDBDownload({
76+
downloadDir,
77+
platform,
78+
arch,
79+
version,
80+
http,
81+
});
8582

86-
downloader.debug = debug;
83+
downloader.debug = debug;
84+
const releaseDir = await downloader.downloadAndExtract();
85+
this.cache[version] = await this.findBinPath(releaseDir);
86+
}
8787

88-
downloader
89-
.downloadAndExtract()
90-
.then(releaseDir => {
91-
releaseLock(e => {
92-
debug(
93-
e
94-
? `MongoBinary: Error when removing download lock ${e}`
95-
: `MongoBinary: Download lock removed`
96-
);
97-
});
98-
return this.findBinPath(releaseDir);
99-
})
100-
.then(binPath => {
101-
this.cache[version] = binPath;
102-
resolve();
103-
})
104-
.catch(e => {
105-
debug(`MongoBinary: Error with mongod binary path: ${e}`);
106-
reject(e);
107-
});
108-
}
88+
// remove lock
89+
lockFile.unlock(downloadDir, err => {
90+
debug(
91+
err
92+
? `MongoBinary: Error when removing download lock ${err}`
93+
: `MongoBinary: Download lock removed`
10994
);
11095
});
11196
}

0 commit comments

Comments
 (0)