Skip to content
This repository has been archived by the owner on Jun 2, 2024. It is now read-only.

Commit

Permalink
feat: retry sync fail on cnpm registry (#1547)
Browse files Browse the repository at this point in the history
fengmk2 authored Jan 13, 2020

Unverified

This commit is not signed, but one or more authors requires that any commit attributed to them is signed.
1 parent f48f27a commit a8ff647
Showing 2 changed files with 19 additions and 6 deletions.
1 change: 1 addition & 0 deletions config/index.js
Original file line number Diff line number Diff line change
@@ -193,6 +193,7 @@ var config = {
// please don't change it if not necessary
officialNpmRegistry: 'https://registry.npmjs.com',
officialNpmReplicate: 'https://replicate.npmjs.com',
cnpmRegistry: 'https://r.cnpmjs.com',

// sync source, upstream registry
// If you want to directly sync from official npm's registry
24 changes: 18 additions & 6 deletions controllers/sync_module_worker.js
Original file line number Diff line number Diff line change
@@ -322,7 +322,8 @@ SyncModuleWorker.prototype.next = function* (concurrencyId) {
yield this.syncByName(concurrencyId, name, registry);
};

SyncModuleWorker.prototype.syncByName = function* (concurrencyId, name, registry) {
SyncModuleWorker.prototype.syncByName = function* (concurrencyId, name, registry, retryCount) {
retryCount = retryCount || 0;
var that = this;
that.syncingNames[name] = true;
var pkg = null;
@@ -373,17 +374,28 @@ SyncModuleWorker.prototype.syncByName = function* (concurrencyId, name, registry
// if 404
if (!err.res || err.res.statusCode !== 404) {
var errMessage = err.name + ': ' + err.message;
that.log('[c#%s] [error] [%s] get package(%s%s) error: %s, status: %s',
concurrencyId, name, registry, packageUrl, errMessage, status);
// replicate request error, try to request from official registry
if (registry !== config.officialNpmReplicate) {
that.log('[c#%s] [error] [%s] get package(%s%s) error: %s, status: %s, retryCount: %s',
concurrencyId, name, registry, packageUrl, errMessage, status, retryCount);

// retry from cnpmRegistry again, max 3 times
if (registry === config.cnpmRegistry && retryCount < 3) {
this.log('[c#%d] [%s] retry from %s after 3s, retryCount: %s',
concurrencyId, name, registry, retryCount);
yield sleep(3000);
yield that.syncByName(concurrencyId, name, registry, retryCount + 1);
return;
}

// replicate/cnpmRegistry request error, try to request from official registry
if (registry !== config.officialNpmReplicate && registry !== config.cnpmRegistry) {
// sync fail
yield that._doneOne(concurrencyId, name, false);
return;
}

// retry from officialNpmRegistry when officialNpmReplicate fail
this.log('[c#%d] [%s] retry from %s', concurrencyId, name, config.officialNpmRegistry);
this.log('[c#%d] [%s] retry from %s, retryCount: %s',
concurrencyId, name, config.officialNpmRegistry, retryCount);
try {
var result = yield npmSerivce.request(packageUrl, { registry: config.officialNpmRegistry });
pkg = result.data;

0 comments on commit a8ff647

Please sign in to comment.