retry after MongooseServerSelectionError connection timed out - node.js

I want my nodejs application to retry after the inital connecton to mongoose fails. I connect to two separate databases in my application. Below is the code snippet for your reference.
function makeNewConnection(uri) {
const db = mongoose.createConnection(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
serverSelectionTimeoutMS: 180000
});
db.on('error', async function (error) {
console.log(`MongoDB :: connection ${this.name} ${JSON.stringify(error)}`);
db.close().catch(() => console.log(`MongoDB :: failed to close connection ${this.name}`));
});
db.on('connected', function () {
mongoose.set('debug', false);
console.log(`MongoDB :: connected ${this.name}`);
});
db.on('disconnected', function () {
console.log(`MongoDB :: disconnected ${this.name}`);
});
return db;
}
const localDBconnection = makeNewConnection(localDB);
const serverDBConnection = makeNewConnection(serverDB);
localDB and serverDB are my two connection uri's.
In case i retry with a timeout then it does not find the model instance. can someone suggest a clean way to do it.

Related

Node.js/Express: How Do I Share or Export Mongo Database Connection To Other Modules?

I am trying to share the Mongo connection with other modules in my Node.js project. I keep getting either undefined or is not a function when attempting to use the exported client. I also had a question around detecting if the connection is in fact open before performing operations on the database.
It seems like using the app.locals would be the proper way to share the connection but I could not get that working either. Below is what I have at the moment. I've tried this many ways. Most of what I can find online seems to export the Mongo Node driver's method, not the connection itself. The idea is to connect once and never disconnect until the app shuts down.
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
async function connect () {
app.locals.dbConnected = false;
try {
await client.connect();
app.locals.dbConnected = true;
module.exports = client;
} catch (e) {
console.error(e);
}
};
then in another module do something like:
await client.db('syslogs').collection('production').insertOne(doc);
Is it possible to share the connection?
Could do something like below:
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
let __inst = null;
export default new Promise((resolve, reject) => {
if (__inst !== null) resolve(__inst);
// the open event is the key here
// like this we can handle error, close etc thru events as well
client.open((err, mongoInst) => {
if (err) reject(err);
__inst = mongoInst;
resolve(__inst);
});
});
Then in other module you can use the export client like you want.
Thanks.
I just got it working using app.locals.
index.js
const { MongoClient } = require("mongodb");
const client = new MongoClient(uri, {
useNewUrlParser: true,
useUnifiedTopology: true,
});
(async () => {
app.locals.dbConnected = false;
try {
await client.connect();
console.log("Connected to DB");
app.locals.client = client;
app.listen(PORT, HOST, () => {
console.log(`Running on http://${HOST}:${PORT}`);
});
} catch (e) {
console.error(e);
}
})();
Then in my module:
async function index (req, res) {
try {
let db = req.app.locals.client.db("admin");
await db.command({ ping: 1 });
console.log("pinged admin database");
}catch(err) {
console.log(err);
}
}

Unable to close mongoose connection in node.js

I am unable to close mongoose connection when I am storing mongoose.connection in a variable called db and creating a separate method to closing the connection via variable db
I have tried both method db.close & mongoose.disconnect()
const mongoose = require('mongoose');
var db;
const connectToDB = function (callback) {
let dbUrl;
if (environmentTokens.enviroment === "test") {
dbUrl = localDBUrl;
} else {
dbUrl = environmentTokens.mongoDBUrl;
}
mongoose.connect(dbUrl);
db = mongoose.connection
db.on('error', (err) => {
tracer.error('Connection error with database', err);
})
db.on('connected', () => {
tracer.info('Connected with database', dbUrl);
console.log('Mongoose default connection connected');
callback();
})
};
const getDB = function () {
return db;
};
const disconnectDB = function () {
db.close(function () {
console.log('Mongoose default connection disconnected through app termination')})
// mongoose.disconnect(function () {
// console.log('Mongoose default connection disconnected through app termination')})
}
module.exports = {
connectToDB, disconnectDB, getDB
};
And calling the disconnectDB method from index.js file
process.on('SIGINT', () => {
disconnectDB();
process.exit()
})
The connection is closing, but your program is exiting before the callback is invoked.
To confirm, try:
process.on('SIGINT', () => {
disconnectDB(true);
})
const disconnectDB = function (exit=false) {
db.close(function () {
console.log('Mongoose default connection disconnected through app termination')})
if (exit) process.exit()
})
// mongoose.disconnect(function () {
// console.log('Mongoose default connection disconnected through app termination')})
}

