Skip to content

Commit 9e9acc3

Browse files
Merge pull request #219 from roshan04/fix_zip_uploadbar_stuck_issue
Fix zip uploadbar stuck issue on zip upload failure
2 parents 954028a + 6569dff commit 9e9acc3

File tree

2 files changed

+125
-36
lines changed

2 files changed

+125
-36
lines changed

bin/helpers/zipUpload.js

Lines changed: 42 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -10,9 +10,18 @@ const config = require("./config"),
1010
Constants = require("./constants"),
1111
utils = require("./utils");
1212

13-
const uploadSuits = (bsConfig, filePath, opts) => {
13+
14+
const purgeUploadBar = (obj) => {
15+
obj.bar1.update(100, {
16+
speed: ((obj.size / (Date.now() - obj.startTime)) / 125).toFixed(2) //kbits per sec
17+
});
18+
obj.bar1.stop();
19+
clearInterval(obj.zipInterval);
20+
}
21+
22+
const uploadSuits = (bsConfig, filePath, opts, obj) => {
1423
return new Promise(function (resolve, reject) {
15-
let startTime = Date.now();
24+
obj.startTime = Date.now();
1625

1726
if (opts.urlPresent) {
1827
return resolve({ [opts.md5ReturnKey]: opts.url });
@@ -21,28 +30,26 @@ const uploadSuits = (bsConfig, filePath, opts) => {
2130
return resolve({});
2231
}
2332

24-
let size = fs.lstatSync(filePath).size;
33+
let size = obj.size;
2534

2635
// create new progress bar
27-
let bar1 = new cliProgress.SingleBar({
28-
format: `${filePath} [{bar}] {percentage}% | ETA: {eta}s | Speed: {speed} kbps | Duration: {duration}s [${(size / 1000000).toFixed(2)} MB]`,
29-
hideCursor: true,
36+
obj.bar1 = new cliProgress.SingleBar({
37+
format: `${filePath} [{bar}] {percentage}% | ETA: {eta}s | Speed: {speed} kbps | Duration: {duration}s [${(size / 1000000).toFixed(2)} MB]`
3038
});
3139

32-
bar1.start(100, 0, {
40+
obj.bar1.start(100, 0, {
3341
speed: "N/A"
3442
});
3543

36-
bar1.on('start', () => {
44+
obj.bar1.on('start', () => {
3745
});
3846

39-
bar1.on('stop', () => {
47+
obj.bar1.on('stop', () => {
4048
});
4149

4250
let options = utils.generateUploadParams(bsConfig, filePath, opts.md5Data, opts.fileDetails)
4351
let responseData = null;
4452
var r = request.post(options, function (err, resp, body) {
45-
clearInterval(q);
4653

4754
if (err) {
4855
reject(err);
@@ -55,7 +62,7 @@ const uploadSuits = (bsConfig, filePath, opts) => {
5562
if (resp.statusCode != 200) {
5663
if (resp.statusCode == 401) {
5764
if (responseData && responseData["error"]) {
58-
responseData["time"] = Date.now() - startTime;
65+
responseData["time"] = Date.now() - obj.startTime;
5966
return reject(responseData["error"]);
6067
} else {
6168
return reject(Constants.validationMessages.INVALID_DEFAULT_AUTH_PARAMS);
@@ -65,7 +72,7 @@ const uploadSuits = (bsConfig, filePath, opts) => {
6572
return resolve({});
6673
}
6774
if(responseData && responseData["error"]){
68-
responseData["time"] = Date.now() - startTime;
75+
responseData["time"] = Date.now() - obj.startTime;
6976
reject(responseData["error"]);
7077
} else {
7178
if (resp.statusCode == 413) {
@@ -75,30 +82,26 @@ const uploadSuits = (bsConfig, filePath, opts) => {
7582
}
7683
}
7784
} else {
78-
bar1.update(100, {
79-
speed: ((size / (Date.now() - startTime)) / 125).toFixed(2) //kbits per sec
80-
});
81-
bar1.stop();
85+
purgeUploadBar(obj);
8286
logger.info(`${opts.messages.uploadingSuccess} (${responseData[opts.md5ReturnKey]})`);
8387
opts.cleanupMethod();
84-
responseData["time"] = Date.now() - startTime;
88+
responseData["time"] = Date.now() - obj.startTime;
8589
resolve(responseData);
8690
}
8791
}
8892
});
8993

90-
var q = setInterval(function () {
94+
obj.zipInterval = setInterval(function () {
9195
let dispatched = r.req.connection._bytesDispatched;
9296
let percent = dispatched * 100.0 / size;
93-
bar1.update(percent, {
94-
speed: ((dispatched / (Date.now() - startTime)) / 125).toFixed(2) //kbits per sec
97+
obj.bar1.update(percent, {
98+
speed: ((dispatched / (Date.now() - obj.startTime)) / 125).toFixed(2) //kbits per sec
9599
});
96100
}, 150);
97101

98102
});
99103
}
100104

101-
102105
const uploadCypressZip = (bsConfig, md5data, packageData) => {
103106
return new Promise(function (resolve, reject) {
104107
let obj = {}
@@ -116,8 +119,22 @@ const uploadCypressZip = (bsConfig, md5data, packageData) => {
116119
logger.info(npmOptions.messages.uploading);
117120
}
118121

119-
let zipUpload = uploadSuits(bsConfig, config.fileName, zipOptions);
120-
let npmPackageUpload = uploadSuits(bsConfig, config.packageFileName, npmOptions);
122+
var testZipUploadObj = {
123+
bar1: null,
124+
zipInterval: null,
125+
size: fs.existsSync(config.fileName) ? fs.lstatSync(config.fileName).size : 0,
126+
startTime: null
127+
}
128+
129+
var npmPackageZipUploadObj = {
130+
bar1: null,
131+
zipInterval: null,
132+
size: fs.existsSync(config.packageFileName) ? fs.lstatSync(config.packageFileName).size : 0,
133+
startTime: null
134+
}
135+
136+
let zipUpload = uploadSuits(bsConfig, config.fileName, zipOptions, testZipUploadObj);
137+
let npmPackageUpload = uploadSuits(bsConfig, config.packageFileName, npmOptions, npmPackageZipUploadObj);
121138
Promise.all([zipUpload, npmPackageUpload]).then(function (uploads) {
122139
uploads.forEach(upload => {
123140
if(upload.zip_url && upload.time) {
@@ -130,6 +147,8 @@ const uploadCypressZip = (bsConfig, md5data, packageData) => {
130147
});
131148
return resolve(obj);
132149
}).catch((error) => {
150+
purgeUploadBar(testZipUploadObj);
151+
purgeUploadBar(npmPackageZipUploadObj);
133152
return reject(error);
134153
})
135154
})

test/unit/bin/helpers/zipUpload.js

Lines changed: 83 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,13 @@ describe("zipUpload", () => {
7171
archivePresent: true,
7272
messages: {}
7373
}
74-
return uploadSuitsrewire(bsConfig, filePath, opts)
74+
let obj = {
75+
bar1: null,
76+
zipInterval: null,
77+
size: 0,
78+
startTime: null
79+
}
80+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
7581
.then((_data) => {
7682
chai.assert.fail("Promise error");
7783
})
@@ -87,7 +93,13 @@ describe("zipUpload", () => {
8793
md5ReturnKey: 'returnKey',
8894
url: 'bs://random_hash'
8995
}
90-
return uploadSuitsrewire(bsConfig, filePath, opts)
96+
let obj = {
97+
bar1: null,
98+
zipInterval: null,
99+
size: 0,
100+
startTime: null
101+
}
102+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
91103
.then((data) => {
92104
chai.assert.deepEqual(data, {returnKey: 'bs://random_hash'});
93105
})
@@ -101,7 +113,13 @@ describe("zipUpload", () => {
101113
let opts = {
102114
archivePresent: false,
103115
}
104-
return uploadSuitsrewire(bsConfig, filePath, opts)
116+
let obj = {
117+
bar1: null,
118+
zipInterval: null,
119+
size: 0,
120+
startTime: null
121+
}
122+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
105123
.then((data) => {
106124
chai.assert.deepEqual(data, {});
107125
})
@@ -126,7 +144,13 @@ describe("zipUpload", () => {
126144
archivePresent: true,
127145
messages: {}
128146
}
129-
return uploadSuitsrewire(bsConfig, filePath, opts)
147+
let obj = {
148+
bar1: null,
149+
zipInterval: null,
150+
size: 0,
151+
startTime: null
152+
}
153+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
130154
.then((data) => {
131155
chai.assert.hasAllKeys(data, ["time"]);
132156
})
@@ -151,7 +175,13 @@ describe("zipUpload", () => {
151175
archivePresent: true,
152176
messages: {}
153177
}
154-
return uploadSuitsrewire(bsConfig, filePath, opts)
178+
let obj = {
179+
bar1: null,
180+
zipInterval: null,
181+
size: 0,
182+
startTime: null
183+
}
184+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
155185
.then((data) => {
156186
chai.assert.hasAllKeys(data, ["zip_url", "time"]);
157187
})
@@ -175,7 +205,13 @@ describe("zipUpload", () => {
175205
archivePresent: true,
176206
messages: {}
177207
}
178-
return uploadSuitsrewire(bsConfig, filePath, opts)
208+
let obj = {
209+
bar1: null,
210+
zipInterval: null,
211+
size: 0,
212+
startTime: null
213+
}
214+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
179215
.then((_data) => {
180216
chai.assert.fail("Promise error");
181217
})
@@ -199,7 +235,13 @@ describe("zipUpload", () => {
199235
archivePresent: true,
200236
messages: {}
201237
}
202-
return uploadSuitsrewire(bsConfig, filePath, opts)
238+
let obj = {
239+
bar1: null,
240+
zipInterval: null,
241+
size: 0,
242+
startTime: null
243+
}
244+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
203245
.then((_data) => {
204246
chai.assert.fail("Promise error");
205247
})
@@ -224,7 +266,13 @@ describe("zipUpload", () => {
224266
messages: {},
225267
propogateError: false
226268
}
227-
return uploadSuitsrewire(bsConfig, filePath, opts)
269+
let obj = {
270+
bar1: null,
271+
zipInterval: null,
272+
size: 0,
273+
startTime: null
274+
}
275+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
228276
.then((data) => {
229277
chai.assert.deepEqual(data, {});
230278
})
@@ -249,7 +297,13 @@ describe("zipUpload", () => {
249297
messages: {},
250298
propogateError: true
251299
}
252-
return uploadSuitsrewire(bsConfig, filePath, opts)
300+
let obj = {
301+
bar1: null,
302+
zipInterval: null,
303+
size: 0,
304+
startTime: null
305+
}
306+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
253307
.then((_data) => {
254308
chai.assert.fail("Promise error");
255309
})
@@ -274,7 +328,13 @@ describe("zipUpload", () => {
274328
messages: {},
275329
propogateError: true
276330
}
277-
return uploadSuitsrewire(bsConfig, filePath, opts)
331+
let obj = {
332+
bar1: null,
333+
zipInterval: null,
334+
size: 0,
335+
startTime: null
336+
}
337+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
278338
.then((_data) => {
279339
chai.assert.fail("Promise error");
280340
})
@@ -299,7 +359,13 @@ describe("zipUpload", () => {
299359
messages: {},
300360
propogateError: true
301361
}
302-
return uploadSuitsrewire(bsConfig, filePath, opts)
362+
let obj = {
363+
bar1: null,
364+
zipInterval: null,
365+
size: 0,
366+
startTime: null
367+
}
368+
return uploadSuitsrewire(bsConfig, filePath, opts, obj)
303369
.then((_data) => {
304370
chai.assert.fail("Promise error");
305371
})
@@ -320,9 +386,11 @@ describe("zipUpload", () => {
320386
});
321387

322388
it("resolve with test suit", () => {
389+
let purgeUploadBarStub = sandbox.stub().returns(true);
323390
zipUploader.__set__({
324391
utils: utilsStub,
325-
uploadSuits: uploadSuitsStub
392+
uploadSuits: uploadSuitsStub,
393+
purgeUploadBar: purgeUploadBarStub
326394
});
327395
let uploadCypressZiprewire = zipUploader.__get__('uploadCypressZip');
328396
let bsConfig = {}
@@ -340,9 +408,11 @@ describe("zipUpload", () => {
340408

341409
it("reject with error while uploading suit", () => {
342410
let uploadSuitsErrorStub = sandbox.stub().returns(Promise.reject("test error"));
411+
let purgeUploadBarStub = sandbox.stub().returns(true);
343412
zipUploader.__set__({
344413
utils: utilsStub,
345-
uploadSuits: uploadSuitsErrorStub
414+
uploadSuits: uploadSuitsErrorStub,
415+
purgeUploadBar: purgeUploadBarStub
346416
});
347417
let uploadCypressZiprewire = zipUploader.__get__('uploadCypressZip');
348418
let bsConfig = {}

0 commit comments

Comments
 (0)