Skip to content

Commit

Permalink
refactor(sync_worker): only sync request need to sync upstream first
Browse files Browse the repository at this point in the history
  • Loading branch information
fengmk2 committed Nov 6, 2014
1 parent aa9d647 commit 140f5b3
Show file tree
Hide file tree
Showing 6 changed files with 17 additions and 11 deletions.
2 changes: 2 additions & 0 deletions controllers/sync.js
Expand Up @@ -17,6 +17,7 @@
var debug = require('debug')('cnpmjs.org:controllers:sync');
var Log = require('../services/module_log');
var SyncModuleWorker = require('./sync_module_worker');
var config = require('../config');

exports.sync = function* () {
var username = this.user.name || 'anonymous';
Expand All @@ -36,6 +37,7 @@ exports.sync = function* () {
var options = {
publish: publish,
noDep: noDep,
syncUpstreamFirst: config.sourceNpmRegistryIsCNpm,
};

var logId = yield* SyncModuleWorker.sync(name, username, options);
Expand Down
3 changes: 2 additions & 1 deletion controllers/sync_module_worker.js
Expand Up @@ -55,6 +55,7 @@ function SyncModuleWorker(options) {
this.username = options.username;
this.concurrency = options.concurrency || 1;
this._publish = options.publish === true; // _publish_on_cnpm
this.syncUpstreamFirst = options.syncUpstreamFirst;

this.syncingNames = {};
this.nameMap = {};
Expand Down Expand Up @@ -128,7 +129,7 @@ SyncModuleWorker.prototype.start = function () {
var that = this;
co(function *() {
// sync upstream
if (config.sourceNpmRegistryIsCNpm) {
if (that.syncUpstreamFirst) {
try {
yield* that.syncUpstream(that.startName);
} catch (err) {
Expand Down
16 changes: 8 additions & 8 deletions sync/index.js
Expand Up @@ -52,9 +52,14 @@ co(function* () {
}
});

var syncInterval = ms(config.syncInterval);
var minSyncInterval = ms('5m');
if (!syncInterval || syncInterval < minSyncInterval) {
syncInterval = minSyncInterval;
}

// the same time only sync once
var syncing = false;

var handleSync = co(function* () {
debug('mode: %s, syncing: %s', config.syncModel, syncing);
if (!syncing) {
Expand All @@ -79,11 +84,6 @@ var handleSync = co(function* () {

if (sync) {
handleSync();
var syncInterval = ms(config.syncInterval);
var minSyncInterval = ms('5m');
if (!syncInterval || syncInterval < minSyncInterval) {
syncInterval = minSyncInterval;
}
setInterval(handleSync, syncInterval);
}

Expand Down Expand Up @@ -116,7 +116,7 @@ var syncDist = co(function* () {

if (config.syncDist) {
syncDist();
setInterval(syncDist, ms(config.syncInterval));
setInterval(syncDist, syncInterval);
} else {
logger.syncInfo('sync dist disable');
}
Expand Down Expand Up @@ -149,7 +149,7 @@ var syncPythonDist = co(function* () {

if (config.syncPythonDist) {
syncPythonDist();
setInterval(syncPythonDist, ms(config.syncInterval));
setInterval(syncPythonDist, syncInterval);
} else {
logger.syncInfo('sync python dist disable');
}
Expand Down
1 change: 1 addition & 0 deletions sync/sync_all.js
Expand Up @@ -81,6 +81,7 @@ module.exports = function* sync() {
name: packages,
noDep: true,
concurrency: config.syncConcurrency,
syncUpstreamFirst: false,
});
Status.init({need: packages.length}, worker);
worker.start();
Expand Down
3 changes: 2 additions & 1 deletion sync/sync_exist.js
Expand Up @@ -88,7 +88,8 @@ module.exports = function* sync() {
var worker = new SyncModuleWorker({
username: 'admin',
name: packages,
concurrency: config.syncConcurrency
concurrency: config.syncConcurrency,
syncUpstreamFirst: false,
});
Status.init({need: packages.length}, worker);
worker.start();
Expand Down
3 changes: 2 additions & 1 deletion sync/sync_popular.js
Expand Up @@ -27,7 +27,8 @@ module.exports = function* syncPopular() {
var worker = new SyncModuleWorker({
username: 'admin',
name: packages,
concurrency: config.syncConcurrency
concurrency: config.syncConcurrency,
syncUpstreamFirst: false,
});

Status.init({need: packages.length}, worker);
Expand Down

0 comments on commit 140f5b3

Please sign in to comment.