Skip to content

Commit

Permalink
fix(db): await before dataTransformationHandler call
Browse files Browse the repository at this point in the history
- wait for database to initialize before calling dataTransformationHandler
  • Loading branch information
YashKumarVerma committed May 7, 2021
1 parent 8221a6f commit a986e1b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 7 deletions.
13 changes: 11 additions & 2 deletions caracal.js
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down Expand Up @@ -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

9 changes: 4 additions & 5 deletions service/database/connector.js
Original file line number Diff line number Diff line change
Expand Up @@ -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}`;

Expand All @@ -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
Expand All @@ -58,4 +55,6 @@ const getConnection = (databaseName = "camic") => {
/** export the connector to be used by utility functions */
module.exports = {
getConnection,
connector,
};

0 comments on commit a986e1b

Please sign in to comment.