Skip to content
This repository has been archived by the owner on Nov 23, 2017. It is now read-only.

Commit

Permalink
more robust browser instance handling
Browse files Browse the repository at this point in the history
  • Loading branch information
Klaus Großmann committed Nov 29, 2015
1 parent dcd88f1 commit c43ee8b
Show file tree
Hide file tree
Showing 2 changed files with 49 additions and 21 deletions.
62 changes: 46 additions & 16 deletions client/lib/browser.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand All @@ -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) {
Expand Down Expand Up @@ -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);
8 changes: 3 additions & 5 deletions master/app.js
Original file line number Diff line number Diff line change
@@ -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');
Expand Down Expand Up @@ -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')));
Expand All @@ -71,7 +70,6 @@ app.use(function (req, res, next) {
next(err);
});


// error handlers

// development error handler
Expand Down

0 comments on commit c43ee8b

Please sign in to comment.