From e6cc098c9e7a22839950ff0c6f9fb966aedcd8e9 Mon Sep 17 00:00:00 2001 From: aman-tunerlabs Date: Fri, 11 Jun 2021 18:21:06 +0530 Subject: [PATCH 1/2] Added mongodb replica set configurations --- .env.sample | 5 ++++- config/db/mongodb.js | 11 +++++++---- 2 files changed, 11 insertions(+), 5 deletions(-) diff --git a/.env.sample b/.env.sample index e8d6d82c..9d6020f7 100644 --- a/.env.sample +++ b/.env.sample @@ -26,4 +26,7 @@ KAFKA_URL = "172.31.0.4:9092" KAFKA_GROUP_ID = "projects" // Kafka group id # SUBMISSION TOPIC -SUBMISSION_TOPIC = "dev.sl.projects.submissions" // Kafka topic name for pushing projects submissions \ No newline at end of file +SUBMISSION_TOPIC = "dev.sl.projects.submissions" // Kafka topic name for pushing projects submissions + +# Replica set name required for mongodb replicaSets +REPLICA_SET_NAME = "rs0" \ No newline at end of file diff --git a/config/db/mongodb.js b/config/db/mongodb.js index 1ba8f648..c7563ed3 100644 --- a/config/db/mongodb.js +++ b/config/db/mongodb.js @@ -23,12 +23,15 @@ const DB = function() { mongoose.set('useCreateIndex', true) mongoose.set('useFindAndModify', false) mongoose.set('useUnifiedTopology', true) + + let options = {useNewUrlParser: true}; + + if ( process.env.REPLICA_SET_NAME && process.env.REPLICA_SET_NAME !== "") { + options["replset"] = {rs_name: process.env.REPLICA_SET_NAME }; + } const db = mongoose.createConnection( - process.env.MONGODB_URL, - { - useNewUrlParser: true - } + process.env.MONGODB_URL,options ); db.on("error", console.error.bind(console, "connection error:")); From 3318f6db60d15e2e6e378d9584889dd51a69e6c7 Mon Sep 17 00:00:00 2001 From: aman-tunerlabs Date: Fri, 11 Jun 2021 19:31:10 +0530 Subject: [PATCH 2/2] Added read preference in replica set --- .env.sample | 5 ++++- config/db/mongodb.js | 6 +++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/.env.sample b/.env.sample index 9d6020f7..1ba95367 100644 --- a/.env.sample +++ b/.env.sample @@ -29,4 +29,7 @@ KAFKA_GROUP_ID = "projects" SUBMISSION_TOPIC = "dev.sl.projects.submissions" // Kafka topic name for pushing projects submissions # Replica set name required for mongodb replicaSets -REPLICA_SET_NAME = "rs0" \ No newline at end of file +REPLICA_SET_NAME = "rs0" + +# Replica set read preference +REPLICA_SET_READ_PREFERENCE = "primary" \ No newline at end of file diff --git a/config/db/mongodb.js b/config/db/mongodb.js index c7563ed3..f2253813 100644 --- a/config/db/mongodb.js +++ b/config/db/mongodb.js @@ -26,8 +26,12 @@ const DB = function() { let options = {useNewUrlParser: true}; - if ( process.env.REPLICA_SET_NAME && process.env.REPLICA_SET_NAME !== "") { + if (process.env.REPLICA_SET_NAME && process.env.REPLICA_SET_NAME !== "") { options["replset"] = {rs_name: process.env.REPLICA_SET_NAME }; + + if (process.env.REPLICA_SET_READ_PREFERENCE && process.env.REPLICA_SET_READ_PREFERENCE !== "") { + options["replset"]["readPreference"] = process.env.readPreference; + } } const db = mongoose.createConnection(