Skip to content

Commit

Permalink
[fix] #70 request for version should be performed async and aborted i…
Browse files Browse the repository at this point in the history
…f jitsu finishes before the request finishes
  • Loading branch information
bmeck committed Aug 11, 2011
1 parent 82cdd59 commit e189205
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 19 deletions.
16 changes: 15 additions & 1 deletion lib/jitsu.js
Expand Up @@ -87,7 +87,21 @@ jitsu.start = function (argv, callback) {

winston.info('Welcome to ' + 'Nodejitsu'.grey);
winston.info('It worked if it ends with ' + 'Nodejitsu'.grey + ' ok'.green.bold);
jitsu.utils.checkVersion(function () {
jitsu.utils.checkVersion(function (handler) {
var oldCallback = callback;
callback = function() {
if(handler.req) {
var timer = setTimeout(function() {
if(handler.req) {
handler.req.abort();
}
handler.on('end', function() {
clearTimeout(timer);
})
}, 200);
}
oldCallback.call(this, arguments);
}
jitsu.config.load(function (err) {
if (err) {
callback(err);
Expand Down
27 changes: 9 additions & 18 deletions lib/jitsu/utils/index.js
Expand Up @@ -143,22 +143,11 @@ utils.objectDiff = function (current, update, level) {
utils.checkVersion = function (callback) {
var responded = false

//
// Only allow the `checkVersion` function 200ms
// to attempt to contact GitHub.
//
setTimeout(function () {
if (!responded) {
responded = true;
callback();
}
}, 200);

//
// Check the GitHub tags for `jitsu` to see if the current
// version is outdated.
// version is outdated. If it is not make sure to message the user at the end.
//
request({
var handler = request({
uri: 'http://registry.npmjs.org/jitsu/latest'
}, function (err, res, body) {
if (!responded) {
Expand All @@ -168,8 +157,10 @@ utils.checkVersion = function (callback) {
var pkg = JSON.parse(body);

if (semver.gt(pkg.version, jitsu.version)) {
winston.warn('A newer version of jitsu is available: ' + pkg.version);
winston.warn('Please run `npm update jitsu`');
process.on('exit', function() {
winston.warn('A newer version of jitsu is available: ' + pkg.version);
winston.warn('Please run `npm update jitsu`');
});
}
}
catch (ex) {
Expand All @@ -178,8 +169,8 @@ utils.checkVersion = function (callback) {
// of an upgrade at the next possible opportunity.
//
}

callback();
}
});
};

callback(handler);
};

0 comments on commit e189205

Please sign in to comment.