Skip to content

Commit

Permalink
Soft kill processes, throw error when browser is already running. Closes
Browse files Browse the repository at this point in the history
 #7, closes #8
  • Loading branch information
daffl committed Dec 6, 2012
1 parent 3b039fc commit 2671da0
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 10 deletions.
12 changes: 6 additions & 6 deletions examples/local.js
Original file line number Diff line number Diff line change
Expand Up @@ -3,22 +3,22 @@ var launch = require('../lib');
launch.local(function(err, launcher) {
// User the launcher api
launcher('http://github.com/ekryski', {
browser : 'canary'
browser : 'chrome'
}, function(error, worker) {
if(error) {
console.log('Error:', error);
return;
}
console.log('Launched Opera. Process id:', worker.id);
console.log('Launched Safari. Process id:', worker.id);
setTimeout(function() {
worker.stop(function() {
console.log('Opera stopped');
console.log('Safari stopped');
});
}, 4000);
}, 10000);
});

// Short hand launcher
launcher.aurora('http://github.com/daffl', function(error, worker) {
launcher.firefox('http://github.com/daffl', function(error, worker) {
if(error) {
console.log('Error:', error);
return;
Expand All @@ -28,6 +28,6 @@ launch.local(function(err, launcher) {
worker.stop(function() {
console.log('Firefox stopped');
});
}, 2000);
}, 6000);
});
});
7 changes: 6 additions & 1 deletion lib/local/index.js
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,12 @@ module.exports = function (settings, callback) {

var args = browser.args || [];
args.push(url);
callback(null, instance.start(browser.command, args, settings, browser), normalize(browser));
instance.start(browser.command, args, settings, browser, function(err, instance) {
if(err) {
return callback(err);
}
return callback(null, instance, normalize(browser));
})
}


Expand Down
44 changes: 41 additions & 3 deletions lib/local/instance.js
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,17 @@ var spawn = require("child_process").spawn;
var exec = require("child_process").exec;
var EventEmitter = require('events').EventEmitter;

var getProcessId = function(name, callback) {
// Get the process with the given name if it is running
exec("ps -clx | grep '" + name + "$' | awk '{print $2}' | head -1", function(err, stdout) {
var pid = stdout.trim();
if(!pid) {
return callback(new Error('There does not seem to be a ' + name + ' process running'));
}
callback(null, pid);
});
}

var Instance = function (cmd, args, settings, options) {
this.options = options || {};
var self = this;
Expand All @@ -28,6 +39,14 @@ var Instance = function (cmd, args, settings, options) {

Instance.prototype = new EventEmitter();

Instance.prototype.getPid = function(callback) {
if(this.options.process) {
getProcessId(this.options.process, callback);
} else {
callback(null, this.process.pid);
}
}

Instance.prototype.stop = function (callback) {
if(callback) {
this.once('stop', function(data) {
Expand All @@ -36,7 +55,14 @@ Instance.prototype.stop = function (callback) {
}

if(this.options.process && process.platform !== 'win32') {
spawn('killall', [this.options.process]);
this.getPid(function(err, pid) {
if(err) {
callback(err);
}

// We kill the process with SIGTERM (requesting termination)
spawn('kill', ['-s', 'TERM', pid]);
});
} else {
this.process.kill();
}
Expand All @@ -53,6 +79,18 @@ exports.Instance = Instance;
* @param callback function(error, instance) Callback after the instance is started
* @see http://nodejs.org/api/child_process.html#child_process_child_process
*/
exports.start = function (cmd, args, settings, options) {
return new Instance(cmd, args, settings, options);
exports.start = function (cmd, args, settings, options, callback) {
var getInstance = function() {
return new Instance(cmd, args, settings, options)
}
if(options.process) {
getProcessId(options.process, function(err, pid) {
if(!err) {
return callback(new Error(options.process + ' seems already running with process id ' + pid));
}
return callback(null, getInstance());
});
} else {
callback(null, getInstance());
}
};

0 comments on commit 2671da0

Please sign in to comment.