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

add sync popular modules, close #468 #472

Merged
merged 2 commits into from
Oct 13, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 8 additions & 0 deletions config/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -207,6 +207,14 @@ var config = {
// sync interval, default is 10 minutes
syncInterval: '10m',

// sync polular modules, default to false
// because cnpm can't auto sync tag change for now
// so we want to sync popular modules to ensure their tags
syncPopular: false,
syncPopularInterval: '1h',
// top 100
topPopular: 100,

// sync devDependencies or not, default is false
syncDevDependencies: false,

Expand Down
1 change: 0 additions & 1 deletion controllers/registry/module.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,6 @@ var coWrite = require('co-write');
var urlparse = require('url').parse;
var mime = require('mime');
var semver = require('semver');
var ms = require('ms');
var config = require('../../config');
var Module = require('../../proxy/module');
var Total = require('../../proxy/total');
Expand Down
2 changes: 1 addition & 1 deletion controllers/utils.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ var debug = require('debug')('cnpmjs.org:controllers:utils');
var path = require('path');
var fs = require('fs');
var utility = require('utility');
var ms = require('ms');
var ms = require('humanize-ms');
var nfs = require('../common/nfs');
var config = require('../config');

Expand Down
6 changes: 3 additions & 3 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@
"giturl": "~0.0.3",
"graceful": "~0.1.0",
"gravatar": "~1.1.0",
"humanize-ms": "~1.0.0",
"humanize-number": "~0.0.2",
"koa": "~0.12.2",
"koa-limit": "~1.0.2",
Expand All @@ -40,9 +41,8 @@
"mini-logger": "~1.0.0",
"mkdirp": "~0.5.0",
"moment": "~2.8.3",
"ms": "~0.6.2",
"multiline": "~1.0.1",
"mysql": "~2.5.1",
"mysql": "~2.5.2",
"nodemailer": "~1.3.0",
"ready": "~0.1.1",
"redis": "~0.12.1",
Expand All @@ -59,7 +59,7 @@
"istanbul-harmony": "~0.3.0",
"jshint": "~2.5.6",
"mm": "~0.2.1",
"mocha": "~1.21.4",
"mocha": "~1.21.5",
"node-dev": "*",
"pedding": "~1.0.0",
"should": "~4.0.4",
Expand Down
29 changes: 25 additions & 4 deletions proxy/npm.js
Original file line number Diff line number Diff line change
Expand Up @@ -64,17 +64,38 @@ exports.get = function *(name) {
return data;
};

exports.getAllSince = function *(startkey) {
exports.getAllSince = function *(startkey, timeout) {
var r = yield *request('/-/all/since?stale=update_after&startkey=' + startkey, {
timeout: 300000
timeout: timeout || 300000
});
return r.data;
};

exports.getShort = function *() {
exports.getShort = function *(timeout) {
var r = yield *request('/-/short', {
timeout: 300000,
timeout: timeout || 300000,
registry: 'http://r.cnpmjs.org', // registry.npmjs.org/-/short is 404 now.
});
return r.data;
};

exports.getPopular = function* (top, timeout) {
var r = yield *request('/-/_view/dependedUpon?group_level=1', {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we need to impl this api too?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

感觉可以等私有的搞定之后做个私有的,公共的可以直接代理到 NPM ?

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

嗯,有道理 +1
2014-10-13 上午11:52于 "Yiyu He" [email protected]写道:

In proxy/npm.js:

 registry: 'http://r.cnpmjs.org', // registry.npmjs.org/-/short is 404 now.

});
return r.data;
};
+
+exports.getPopular = function* (top, timeout) {

  • var r = yield *request('/-/_view/dependedUpon?group_level=1', {

感觉可以等私有的搞定之后做个私有的,公共的可以直接代理到 NPM ?


Reply to this email directly or view it on GitHub
https://github.com/cnpm/cnpmjs.org/pull/472/files#r18753302.

registry: config.officialNpmRegistry,
timeout: timeout || 60000
});
if (!r.data || !r.data.rows || !r.data.rows.length) {
return [];
}

return r.data.rows.sort(function (a, b) {
return b.value - a.value;
})
.slice(0, top)
.map(function (r) {
return r.key && r.key[0]
})
.filter(function (r) {
return r;
});
}
1 change: 0 additions & 1 deletion proxy/sync_module_worker.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,6 @@ var crypto = require('crypto');
var sleep = require('co-sleep');
var urllib = require('../common/urllib');
var utility = require('utility');
var ms = require('ms');
var urlparse = require('url').parse;
var nfs = require('../common/nfs');
var npm = require('./npm');
Expand Down
45 changes: 43 additions & 2 deletions sync/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@

var debug = require('debug')('cnpmjs.org:sync:index');
var co = require('co');
var ms = require('ms');
var ms = require('humanize-ms');
var util = require('util');
var utility = require('utility');
var config = require('../config');
Expand Down Expand Up @@ -58,7 +58,7 @@ var handleSync = co(function *() {
var data;
var error;
try {
var data = yield *sync();
data = yield *sync();
} catch (err) {
error = err;
error.message += ' (sync package error)';
Expand All @@ -77,6 +77,9 @@ if (sync) {
setInterval(handleSync, ms(config.syncInterval));
}

/**
* sync dist(node.js and phantomjs)
*/
var syncingDist = false;
var syncDist = co(function* syncDist() {
if (syncingDist) {
Expand Down Expand Up @@ -104,6 +107,44 @@ if (config.syncDist) {
logger.syncInfo('sync dist disable');
}

/**
* sync popular modules
*/

var syncingPopular = false;
var syncPopular = co(function* syncPopular() {
if (syncingPopular) {
return;
}
syncingPopular = true;
logger.syncInfo('Start syncing popular modules...');
var data;
var error;
try {
data = yield *require('./sync_popular');
} catch (err) {
error = err;
error.message += ' (sync package error)';
logger.syncError(error);
}

if (data) {
logger.syncInfo(data);
}
if (!config.debug) {
sendMailToAdmin(error, data, new Date());
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

沒有錯誤還發郵件?

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

可能会有同步失败的,但是没有错误。 sendMail 里面的逻辑会处理

}

syncPopular = false;
});

if (config.syncPopular) {
syncPopular();
setInterval(syncPopular, ms(config.syncPopularInterval));
} else {
logger.syncInfo('sync popular module disable');
}

function sendMailToAdmin(err, result, syncTime) {
result = result || {};
var to = [];
Expand Down
2 changes: 1 addition & 1 deletion sync/sync_all.js
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@
*/

var debug = require('debug')('cnpmjs.org:sync:sync_all');
var ms = require('ms');
var ms = require('humanize-ms');
var utility = require('utility');
var config = require('../config');
var Status = require('./status');
Expand Down
2 changes: 1 addition & 1 deletion sync/sync_exist.js
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ var SyncModuleWorker = require('../proxy/sync_module_worker');
var debug = require('debug')('cnpmjs.org:sync:sync_exist');
var utility = require('utility');
var Status = require('./status');
var ms = require('ms');
var ms = require('humanize-ms');
var thunkify = require('thunkify-wrap');

function intersection(arrOne, arrTwo) {
Expand Down
47 changes: 47 additions & 0 deletions sync/sync_popular.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
/*!
* cnpmjs.org - sync/sync_popular.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <[email protected]> (http://deadhorse.me)
*/

'use strict';

/**
* Module dependencies.
*/

var SyncModuleWorker = require('../proxy/sync_module_worker');
var debug = require('debug')('cnpmjs.org:sync:sync_popular');
var thunkify = require('thunkify-wrap');
var config = require('../config');
var Npm = require('../proxy/npm');
var utility = require('utility');
var Status = require('./status');

module.exports = function *sync() {
var syncTime = Date.now();

var packages = yield Npm.getPopular(config.topPopular);

var worker = new SyncModuleWorker({
username: 'admin',
name: packages,
concurrency: config.syncConcurrency
});
Status.init({need: packages.length}, worker);
worker.start();
var end = thunkify.event(worker);
yield end();

debug('All popular packages sync done, successes %d, fails %d',
worker.successes.length, worker.fails.length);

return {
successes: worker.successes,
fails: worker.fails
};
};
14 changes: 11 additions & 3 deletions test/proxy/npm.test.js
Original file line number Diff line number Diff line change
Expand Up @@ -26,12 +26,12 @@ var fixtures = path.join(path.dirname(__dirname), 'fixtures');
describe('proxy/npm.test.js', function () {
afterEach(mm.restore);

it('should return a module info from source npm', function *() {
it('should return a module info from source npm', function* () {
var data = yield npm.get('pedding');
data.name.should.equal('pedding');
});

it('should return null when module not exist', function *() {
it('should return null when module not exist', function* () {
var data = yield npm.get('pedding-not-exists');
should.not.exist(data);
});
Expand All @@ -46,7 +46,7 @@ describe('proxy/npm.test.js', function () {
}
});

it('should return ServerError when http 500 response', function *() {
it('should return ServerError when http 500 response', function* () {
var content = fs.createReadStream(path.join(fixtures, '500.txt'));
mm.http.request(/\//, content, { statusCode: 500 });
// http://registry.npmjs.org/octopie
Expand All @@ -58,4 +58,12 @@ describe('proxy/npm.test.js', function () {
err.message.should.equal('Status 500, ' + fs.readFileSync(path.join(fixtures, '500.txt'), 'utf8'));
}
});

describe('getPopular()', function () {
it('should return popular modules', function* () {
var names = yield npm.getPopular(10);
names.should.have.a.lengthOf(10);
names[0].should.equal('underscore');
});
});
});
32 changes: 32 additions & 0 deletions test/sync/sync_popular.test.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*!
* cnpmjs.org - test/sync/sync_popular.test.js
*
* Copyright(c) cnpmjs.org and other contributors.
* MIT Licensed
*
* Authors:
* dead_horse <[email protected]> (http://deadhorse.me)
*/

'use strict';

/**
* Module dependencies.
*/
var sync = require('../../sync/sync_popular');
var mm = require('mm');
var Npm = require('../../proxy/npm');
var Total = require('../../proxy/total');
var should = require('should');

describe('sync/sync_popular.test.js', function () {
describe('sync()', function () {
afterEach(mm.restore);
it('should sync popular modules ok', function *() {
mm.data(Npm, 'getPopular', ['mk2testmodule']);
var data = yield sync;
data.successes.should.eql(['mk2testmodule']);
mm.restore();
});
});
});