Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up
Appearance settings

Commit 1c9a65d

Browse files
Shsgearshsgear
and
shsgear
authored
feat: retry after chunks merge failed (#1)
* feat: retry after merge chunks failed Co-authored-by: shsgear <sunhao@coding.net>
1 parent 950f1a9 commit 1c9a65d

File tree

3 files changed

+50
-8
lines changed

3 files changed

+50
-8
lines changed

‎bin/index.js

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -6,14 +6,18 @@ const chalk = require('chalk');
66
const prompts = require('prompts');
77
const path = require('path');
88
require('winston-daily-rotate-file');
9-
const logger = require('../lib/log');
109
const ProgressBar = require('progress');
10+
const BlueBirdPromise = require("bluebird");
11+
12+
const logger = require('../lib/log');
1113
const { CHUNK_SIZE } = require('../lib/constants');
1214
const { generateAuthorization, getRegistryInfo } = require('../lib/utils');
13-
const { getExistChunks: _getExistChunks, uploadChunk: _uploadChunk, uploadSuccess: _uploadSuccess } = require('../lib/request');
14-
const BlueBirdPromise = require("bluebird");
1515

16+
const { getExistChunks: _getExistChunks, uploadChunk: _uploadChunk, mergeAllChunks: _mergeAllChunks } = require('../lib/request');
17+
18+
const { withRetry } = require('../lib/withRetry');
1619
const argv = require('../lib/argv');
20+
1721
const { requestUrl, version } = getRegistryInfo(argv.registry);
1822

1923
let Authorization = '';
@@ -106,15 +110,23 @@ const upload = async (filePath, parts = []) => {
106110
return;
107111
}
108112

109-
try {
110-
const res = await _uploadSuccess(requestUrl, {
113+
114+
115+
116+
117+
const merge = async () =>
118+
await _mergeAllChunks(requestUrl, {
111119
version,
112120
uploadId,
113121
fileSize,
114122
fileTag: md5
115123
}, {
116124
Authorization
117125
});
126+
127+
128+
try {
129+
const res = await withRetry(merge, 3, 500);
118130
if (res.code) {
119131
throw (res.message);
120132
}

‎lib/request.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -50,15 +50,15 @@ const uploadChunk = (requestUrl, {
5050
}
5151

5252
/**
53-
* 完成分片上传
53+
* 分片上传完成后通知合并所有 chunk
5454
* @param {string} requestUrl
5555
* @param {string} version
5656
* @param {string} uploadId
5757
* @param {string} fileTag
5858
* @param {number} fileSize
5959
* @param {string} Authorization
6060
*/
61-
const uploadSuccess = (requestUrl, {
61+
const mergeAllChunks = (requestUrl, {
6262
version,
6363
uploadId,
6464
fileTag,
@@ -74,5 +74,5 @@ const uploadSuccess = (requestUrl, {
7474
module.exports = {
7575
getExistChunks,
7676
uploadChunk,
77-
uploadSuccess
77+
mergeAllChunks
7878
}

‎lib/withRetry.js

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
const chalk = require('chalk')
2+
3+
4+
const delay = (wait) => new Promise((r) => setTimeout(() => r(), wait))
5+
6+
7+
/**
8+
*
9+
* @param {fucntion} fn the function need to retry after rejected
10+
* @param {Number} retryCount number of retries
11+
* @param {Number} retryDelay delay time between each retries
12+
*/
13+
const withRetry = (fn, retryCount, retryDelay) => new Promise((resolve, reject) =>
14+
fn()
15+
.then(resolve)
16+
.catch((err) => {
17+
if (retryCount > 0) {
18+
console.log(chalk.cyan('遇到了一个小问题,重试中...'))
19+
return delay(retryDelay)
20+
.then(withRetry.bind(null, fn, retryCount - 1, retryDelay))
21+
.then(resolve)
22+
.catch(reject)
23+
}
24+
return reject(err)
25+
})
26+
)
27+
28+
module.exports = {
29+
withRetry
30+
}

0 commit comments

Comments
(0)

AltStyle によって変換されたページ (->オリジナル) /