Skip to content
This repository has been archived by the owner on Mar 7, 2018. It is now read-only.

Block server start until Cassandra is initialized #98

Merged
merged 3 commits into from Aug 15, 2017
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
14 changes: 10 additions & 4 deletions server.js
Expand Up @@ -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}));

Expand Down Expand Up @@ -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 () {});
serverStartBlocker.then(startServer).catch(console.error);
10 changes: 10 additions & 0 deletions src/clients/cassandra/CassandraConnector.js
Expand Up @@ -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')
Expand Down