Skip to content

Commit 95f6acf

Browse files
Karan NagpalKaran Nagpal
authored andcommitted
add sleep before downloading artifacts
1 parent e14a9ff commit 95f6acf

File tree

2 files changed

+49
-23
lines changed

2 files changed

+49
-23
lines changed

bin/commands/runs.js

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,9 @@ module.exports = function run(args) {
132132
// stop the Local instance
133133
await utils.stopLocalBinary(bsConfig, bs_local, args);
134134

135+
// waiting for 5 secs for upload to complete (as a safety measure)
136+
await new Promise(resolve => setTimeout(resolve, 5000));
137+
135138
// download build artifacts
136139
await downloadBuildArtifacts(bsConfig, data.build_id, args);
137140

bin/helpers/buildArtifacts.js

Lines changed: 46 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
'use strict';
22

33
const fs = require('fs'),
4-
path = require('path'),
5-
https = require('https');
4+
path = require('path');
65

76
const axios = require('axios'),
87
unzipper = require('unzipper');
@@ -86,31 +85,48 @@ const createDirectories = async (buildId, data) => {
8685
}
8786

8887
const downloadAndUnzip = async (filePath, fileName, url) => {
89-
return new Promise(async (resolve, reject) => {
90-
let tmpFilePath = path.join(filePath, fileName);
91-
https.get(url, function(response) {
92-
response.on('data', function (data) {
93-
fs.appendFileSync(tmpFilePath, data);
88+
let tmpFilePath = path.join(filePath, fileName);
89+
const writer = fs.createWriteStream(tmpFilePath);
90+
91+
return axios({
92+
method: 'get',
93+
url: url,
94+
responseType: 'stream',
95+
}).then(response => {
96+
97+
//ensure that the user can call `then()` only when the file has
98+
//been downloaded entirely.
99+
100+
return new Promise(async (resolve, reject) => {
101+
response.data.pipe(writer);
102+
let error = null;
103+
writer.on('error', err => {
104+
error = err;
105+
writer.close();
106+
reject(err);
94107
});
95-
response.on('end', function() {
96-
fs.createReadStream(tmpFilePath).pipe(unzipper.Extract({ path: filePath })
97-
.on('close', function () {
98-
fs.unlinkSync(tmpFilePath);
99-
resolve();
100-
})
101-
.on('error', function(err) {
102-
process.env.BUILD_ARTIFACTS_FAIL_COUNT = Number(process.env.BUILD_ARTIFACTS_FAIL_COUNT) + 1;
103-
reject(err);
104-
})
105-
);
108+
writer.on('close', async () => {
109+
if (!error) {
110+
await unzipFile(filePath, fileName);
111+
fs.unlinkSync(tmpFilePath);
112+
resolve(true);
113+
}
114+
//no need to call the reject here, as it will have been called in the
115+
//'error' stream;
106116
});
107-
response.on('error', function () {
108-
reject();
109-
})
110117
});
111118
});
112119
}
113120

121+
const unzipFile = async (filePath, fileName) => {
122+
return new Promise( async (resolve, reject) => {
123+
await unzipper.Open.file(path.join(filePath, fileName))
124+
.then(d => d.extract({path: filePath, concurrency: 5}))
125+
.catch((err) => reject(err));
126+
resolve();
127+
});
128+
}
129+
114130
const sendUpdatesToBstack = async (bsConfig, buildId, args, options) => {
115131
let url = `${config.buildUrl}${buildId}/build_artifacts/status`;
116132

@@ -182,8 +198,15 @@ exports.downloadBuildArtifacts = async (bsConfig, buildId, args) => {
182198
messageType = Constants.messageTypes.ERROR;
183199
errorCode = 'api_failed_build_artifacts';
184200

185-
logger.error('Downloading the build artifacts failed.');
186-
logger.error(err);
201+
if (process.env.BUILD_ARTIFACTS_FAIL_COUNT > 0) {
202+
messageType = Constants.messageTypes.ERROR;
203+
message = Constants.userMessages.DOWNLOAD_BUILD_ARTIFACTS_FAILED.replace('<build-id>', buildId).replace('<machine-count>', process.env.BUILD_ARTIFACTS_FAIL_COUNT);
204+
logger.error(message);
205+
} else {
206+
logger.error('Downloading the build artifacts failed.');
207+
logger.error(err);
208+
}
209+
187210
utils.sendUsageReport(bsConfig, args, err, messageType, errorCode);
188211
}
189212
};

0 commit comments

Comments
 (0)