Skip to content

Commit 6457073

Browse files
author
Henry Mollman
committed
Added listener to exit process when db connection fails
1 parent f65d8f5 commit 6457073

File tree

1 file changed

+13
-0
lines changed

1 file changed

+13
-0
lines changed

lib/models/mongo/mongoose-control.js

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,19 @@ mongooseControl.start = function (cb) {
5252
}
5353

5454
mongoose.connect(process.env.MONGO, mongooseOptions, cb)
55+
mongoose.connection.on('disconnected', function () {
56+
if (!mongoose.connection._hasOpened) {
57+
log.fatal({message: 'Failed to connect to ' + process.env.MONGO}, 'failed to establish a connection to mongodb')
58+
process.exit(1)
59+
} else {
60+
log.error({message: 'Lost connection to ' + process.env.MONGO})
61+
setTimeout(function () {
62+
if (!mongoose.connection.readyState) {
63+
process.exit(1)
64+
}
65+
}, 10000)
66+
}
67+
})
5568
}
5669

5770
mongooseControl.stop = function (cb) {

0 commit comments

Comments
 (0)