From a986e1b215d33e3bed5fb4b4982af077f21276ea Mon Sep 17 00:00:00 2001 From: Yash Kumar Verma Date: Sat, 1 May 2021 09:54:17 +0000 Subject: [PATCH] fix(db): await before dataTransformationHandler call - wait for database to initialize before calling dataTransformationHandler --- caracal.js | 13 +++++++++++-- service/database/connector.js | 9 ++++----- 2 files changed, 15 insertions(+), 7 deletions(-) diff --git a/caracal.js b/caracal.js index 5d829c8..8516348 100644 --- a/caracal.js +++ b/caracal.js @@ -24,6 +24,8 @@ const Model = require('./handlers/modelTrainer.js'); const DataTransformationHandler = require('./handlers/dataTransformationHandler.js'); // TODO validation of data +const {connector} = require("./service/database/connector"); + var WORKERS = process.env.NUM_THREADS || 4; var PORT = process.env.PORT || 4010; @@ -196,7 +198,14 @@ var startApp = function(app) { throng(WORKERS, startApp(app)); -const handler = new DataTransformationHandler(MONGO_URI, './json/configuration.json'); -handler.startHandler(); +/** initialize DataTransformationHandler only after database is ready */ +connector.init().then(() => { + const handler = new DataTransformationHandler(MONGO_URI, './json/configuration.json'); + handler.startHandler(); +}).catch((e) => { + console.error("error connecting to database"); + process.exit(1); +}); module.exports = app; // for tests + diff --git a/service/database/connector.js b/service/database/connector.js index 6b28ab7..b24d580 100644 --- a/service/database/connector.js +++ b/service/database/connector.js @@ -15,7 +15,8 @@ class MongoDBConnector { */ constructor() { /** connection specifics */ - const connectionString = process.env.MONGO_URI || "mongodb://127.0.0.1:27017"; + const connectionString = + process.env.MONGO_URI || "mongodb://127.0.0.1:27017"; const databaseName = process.env.MONGO_DB || "camic"; const url = `${connectionString}/${databaseName}`; @@ -42,10 +43,6 @@ class MongoDBConnector { /** initialize an instance of mongoDB connection and kill process if connection fails */ const connector = new MongoDBConnector(); -connector.init().catch((e) => { - console.error("error connecting to database"); - process.exit(1); -}); /** * to load connection instances in database operations @@ -58,4 +55,6 @@ const getConnection = (databaseName = "camic") => { /** export the connector to be used by utility functions */ module.exports = { getConnection, + connector, }; +