diff --git a/examples/local.js b/examples/local.js index 4c710b9..1425a0c 100644 --- a/examples/local.js +++ b/examples/local.js @@ -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; @@ -28,6 +28,6 @@ launch.local(function(err, launcher) { worker.stop(function() { console.log('Firefox stopped'); }); - }, 2000); + }, 6000); }); }); \ No newline at end of file diff --git a/lib/local/index.js b/lib/local/index.js index 7d920b6..a666f19 100644 --- a/lib/local/index.js +++ b/lib/local/index.js @@ -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)); + }) } diff --git a/lib/local/instance.js b/lib/local/instance.js index aeff97c..bcc288e 100644 --- a/lib/local/instance.js +++ b/lib/local/instance.js @@ -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; @@ -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) { @@ -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(); } @@ -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()); + } };