diff --git a/server.js b/server.js index 0f81529..c2dd49f 100644 --- a/server.js +++ b/server.js @@ -35,8 +35,6 @@ app.use(function(req, res, next) { } }); -console.log('PORT: ' + port); - app.use(bodyParser.json({limit: '50mb'})); app.use(bodyParser.urlencoded({limit: '50mb', extended: true})); @@ -64,6 +62,14 @@ app.use('/api/settings', graphqlHTTP({ graphiql: true })); -const server = http.createServer(app); +function startServer() { + console.log(`PORT: ${port}`); + const server = http.createServer(app); + server.listen(port, function () {}); +} + +const serverStartBlocker = process.env.ENABLE_V2 + ? require('./src/clients/cassandra/CassandraConnector').initialize() + : require('promise').resolve(); -server.listen(port, function () {}); \ No newline at end of file +serverStartBlocker.then(startServer).catch(console.error); \ No newline at end of file diff --git a/src/clients/cassandra/CassandraConnector.js b/src/clients/cassandra/CassandraConnector.js index bc530df..2099893 100644 --- a/src/clients/cassandra/CassandraConnector.js +++ b/src/clients/cassandra/CassandraConnector.js @@ -108,7 +108,17 @@ function executeQueries(queries) { }); } +/** + * Should be called on server start to warm up the connection to + * Cassandra so that subsequent calls are fast. + * @returns {Promise} + */ +function intialize() { + return executeQuery('select sitename from fortis.sitesettings limit 1', []); +} + module.exports = { + initialize: trackDependency(intialize, 'Cassandra', 'initialize'), executeBatchMutations: trackDependency(executeBatchMutations, 'Cassandra', 'executeBatchMutations'), executeQueries: trackDependency(executeQueries, 'Cassandra', 'executeQueries'), executeQuery: trackDependency(executeQuery, 'Cassandra', 'executeQuery')