diff --git a/client/lib/browser.js b/client/lib/browser.js index 51fb183..98e778a 100644 --- a/client/lib/browser.js +++ b/client/lib/browser.js @@ -7,8 +7,31 @@ var chromeConnection; var chromeInstance; var params = {}; +var currentUrl; var initialized = false; +function initChromeInstance(cb) { + chromeInstance = spawn(params.path, params.args.concat([params.url])); + + chromeInstance.on('error', function (err) { + cb(err); + }); + chromeInstance.on('close', function (code) { + logger.warn('Chrome instance exited with code ' + code); + initChromeInstance(); + }); + chromeInstance.stdout.on('data', function (data) { + logger.info(data.toString()); + }); + + chromeInstance.stderr.on('data', function (data) { + logger.error(data.toString()); + }); + + // wait to let chrome start gracefully + setTimeout(cb, params.startupGracePeriod); +} + module.exports = { isInitialized: function () { return initialized; @@ -18,27 +41,13 @@ module.exports = { if (!initialized) { params = options || {}; + currentUrl = params.url; async.waterfall([ function (next) { - chromeInstance = spawn(params.path, params.args.concat([params.url])); - - chromeInstance.on('error', function (err) { + initChromeInstance(function (err) { next(err); }); - chromeInstance.on('close', function (code) { - logger.warn('child process exited with code ' + code); - }); - chromeInstance.stdout.on('data', function (data) { - logger.info(data.toString()); - }); - - chromeInstance.stderr.on('data', function (data) { - logger.error(data.toString()); - }); - - // wait to let chrome start gracefully - setTimeout(next, params.startupGracePeriod); }, // setup Chrome Protocol function (next) { @@ -81,3 +90,24 @@ module.exports = { } } }; + +function exitHandler(options, err) { + if (options.cleanup) { + console.log('clean'); + } + if (err) { + console.log(err.stack); + } + if (options.exit) { + process.exit(); + } +} + +//do something when app is closing +process.on('exit', exitHandler); + +//catches ctrl+c event +process.on('SIGINT', exitHandler); + +//catches uncaught exceptions +process.on('uncaughtException', exitHandler); diff --git a/master/app.js b/master/app.js index 5e676ed..732c7f6 100644 --- a/master/app.js +++ b/master/app.js @@ -1,6 +1,5 @@ var express = require('express'); var hbs = require('hbs'); -//var browserify = require('browserify-middleware'); var path = require('path'); var favicon = require('serve-favicon'); var logger = require('morgan'); @@ -54,9 +53,9 @@ app.use(cookieParser()); //app.use('/js', browserify(path.join(__dirname, 'public/js'))); if (process.env.NODE_ENV != 'production') { - var serve = require("staticr/serve"); - app.use(serve(require("./static-routes/browserify"))); - app.use(serve(require("./static-routes/less"))); + var serve = require('staticr/serve'); + app.use(serve(require('./static-routes/browserify'))); + app.use(serve(require('./static-routes/less'))); } app.use('/fonts', express.static(path.join(__dirname, '../node_modules/font-awesome/fonts'))); @@ -71,7 +70,6 @@ app.use(function (req, res, next) { next(err); }); - // error handlers // development error handler