@@ -46,12 +46,15 @@ export default class MongoBinary {
46
46
if ( this . cache [ version ] ) {
47
47
debug ( `MongoBinary: found cached binary path for ${ version } ` ) ;
48
48
} else {
49
+ // create downloadDir if not exists
49
50
await new Promise ( ( resolve , reject ) => {
50
51
mkdirp ( downloadDir, err => {
51
52
if ( err ) reject ( err ) ;
52
53
else resolve( ) ;
53
54
} ) ;
54
55
} ) ;
56
+
57
+ // wait lock
55
58
await new Promise ( ( resolve, reject ) => {
56
59
lockFile . lock (
57
60
downloadDir ,
@@ -60,52 +63,34 @@ export default class MongoBinary {
60
63
// try to get lock every second, give up after 3 minutes
61
64
retries : { retries : 180 , factor : 1 , minTimeout : 1000 } ,
62
65
} ,
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
+ } ) ;
77
72
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
+ } ) ;
85
82
86
- downloader . debug = debug ;
83
+ downloader . debug = debug ;
84
+ const releaseDir = await downloader . downloadAndExtract ( ) ;
85
+ this . cache [ version ] = await this . findBinPath ( releaseDir ) ;
86
+ }
87
87
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`
109
94
) ;
110
95
} ) ;
111
96
}
0 commit comments