Skip to content

Commit

Permalink
Merge pull request #233 from oortcloud/rolling-timeout
Browse files Browse the repository at this point in the history
improve error reporting and use rolling timeout
  • Loading branch information
tmeasday committed Jan 17, 2014
2 parents 85ee7e3 + 9acd62e commit 6d09b3d
Show file tree
Hide file tree
Showing 3 changed files with 73 additions and 14 deletions.
77 changes: 67 additions & 10 deletions lib/sources/git.js
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,11 @@ var wrench = require('wrench');
var fs = require('fs');
var url = require('url');
var spawn = require('child_process').spawn;
var exec = require('child_process').exec;
var exec = require('rolling_timeout_exec').exec;
var fstream = require('fstream');

var ROLLING_TIMEOUT = 15000;

GitSource = function(basePath, config) {

// Setup
Expand All @@ -27,6 +29,7 @@ GitSource = function(basePath, config) {
}

};

GitSource.prototype.packagePath = function() {
return path.join(this.basePath, this.packageNamespace(), this.commit);
};
Expand Down Expand Up @@ -107,14 +110,25 @@ GitSource.prototype._pull = function(fn) {
};

GitSource.prototype._clone = function(fn) {
var self = this;
var self = this,
child,
timedOut = false,
command = 'git clone --progress ' +
this.url + ' "' + this.sourcePath + '"',
options = { rollingTimeout: ROLLING_TIMEOUT };

if (!fs.existsSync(this.sourcePath)) {
exec('git clone ' + self.url + ' "' + this.sourcePath + '"', function(err, stdout, stderr) {
if (err)
child = exec(command, options, function(err, stdout, stderr) {
if (err) {
reportErrors(err, stdout, stderr, timedOut);
throw "There was a problem cloning repo: " + self.url +
"\nPlease check https://github.com/oortcloud/meteorite/blob/master/CONTRIBUTING.md#troubleshooting for potential explanations.";
}
fn();
});
child.on('rolling-timeout', function () {
timedOut = true;
});
} else {
fn();
}
Expand Down Expand Up @@ -151,13 +165,26 @@ GitSource.prototype._load = function(fn) {
};

GitSource.prototype._checkout = function(fn) {
var self = this;

exec('git checkout ' + (this.commit || this.head), {cwd: this.sourcePath}, function(err, stdout, stderr) {
if (err)
throw "There was a problem checking out " + self.checkoutType + ": " + (self.commit || self.head);
var self = this,
child,
timedOut = false,
command = 'git checkout ' + (this.commit || this.head),
options = {
rollingTimeout: ROLLING_TIMEOUT,
cwd: this.sourcePath
};

child = exec(command, options, function(err, stdout, stderr) {
if (err) {
reportErrors(err, stdout, stderr, timedOut);
throw "There was a problem checking out " +
self.checkoutType + ": " + (self.commit || self.head);
}
fn();
});
child.on('rolling-timeout', function () {
timedOut = true;
})
};

GitSource.prototype._updateSubmodules = function(fn) {
Expand Down Expand Up @@ -219,9 +246,39 @@ GitSource.prototype.overrides = function(otherSource) {
}

GitSource.getCommitForDir = function(path, fn) {
exec('git rev-parse HEAD', { cwd: path }, function (err, stdout) {
var self = this,
child,
timedOut = false,
command = 'git rev-parse HEAD',
options = {
rollingTimeout: ROLLING_TIMEOUT,
cwd: path
};

child = exec(command, options, function (err, stdout, stderr) {
if (err) {
reportErrors(err, stdout, stderr, timedOut);
}
fn(stdout.trim());
});
child.on('rolling-timeout', function () {
timedOut = true;
});
};

module.exports = GitSource;




function reportErrors (err, stdout, stderr, timedOut) {
var timedOutMsg = 'child process timed out, no activity for ' +
ROLLING_TIMEOUT/1000 + ' seconds';

if (timedOut) {
console.error(timedOutMsg, '\n');
}
console.error('ERROR:', err.code, err.message, '\n');
console.error('STDOUT:', stdout, '\n');
console.error('STDERR:', stderr, '\n');
}
1 change: 1 addition & 0 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@
, "prompt": "0.2.11"
, "colors": "0.6.0-1"
, "async": "0.2.9"
, "rolling_timeout_exec": ">=0.0.1"
}
, "devDependencies":
{ "mocha": ">=1.2.2"
Expand Down
9 changes: 5 additions & 4 deletions spec/support/bin/git
Original file line number Diff line number Diff line change
Expand Up @@ -21,16 +21,17 @@ if (command === 'clone') {
if (!fs.existsSync(cachePath))
wrench.mkdirSyncRecursive(cachePath);

var repoUrl = args[1];
var destPath = args.pop();
var repoUrl = args.pop();
var repoParts = repoUrl.replace('.git', '').split('/');
var repoName = repoParts.pop();
var repoUser = repoParts.pop();
var destPath = args[2];
var repoCachePath = path.join(cachePath, repoUser, repoName);

if (!fs.existsSync(repoCachePath)) {

var git = spawn('/usr/local/bin/git', ['clone', repoUrl, repoCachePath]);
args.push(repoUrl);
args.push(repoCachePath);
var git = spawn('/usr/local/bin/git', args);

git.on('exit', function() {
copyRepo(repoCachePath, destPath);
Expand Down

0 comments on commit 6d09b3d

Please sign in to comment.