<commit_before>
/**
 * Module dependencies.
 */

var express = require('express'),
	fs = require("fs"),
	winston = require('winston'),
	socketio = require('socket.io'),
	config = require('./config'),
	mongoose = require('mongoose');

var numThreads = process.env.numThreads || require('os').cpus().length;

/**
 * Needed modules:
 * express
 * npm install -g express
 */

var app = module.exports = express.createServer();
//Use Socket.IO
var io = socketio.listen(app);
io.set('log level', 1);

//Load and set up the logger
var logger = new (winston.Logger)({
	//Make it log to both the console and a file 
	transports : [new (winston.transports.Console)(),
	              new (winston.transports.File)({filename: 'logs/general.log'})],
	//Log uncought exceptions to a seperate log
	exceptionHandlers: [new winston.transports.File({filename: 'logs/exceptions.log'}),
	                    new (winston.transports.Console)()]
});

// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/static'));
});

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

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

//Custom error handler
//This is modeled off the connect errorHandler
//https://github.com/senchalabs/connect/blob/master/lib/middleware/errorHandler.js
// http://stackoverflow.com/questions/7151487/error-handling-principles-for-nodejs-express-apps
app.use(function errorHandler(err, req, res, next){
  if (err.status) res.statusCode = err.status;
  if (res.statusCode < 400) res.statusCode = 500;
  //Send back json
  res.setHeader('Content-Type', 'application/json');
  //Dont send the whole stack, could have security issues
  res.end(JSON.stringify({error: err.message}));
  //Log it
  logger.log('error', 'Error ', {stack: err.stack});
});

//Routes
//Dummy routes
logger.info('Loading dummy');
require('./dummy/dummy.js').load(app);
//Event routes
logger.info('Loading events');
require('./events/router.js').load(app, io);


if(config.noDB){
	logger.info("Running in no-database mode, all data will be cleared on exit.");
}

/*
 * Using cluster to run this one
 * Note for windows with cygwin: After this is started and killed, you will need to go through the task manager and kill all the node.exe processes
 * If you dont, it will crash with IPC error
 */
var cluster = require('cluster');
if(cluster.isMaster){
	logger.info("Starting "+numThreads+" threads");
	//Expand to all cores
	for(var i = 0; i<numThreads; i++)
		cluster.fork();
} else {
	app.listen((process.env.port || 8081), function(){
		logger.info("Express server listening on port "+app.address().port+" in "+app.settings.env+" mode");
		});
}<commit_msg>Disable threading when not connected to a database<commit_after>
/**
 * Module dependencies.
 */

var express = require('express'),
	fs = require("fs"),
	winston = require('winston'),
	socketio = require('socket.io'),
	config = require('./config'),
	mongoose = require('mongoose');

var numThreads = process.env.numThreads || require('os').cpus().length;

/**
 * Needed modules:
 * express
 * npm install -g express
 */

var app = module.exports = express.createServer();
//Use Socket.IO
var io = socketio.listen(app);
io.set('log level', 1);

//Load and set up the logger
var logger = new (winston.Logger)({
	//Make it log to both the console and a file 
	transports : [new (winston.transports.Console)(),
	              new (winston.transports.File)({filename: 'logs/general.log'})],
	//Log uncought exceptions to a seperate log
	exceptionHandlers: [new winston.transports.File({filename: 'logs/exceptions.log'}),
	                    new (winston.transports.Console)()]
});

// Configuration
app.configure(function(){
  app.set('views', __dirname + '/views');
  app.set('view engine', 'jade');
  app.use(express.bodyParser());
  app.use(express.methodOverride());
  app.use(app.router);
  app.use(express.static(__dirname + '/static'));
});

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

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

//Custom error handler
//This is modeled off the connect errorHandler
//https://github.com/senchalabs/connect/blob/master/lib/middleware/errorHandler.js
// http://stackoverflow.com/questions/7151487/error-handling-principles-for-nodejs-express-apps
app.use(function errorHandler(err, req, res, next){
  if (err.status) res.statusCode = err.status;
  if (res.statusCode < 400) res.statusCode = 500;
  //Send back json
  res.setHeader('Content-Type', 'application/json');
  //Dont send the whole stack, could have security issues
  res.end(JSON.stringify({error: err.message}));
  //Log it
  logger.log('error', 'Error ', {stack: err.stack});
});

//Routes
//Dummy routes
logger.info('Loading dummy');
require('./dummy/dummy.js').load(app);
//Event routes
logger.info('Loading events');
require('./events/router.js').load(app, io);


if(config.noDB){
	logger.info("Running in no-database mode, all data will be cleared on exit.");
}

/*
 * Using cluster to run this one
 * Note for windows with cygwin: After this is started and killed, you will need to go through the task manager and kill all the node.exe processes
 * If you dont, it will crash with IPC error
 */
var cluster = require('cluster');
if(!config.noDB){
	if(cluster.isMaster){
		logger.info("Starting "+numThreads+" threads");
		//Expand to all cores
		for(var i = 0; i<numThreads; i++)
			cluster.fork();
	} else {
		app.listen(config.port, function(){
			logger.info("Express server listening on port "+app.address().port+" in "+app.settings.env+" mode");
			});
	}
} else {
	logger.warn('Sorry, can not use multiple threads in no database mode. Because Node.js doesn\'t support shared memory, the threads'+
			' will have inconsistant data. Falling back to one thread.');
	app.listen(config.port, function(){
		logger.info("Express server listening on port "+app.address().port+" in "+app.settings.env+" mode");
		});
}
{'fraction_non_alphanumeric': 0.1678634115206638, 'fraction_numerical': 0.005425243338120313, 'mean_word_length': 4.245188284518829, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 3, 'https://': 2, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5773141', 'n_tokens_mistral': 2008, 'n_tokens_neox': 1923, 'n_words': 534}