tunnel-ssh throws an error after getting connected

I'm trying to connect to mongodb(mongodb package) using the tunnel-ssh package. It gets connected and I can log the db but it immediately throws an error and disconnects.
buffer.js:705
throw new ERR_INVALID_ARG_TYPE(
^
TypeError [ERR_INVALID_ARG_TYPE]: The "string" argument must be one of type string, Buffer, or ArrayBuffer. Received type undefined
at Function.byteLength (buffer.js:705:11)
at SSH2Stream.directTcpip (D:\WORK\node_modules\ssh2-streams\lib\ssh.js:1128:23)
at openChannel (D:\WORK\node_modules\ssh2\lib\client.js:1142:21)
at Client.forwardOut (D:\WORK\node_modules\ssh2\lib\client.js:994:10)
at Client.<anonymous> (D:\WORK\node_modules\tunnel-ssh\index.js:16:23)
at Client.emit (events.js:223:5)
at SSH2Stream.<anonymous> (D:\WORK\node_modules\ssh2\lib\client.js:601:10)
at Object.onceWrapper (events.js:312:28)
at SSH2Stream.emit (events.js:223:5)
at parsePacket (D:\WORK\node_modules\ssh2-streams\lib\ssh.js:3911:10) {
code: 'ERR_INVALID_ARG_TYPE'
}
This is my code.
const tunnel = require("tunnel-ssh");
const config = require("config");
const MongoClient = require("mongodb").MongoClient;
const connection = new Promise((resolve, _) => {
// eslint-disable-next-line
tunnel(config.get("server"), async (err, server) => {
server.on("connection", console.log.bind(console, "server error"));
const client = await MongoClient.connect(config.get("mongodb").url, {
useUnifiedTopology: true,
useNewUrlParser: true
});
client.on("error", console.error.bind(console, "mongodb error"));
resolve({ client });
});
});
async function runQuery() {
const { client} = await connection;
console.log(client);
}
runQuery();
There is no problem with config. In fact, the logging in runQuery function works but throws that error immediately.
I have not used the tunnel-ssh package you have mentioned, but I went through the docs and I see that you are using it wrong. I simply copied the configuration given in the docs of tunnel-ssh and it started working for me. pasting the entire code below
const tunnel = require("tunnel-ssh");
const MongoClient = require("mongodb").MongoClient;
const connection = new Promise((resolve, _) => {
// eslint-disable-next-line
tunnel(
{
username: "root",
Password: "secret",
host: "127.0.0.1",
port: 22,
dstHost: "127.0.0.1",
dstPort: 27017,
localHost: "127.0.0.1",
localPort: 27000
},
async (err, server) => {
server.on("connection", console.log.bind(console, "server error"));
const client = await MongoClient.connect(
"mongodb://localhost:27017/user",
{
useUnifiedTopology: true,
useNewUrlParser: true
}
);
client.on("error", console.error.bind(console, "mongodb error"));
resolve({ client });
server.close();
}
);
});
async function runQuery() {
const { client } = await connection;
console.log("Connection Successful");
}
runQuery();
The part where you went wrong is passing string to tunnel package. it expects configuration object not string.

Mongoose connection events with createConnection

I moved away from using the default connection to a explicitly defined connection.
My code is working except that none of the event handlers which track the state of my mongo connection are firing.
the events used to fire when I was using default mongoose connection. (mongoose.connect).
var mongoose = require('mongoose'),
logger = require('../logger'),
config = require('../config');
mongoose.connection.on('connecting', function(){
logger.info("trying to establish a connection to mongo");
});
mongoose.connection.on('connected', function() {
logger.info("connection established successfully");
});
mongoose.connection.on('error', function(err) {
logger.error('connection to mongo failed ' + err);
});
mongoose.connection.on('disconnected', function() {
logger.log('mongo db connection closed');
})
var gracefulExit = function() {
db.close(function(){
logger.log("mongoose connection with db " + server + 'is closing');
process.exit(0);
});
};
process.on('SIGNT', gracefulExit).on('SIGTERM', gracefulExit);
var db = mongoose.createConnection(config.get('DB_URL'));
module.exports = db;
the problem with the code is
None of the event handlers are firing.
Even the process events on SIGNT and SIGTERM are not firing.
I also tried
var db = mongoose.createConnection(config.get('DB_URL'));
db.on('connecting', function(){
logger.info("trying to establish a connection to mongo");
});
but this also doesn't fire.
i also faced with this problem,
and finally i solved it, after understood that the connect() / createConnection() execute before i declare the event handlers.
so the my open or connected events handlers will never called again.
so by declare them first and just after trying to connect - will solve your problem!
for example:
try {
mongoose.connection
.on('error', err => {
console.error(err);
})
.on('open', err => {
console.log(`DB connected`);
})
await mongoose.connect('mongodb://localhost:27017/test', { useNewUrlParser: true });
} catch (error) {
handleError(error);
}
This snippet blow work fine for me
const connection = mongoose.createConnection(`mongodb://${process.env.MONGO_URI}`);
connection.on('connected', () => {
console.log('connected to mongodb');
});
connection.on('disconnected', () => {
console.log('connection disconnected');
});
I just stumbled over this issue myself and found that I needed to set up the EventListeners before the actual connection to the database like so (with async/await):
export class Database {
public static connect( databaseCredentials: string ) {
return new Promise( async resolve => {
// ! The `EventListeners` is setup here.
mongoose.connection.on( "connected", () => {
logger.info( "Database has connected successfully." );
});
mongoose.connection.on( "error", (error) => {
logger.error( " Obs! There was an unexpected error connecting to the database.", error );
});
// ! The acutal connection to the database happens here.
await mongoose.connect( databaseCredentials, {
useNewUrlParser: true,
useUnifiedTopology: true,
keepAlive: true,
reconnectInterval: 500,
connectTimeoutMS: 10000,
});
resolve();
});
}
}
This should also work the same way with mongoose.createConnections.
This will yield the result (if successful):
{ message: 'Database has connected successfully.',
level: 'info',
timestamp: '2019-10-11T15:17:16.616Z' }
I just wanted to chip in and give my solution to the problem at hand and give a full example implementation of the code, as the above answers didn't explain in which order you needed to listen for the events. If you were to put the mongoose.connection.on() below the await.mongoose.connect(), this would not yield any response from the EventListeners.
Edit: Typos
try:
mongoose.connection.on('open', function() {
logger.info("database is ready now");
});
also temporary remove .close()

node js cluster mongoose connection

I am using nodejs built in cluster module in my application mongodb( not sharded) used for
storage in each cluster(worker) connection is made using mongoose.createConnection method and closes after inserting data.
But what i am expecting is whenever a request is made it opens connection to the db and process request and close the connection.
But what i noticed that when i checking the mongodb log still open connection and its count sligtly larger than no of processor/(cluster nodes).
and i put poolSize:1,autreconect:false still some connections are not closing even after close() method is called.
my observations are when connection error happends the connection is not closed
Please help me
I am using following script to get the connection .
module.exports.getDb = function () {
var dburl = 'mongodb://localhost/DB';
db = mongoose.createConnection(dburl, {
server: {
socketOptions: {
keepAlive: 1,
connectTimeoutMS: 30000
},
auto_reconnect: false,
poolSize: 1
}
}, function (err) {
console.log(err)
});
db.on('error', function (err) {
console.log(err + " this is error");
db.close();
});
return db;
}
and i close the conneciton using db.close() in the end of evey query callback.
Are you looking for something like this?
db.connection.on('error', function (e) {
console.log(e + " this is error");
db.close();
});
For my API server, I wrote a plugin for Hapi to handle this. Take a look, it might help you.
'use strict';
var bluebird = require('bluebird');
var mongoose = bluebird.promisifyAll(require('mongoose'));
exports.register = function(plugin, options, next) {
mongoose.connect(options.mongo.uri, options.mongo.options, function (e) {
if (e) {
plugin.log(['error', 'database', 'mongodb'], 'Unable to connect to MongoDB: ' + e.message);
process.exit();
}
mongoose.connection.once('open', function () {
plugin.log(['info', 'database', 'mongodb'], 'Connected to MongoDB # ' + options.mongo.uri);
});
mongoose.connection.on('connected', function () {
plugin.log(['info', 'database', 'mongodb'], 'Connected to MongoDB # ' + options.mongo.uri);
});
mongoose.connection.on('error', function (e) {
plugin.log(['error', 'database', 'mongodb'], 'MongoDB ' + e.message);
});
mongoose.connection.on('disconnected', function () {
plugin.log(['warn', 'database', 'mongodb'], 'MongoDB was disconnected');
});
});
return next();
};
exports.register.attributes = {
name: 'mongoose',
version: '1.0.0'
};

Resources