2011-09-05 07:00:41 +08:00
|
|
|
/*
|
2011-10-07 22:38:28 +08:00
|
|
|
* Windshaft-CartoDB
|
|
|
|
* ===============
|
|
|
|
*
|
|
|
|
* ./app.js [environment]
|
|
|
|
*
|
|
|
|
* environments: [development, production]
|
|
|
|
*/
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2014-05-07 17:03:25 +08:00
|
|
|
var path = require('path'),
|
2014-09-24 17:42:36 +08:00
|
|
|
fs = require('fs'),
|
|
|
|
RedisPool = require('redis-mpool')
|
2014-05-07 17:03:25 +08:00
|
|
|
;
|
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2014-02-28 23:22:24 +08:00
|
|
|
if ( process.argv[2] ) ENV = process.argv[2];
|
|
|
|
else if ( process.env['NODE_ENV'] ) ENV = process.env['NODE_ENV'];
|
|
|
|
else ENV = 'development';
|
|
|
|
|
|
|
|
process.env['NODE_ENV'] = ENV;
|
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
// sanity check
|
2013-02-11 20:41:51 +08:00
|
|
|
if (ENV != 'development' && ENV != 'production' && ENV != 'staging' ){
|
2011-10-07 22:38:28 +08:00
|
|
|
console.error("\nnode app.js [environment]");
|
2014-02-28 23:22:24 +08:00
|
|
|
console.error("environments: development, production, staging\n");
|
2011-10-07 22:38:28 +08:00
|
|
|
process.exit(1);
|
2011-09-05 07:00:41 +08:00
|
|
|
}
|
|
|
|
|
2014-04-11 21:14:59 +08:00
|
|
|
var _ = require('underscore');
|
2014-03-29 01:47:59 +08:00
|
|
|
|
2011-09-05 07:00:41 +08:00
|
|
|
// set environment specific variables
|
|
|
|
global.environment = require(__dirname + '/config/environments/' + ENV);
|
2014-07-29 20:54:35 +08:00
|
|
|
global.environment.api_hostname = require('os').hostname().split('.')[0];
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2014-09-24 17:42:36 +08:00
|
|
|
global.log4js = require('log4js');
|
2014-03-13 00:20:49 +08:00
|
|
|
log4js_config = {
|
2014-03-29 01:05:18 +08:00
|
|
|
appenders: [],
|
2014-02-18 22:12:08 +08:00
|
|
|
replaceConsole:true
|
2014-03-13 00:20:49 +08:00
|
|
|
};
|
|
|
|
|
2014-10-08 22:50:35 +08:00
|
|
|
if (global.environment.uv_threadpool_size) {
|
|
|
|
process.env.UV_THREADPOOL_SIZE = global.environment.uv_threadpool_size;
|
|
|
|
}
|
|
|
|
|
2014-03-29 01:05:18 +08:00
|
|
|
if ( global.environment.log_filename ) {
|
2014-05-07 17:03:25 +08:00
|
|
|
var logdir = path.dirname(global.environment.log_filename);
|
|
|
|
// See cwd inlog4js.configure call below
|
|
|
|
logdir = path.resolve(__dirname, logdir);
|
|
|
|
if ( ! fs.existsSync(logdir) ) {
|
|
|
|
console.error("Log filename directory does not exist: " + logdir);
|
|
|
|
process.exit(1);
|
|
|
|
}
|
2014-03-29 01:05:18 +08:00
|
|
|
console.log("Logs will be written to " + global.environment.log_filename);
|
|
|
|
log4js_config.appenders.push(
|
|
|
|
{ type: "file", filename: global.environment.log_filename }
|
|
|
|
);
|
|
|
|
} else {
|
|
|
|
log4js_config.appenders.push(
|
|
|
|
{ type: "console", layout: { type:'basic' } }
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
2014-03-13 00:20:49 +08:00
|
|
|
if ( global.environment.rollbar ) {
|
|
|
|
log4js_config.appenders.push({
|
|
|
|
type: __dirname + "/lib/cartodb/log4js_rollbar.js",
|
|
|
|
options: global.environment.rollbar
|
|
|
|
});
|
|
|
|
}
|
2014-02-18 22:12:08 +08:00
|
|
|
|
2014-04-22 16:52:59 +08:00
|
|
|
log4js.configure(log4js_config, { cwd: __dirname });
|
2014-03-13 00:20:49 +08:00
|
|
|
global.logger = log4js.getLogger();
|
2014-02-18 22:12:08 +08:00
|
|
|
|
2014-10-15 04:12:35 +08:00
|
|
|
var redisOpts = _.extend(global.environment.redis, { name: 'windshaft' }),
|
2014-09-24 17:42:36 +08:00
|
|
|
redisPool = new RedisPool(redisOpts);
|
|
|
|
|
2013-12-18 00:35:12 +08:00
|
|
|
// Include cartodb_windshaft only _after_ the "global" variable is set
|
|
|
|
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/28
|
2014-09-24 17:42:36 +08:00
|
|
|
var CartodbWindshaft = require('./lib/cartodb/cartodb_windshaft'),
|
|
|
|
serverOptions = require('./lib/cartodb/server_options')(redisPool);
|
2011-09-05 07:00:41 +08:00
|
|
|
|
2011-10-13 19:17:00 +08:00
|
|
|
ws = CartodbWindshaft(serverOptions);
|
2013-01-30 00:36:50 +08:00
|
|
|
|
2014-10-15 04:12:35 +08:00
|
|
|
if (global.statsClient) {
|
|
|
|
redisPool.on('status', function(status) {
|
|
|
|
var keyPrefix = status.name + '.db' + status.db + '.';
|
|
|
|
global.statsClient.gauge(keyPrefix + 'count', status.count);
|
|
|
|
global.statsClient.gauge(keyPrefix + 'unused', status.unused);
|
|
|
|
global.statsClient.gauge(keyPrefix + 'waiting', status.waiting);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2013-01-30 00:36:50 +08:00
|
|
|
// Maximum number of connections for one process
|
|
|
|
// 128 is a good number if you have up to 1024 filedescriptors
|
|
|
|
// 4 is good if you have max 32 filedescriptors
|
|
|
|
// 1 is good if you have max 16 filedescriptors
|
|
|
|
ws.maxConnections = global.environment.maxConnections || 128;
|
|
|
|
|
2012-10-09 23:09:40 +08:00
|
|
|
ws.listen(global.environment.port, global.environment.host);
|
2013-01-30 00:36:50 +08:00
|
|
|
|
2014-03-11 19:21:00 +08:00
|
|
|
var version = require("./package").version;
|
|
|
|
|
2012-10-09 23:09:40 +08:00
|
|
|
ws.on('listening', function() {
|
2014-03-11 19:21:00 +08:00
|
|
|
console.log("Windshaft tileserver " + version + " started on "
|
2014-02-28 23:22:24 +08:00
|
|
|
+ global.environment.host + ':' + global.environment.port
|
|
|
|
+ " (" + ENV + ")");
|
2012-10-09 23:09:40 +08:00
|
|
|
});
|
2013-01-30 18:29:44 +08:00
|
|
|
|
2013-03-18 23:37:31 +08:00
|
|
|
// DEPRECATED, use SIGUSR2
|
2013-01-30 18:29:44 +08:00
|
|
|
process.on('SIGUSR1', function() {
|
2013-03-19 01:14:18 +08:00
|
|
|
console.log('WARNING: handling of SIGUSR1 by Windshaft-CartoDB is deprecated, please send SIGUSR2 instead');
|
2013-01-30 18:29:44 +08:00
|
|
|
ws.dumpCacheStats();
|
|
|
|
});
|
2013-03-18 23:37:31 +08:00
|
|
|
|
|
|
|
process.on('SIGUSR2', function() {
|
|
|
|
ws.dumpCacheStats();
|
2014-03-29 01:05:18 +08:00
|
|
|
});
|
|
|
|
|
|
|
|
process.on('SIGHUP', function() {
|
2014-03-29 00:06:44 +08:00
|
|
|
log4js.configure(log4js_config);
|
|
|
|
console.log('Log files reloaded');
|
2013-03-18 23:37:31 +08:00
|
|
|
});
|
2014-02-18 22:12:08 +08:00
|
|
|
|
2014-03-13 00:20:49 +08:00
|
|
|
process.on('uncaughtException', function(err) {
|
2014-03-13 18:58:29 +08:00
|
|
|
logger.error('Uncaught exception: ' + err.stack);
|
2014-03-13 00:20:49 +08:00
|
|
|
});
|