Skip to content
This repository has been archived by the owner on Jan 16, 2020. It is now read-only.

New scripts to fetch github data and update pluginsdb. #137

Closed
wants to merge 8 commits into from
3 changes: 3 additions & 0 deletions config-sample.json
Expand Up @@ -5,5 +5,8 @@
"url": "local.plugins.jquery.com", "url": "local.plugins.jquery.com",
"username": "admin", "username": "admin",
"password": "secret" "password": "secret"
},
"github": {
"token": "oauthtoken"
} }
} }
10 changes: 10 additions & 0 deletions lib/pluginsdb.js
Expand Up @@ -141,6 +141,16 @@ var pluginsDb = module.exports = {
}); });
}), }),


getAllPlugins: auto(function( fn ) {
db.all( "SELECT * FROM plugins", function( error, rows ) {
if ( error ) {
return fn( error );
}

fn( null, rows );
});
}),

_setup: function( fn ) { _setup: function( fn ) {
var Step = require( "step" ); var Step = require( "step" );


Expand Down
47 changes: 46 additions & 1 deletion lib/service/github.js
Expand Up @@ -3,7 +3,10 @@ var fs = require( "fs" ),
exec = require( "child_process" ).exec, exec = require( "child_process" ).exec,
Step = require( "step" ), Step = require( "step" ),
mkdirp = require( "mkdirp" ), mkdirp = require( "mkdirp" ),
service = require( "../service" ); service = require( "../service" ),
logger = require( "../logger" ),
config = require( "../config" ),
https = require( "https" );


var reGithubUrl = /^https?:\/\/github\.com\/([^\/]+)\/([^\/]+)(\/.*)?$/; var reGithubUrl = /^https?:\/\/github\.com\/([^\/]+)\/([^\/]+)(\/.*)?$/;


Expand Down Expand Up @@ -65,6 +68,48 @@ extend( GithubRepo.prototype, {
return this.siteUrl + "/zipball/" + version; return this.siteUrl + "/zipball/" + version;
}, },


getStats: function( plugin, fn ) {

function response( res ) {
var data = "";

res.on( "data", function(chunk){ data += chunk; } );

res.on( "end", function(){

if ( res.statusCode === 200 ) {
try {
data = JSON.parse( data );
plugin.watchers = data.watchers_count;
plugin.forks = data.forks_count;
logger.log(
"Updated " + plugin.plugin +
" Watchers(" + plugin.watchers + ")" +
" Forks(" + plugin.forks + ")"
);
} catch ( error ) {
return fn( error );
}
} else {
logger.error( "Unexpected reply: " + data );
}

fn( null, plugin );
});
}

var options = {
host: "api.github.com",
path: "/repos/" + plugin.repo.split( "/" ).slice( 1 ).join( "/" ),
headers: {
"user-agent": "stats/0.1 (+http://plugins.jquery.com)",
"Authorization": "token " + config.github.token
}
};

https.request( options, response ).on( "error", logger.error ).end();
},

getTags: function( fn ) { getTags: function( fn ) {
var repo = this; var repo = this;
Step( Step(
Expand Down
59 changes: 59 additions & 0 deletions scripts/update-plugin-stats.js
@@ -0,0 +1,59 @@
var db = require( "../lib/pluginsdb" ),
service = require( "../lib/service" ),
logger = require( "../lib/logger" ),
iv,
queue,
ready,
delay = 60*1000;

function update( err, plugin ) {
if ( err ) {
logger.log( err );
} else {
db.updateRepoMeta( plugin.repo, plugin, function(err){
if ( err ) {
logger.log( err );
}
iv = setTimeout( processData, delay );
});
}
}

function processData() {
var repo,
data = queue.shift();

if ( !ready ) {
clearTimeout( iv );
process.exit( 0 );
} else if ( !data ) {
iv = setTimeout( init, delay );
} else if ( ready && data ) {
repo = service.getRepoById( data.repo );
repo.getStats( data, update );
}
}

function init() {
db.getAllPlugins(function( err, plugins ) {
if ( !err && plugins.length > 0 ) {
queue = plugins;
ready = true;
processData();
} else {
iv = setTimeout( init, delay );
}
});
}

// Kick off
init();

// Let the current action finish, then stop processing and exit
function shutdownHook() {
Copy link
Contributor

Choose a reason for hiding this comment

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

Cron jobs don't need shutdown hooks.

logger.log( "Shutting down update-plugin-stats." );
ready = false;
}

process.once( "SIGINT", shutdownHook );
process.once( "SIGTERM", shutdownHook );