Windshaft-cartodb/app.js

174 lines
5.4 KiB
JavaScript
Raw Normal View History

var http = require('http');
var https = require('https');
2015-04-07 19:00:20 +08:00
var path = require('path');
var fs = require('fs');
var _ = require('underscore');
2017-03-25 00:28:23 +08:00
var semver = require('semver');
const glob = require('glob');
2017-03-25 00:28:23 +08:00
2017-03-28 00:27:55 +08:00
// jshint undef:false
2016-09-15 07:35:38 +08:00
var log = console.log.bind(console);
var logError = console.error.bind(console);
// jshint undef:true
2017-09-25 22:55:17 +08:00
var nodejsVersion = process.versions.node;
2017-09-25 23:34:16 +08:00
if (!semver.satisfies(nodejsVersion, '>=6.9.0')) {
2017-09-25 22:55:17 +08:00
logError(`Node version ${nodejsVersion} is not supported, please use Node.js 6.9 or higher.`);
process.exit(1);
}
// See https://github.com/CartoDB/support/issues/984
// CartoCSS properties text-wrap-width/text-wrap-character not working
2018-01-10 21:51:48 +08:00
// This function should be called before the require('yargs').
function setICUEnvVariable() {
2018-01-10 18:24:08 +08:00
if (process.env.ICU_DATA === undefined) {
2018-01-10 21:51:48 +08:00
let directory = glob.sync(__dirname + '/node_modules/mapnik/lib/binding/*/share/mapnik/icu/');
if (directory && directory.length > 0) {
process.env.ICU_DATA = directory[0];
2018-01-10 21:51:48 +08:00
}
}
}
setICUEnvVariable();
var argv = require('yargs')
.usage('Usage: $0 <environment> [options]')
.help('h')
.example(
'$0 production -c /etc/sql-api/config.js',
'start server in production environment with /etc/sql-api/config.js as config file'
)
.alias('h', 'help')
.alias('c', 'config')
.nargs('c', 1)
.describe('c', 'Load configuration from path')
.argv;
var environmentArg = argv._[0] || process.env.NODE_ENV || 'development';
var configurationFile = path.resolve(argv.config || './config/environments/' + environmentArg + '.js');
if (!fs.existsSync(configurationFile)) {
2016-09-15 07:35:38 +08:00
logError('Configuration file "%s" does not exist', configurationFile);
process.exit(1);
2015-04-07 19:00:20 +08:00
}
global.environment = require(configurationFile);
var ENVIRONMENT = argv._[0] || process.env.NODE_ENV || global.environment.environment;
process.env.NODE_ENV = ENVIRONMENT;
2015-07-05 05:09:00 +08:00
var availableEnvironments = {
production: true,
staging: true,
development: true
};
2011-09-05 07:00:41 +08:00
// sanity check
2015-07-05 05:09:00 +08:00
if (!availableEnvironments[ENVIRONMENT]){
logError('node app.js [environment]');
logError('environments: %s', Object.keys(availableEnvironments).join(', '));
process.exit(1);
2011-09-05 07:00:41 +08:00
}
2015-07-05 05:09:00 +08:00
process.env.NODE_ENV = ENVIRONMENT;
if (global.environment.uv_threadpool_size) {
process.env.UV_THREADPOOL_SIZE = global.environment.uv_threadpool_size;
}
// set global HTTP and HTTPS agent default configurations
// ref https://nodejs.org/api/http.html#http_new_agent_options
var agentOptions = _.defaults(global.environment.httpAgent || {}, {
keepAlive: false,
keepAliveMsecs: 1000,
maxSockets: Infinity,
maxFreeSockets: 256
});
http.globalAgent = new http.Agent(agentOptions);
https.globalAgent = new https.Agent(agentOptions);
global.log4js = require('log4js');
var log4jsConfig = {
appenders: [],
replaceConsole: true
};
if ( global.environment.log_filename ) {
var logFilename = path.resolve(global.environment.log_filename);
var logDirectory = path.dirname(logFilename);
if (!fs.existsSync(logDirectory)) {
logError("Log filename directory does not exist: " + logDirectory);
process.exit(1);
}
log("Logs will be written to " + logFilename);
log4jsConfig.appenders.push(
{ type: "file", absolute: true, filename: logFilename }
);
} else {
log4jsConfig.appenders.push(
{ type: "console", layout: { type:'basic' } }
);
}
global.log4js.configure(log4jsConfig);
2015-04-07 19:00:20 +08:00
global.logger = global.log4js.getLogger();
2015-07-05 05:09:00 +08:00
global.environment.api_hostname = require('os').hostname().split('.')[0];
// Include cartodb_windshaft only _after_ the "global" variable is set
// See https://github.com/Vizzuality/Windshaft-cartodb/issues/28
2015-07-05 05:09:00 +08:00
var cartodbWindshaft = require('./lib/cartodb/server');
var serverOptions = require('./lib/cartodb/server_options');
2015-07-05 05:09:00 +08:00
var server = cartodbWindshaft(serverOptions);
// 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
2015-09-18 22:25:10 +08:00
var backlog = global.environment.maxConnections || 128;
2015-09-18 22:25:10 +08:00
var listener = server.listen(serverOptions.bind.port, serverOptions.bind.host, backlog);
2014-03-11 19:21:00 +08:00
var version = require("./package").version;
2015-09-17 08:04:10 +08:00
listener.on('listening', function() {
2017-02-10 17:03:06 +08:00
log("Using Node.js %s", process.version);
log('Using configuration file "%s"', configurationFile);
log(
2015-08-24 18:45:21 +08:00
"Windshaft tileserver %s started on %s:%s PID=%d (%s)",
version, serverOptions.bind.host, serverOptions.bind.port, process.pid, ENVIRONMENT
2015-08-24 18:45:21 +08:00
);
});
setInterval(function() {
var memoryUsage = process.memoryUsage();
Object.keys(memoryUsage).forEach(function(k) {
global.statsClient.gauge('windshaft.memory.' + k, memoryUsage[k]);
});
}, 5000);
process.on('SIGHUP', function() {
global.log4js.clearAndShutdownAppenders(function() {
global.log4js.configure(log4jsConfig);
2015-04-07 19:00:20 +08:00
global.logger = global.log4js.getLogger();
log('Log files reloaded');
});
});
process.on('uncaughtException', function(err) {
2015-04-07 19:00:20 +08:00
global.logger.error('Uncaught exception: ' + err.stack);
});
if (global.gc) {
var gcInterval = Number.isFinite(global.environment.gc_interval) ?
global.environment.gc_interval :
10000;
if (gcInterval > 0) {
setInterval(function gcForcedCycle() {
var start = Date.now();
global.gc();
global.statsClient.timing('windshaft.gc', Date.now() - start);
}, gcInterval);
}
}