This repository has been archived by the owner on Jun 2, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 746
add sync popular modules, close #468 #472
Merged
Merged
Changes from all commits
Commits
Show all changes
2 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -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'); | ||
|
@@ -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)'; | ||
|
@@ -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) { | ||
|
@@ -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()); | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. 沒有錯誤還發郵件? There was a problem hiding this comment. Choose a reason for hiding this commentThe 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 = []; | ||
|
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
}; | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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(); | ||
}); | ||
}); | ||
}); |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
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?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
感觉可以等私有的搞定之后做个私有的,公共的可以直接代理到 NPM ?
There was a problem hiding this comment.
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]写道: