Rapidfacture / mongoose-multi

NPM Module: Create Mongoose connections to severals DBs; short, mongoDB like syntax for requests.
12 stars 4 forks source link

closing connections causes delay #10

Closed mergingseas closed 5 years ago

mergingseas commented 5 years ago

I have 2 connections and when I disconnect I get the following message and a long pause before I get the shutting down message. How can I close the connections and shutdown immediately?

[mongoose-multi] DB local disconnected, mongodb://*** [mongoose-multi] DB global disconnected, mongodb://****

........(long pause...)

[mongoose-multi] shutting down application for restart: Try to reconnet DB.

FelixFurtmayr commented 5 years ago

Thank you for the info.

Currently I do the restart really not the clean way: process.exit(0); This means the app is terminated then and restarts. I did this due to trouble with mongoose and especially mongoose auto reconnect which was not working in many versions.

If you want to disconnect, you should use just the original mongoose connection direct and do the disconnect: You get the connection for database 'application' under 'db.application.mongooseConnection'. You should be able to just disconnect the connection.

Maybe this helps you so far, otherwise please provide your code. By the way: Any contribution on this project is appreciated.

Regards Felix

mergingseas commented 5 years ago

@FelixFurtmayr Thanks for your response. My current code is simply:

db.local.mongooseConnection.close();
db.global.mongooseConnection.close();

Ive got 2 databases, local and global and and I use the mongooseConnection.close() to close them after which I was getting the message

[mongoose-multi] DB local disconnected, mongodb://*** [mongoose-multi] DB global disconnected, mongodb://****

........(long pause...)

[mongoose-multi] shutting down application for restart: Try to reconnet DB.

The long pause is what we are concerned with.

Our team thinks this is a very useful project and we'd love to keep testing it and possibly develop for it too.

Keep up the good work

mergingseas commented 5 years ago

ok so I just peeked at the code a bit and I think this is happening:

dbcon.on('disconnected', function () {
         log.error('[mongoose-multi] DB ' + name + ' disconnected, ' + url);

         // there have been several issues with reconnecting
         // we simple restart the whole process and try it again
         if (options.auto_reconnect !== false) {
            setTimeout(function () {
               log.error('[mongoose-multi] shutting down application for restart: Try to reconnet DB.');
               process.exit(0);
            }, 10000);
         }
      });

I believe I need to pass the auto_reconnect = false option

FelixFurtmayr commented 5 years ago

"I believe I need to pass the auto_reconnect = false option" Yes, then it will disable the restart.

Maybe the auto_reconnect works in the current version and we do not need this killing of the process any longer. Have not tested it.

"Our team thinks this is a very useful project and we'd love to keep testing it and possibly develop for it too." Would be nice. Feel free to make a any pull request. Currently the project could need some test cases.

Regards Felix

FelixFurtmayr commented 5 years ago

I think passing auto_reconnect = false did work, closing the issue.