Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

wait for database to initialize before db calls #98

Merged
merged 1 commit into from
May 7, 2021
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.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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,
};