Skip to content

Commit

Permalink
disconnecting mongoose before attempting to reconnect on errors
Browse files Browse the repository at this point in the history
  • Loading branch information
jonaslan committed Dec 30, 2016
1 parent 8f8083c commit 08007dd
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 6 deletions.
12 changes: 7 additions & 5 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,11 +59,13 @@ function _connect (options) {
mongoose.connection.on('error', dbErr => {
log.warn({ err: dbErr.message }, 'DB connection error, retrying in 30 seconds')
isOk = false
// reject(dbErr)
setTimeout(function () {
mongoose.connect(dbUri, dbOptions)
log.info('Attempting to reconnect')
}, options.reconnectInterval || RECONNECT_TIMEOUT)
// disconnect if connection is still open
mongoose.disconnect().then(() => {
setTimeout(function () {
mongoose.connect(dbUri, dbOptions)
log.info('Attempting to reconnect')
}, options.reconnectInterval || RECONNECT_TIMEOUT)
})
})

mongoose.connection.on('connected', () => {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "kth-node-mongo",
"version": "1.0.3",
"version": "1.0.4",
"description": "Database connection module for Node.js applications using Mongoose.",
"main": "index.js",
"scripts": {
Expand Down

0 comments on commit 08007dd

Please sign in to comment.