vendredi 4 juillet 2014

Isolating app crashes for users in node


Vote count:

0




I have built a web application with node + express and I'm currently testing it.


I realised that when more than one client is connected, a application crash for one client affects all the other clients and resets their sessions when the app restarts.


I want a way to prevent this from happening, so that one user experiencing a crash doesn't affect other connected users.


I read sometime ago about the possibility of using clusters for this (clusters aren't stable yet, at this time), but it isn't working as expected.


This is what I have currently:



var
cluster = require('cluster'),
numCPUs = require('os').cpus().length;

var
express = require('express'),
fibrous = require('fibrous'),
engine = require( 'ejs-locals' ),
http = require('http'),
path = require('path'),
app = express(),
fs = require('fs');


// App Config
app.configure('development', function(){
app.use(express.errorHandler({ dumpExceptions: true, showStack: true }));
});

app.configure( 'production', function(){
app.use(express.errorHandler());
});

app.configure(function(){
app.set('port', process.env.PORT || 5000);
app.engine( 'ejs', engine );
app.set('views', __dirname + '/views');
app.set('view engine', 'ejs');
app.use(express.favicon());
app.use(express.logger('dev'));
app.use(express.bodyParser({ keepExtensions: true }));
app.use(fibrous.middleware);
app.use(express.methodOverride());
app.use(express.cookieParser());
app.use(express.session({ secret: "sailup" }));
app.use(app.router);
app.use(express.static(path.join(__dirname, 'public')));
// Handle 404
app.use(function(req, res) {
process.exit( 1 );
res.status(404);
res.render('404', {
title: 'Page not found',
pagestyles: [{
src: '/assets/css/404.css'
}],
pagescripts: [{
src: '/assets/js/404.js'
}]
});
});

// Handle 500
app.use(function(error, req, res, next) {
console.log(error.stack);

res.status(500);
res.render('500', {
title: 'Something went wrong',
message: errorMessage,
pagestyles: [{
src: '/assets/css/500.css'
}],
pagescripts: [{
src: '/assets/js/500.js'
}]
});
});
}); // Configs End


// Locate and Load Routes
var routesDir = 'routes',
routeFiles = fs.readdirSync(routesDir);

routeFiles.forEach(function(file) {
var filePath = path.resolve('./', routesDir, file),
route = require(filePath);
route.init(app);
}); // Done Loading Routes

if (cluster.isMaster) {
// Fork workers.
for (var i = 0; i < numCPUs; i++) {
cluster.fork();
}

cluster.on('exit', function(worker, code, signal) {
console.log('worker ' + worker.process.pid + ' died');
});
} else {
// Workers can share any TCP connection
// In this case its a HTTP server
// Start The Server Now :)
http.createServer(app).listen(app.get('port'), function(){
console.log('running');
});
}


But it looks like everything runs more than once. For instance, the log statement prints about 4 times and even though crashes are not affected somehow. But most importantly, it looks like my sessions are messed up. Any new page I visit acts as though no session exists. I think the sessions are being lost somehow.


If you know a better way of achieving this, please do help me out. If you can help make this work too, I would be glad to hear from you. Thanks :)



asked 1 min ago






Aucun commentaire:

Enregistrer un commentaire