Added listener to exit process when db connection fails#1752
Added listener to exit process when db connection fails#1752henrymollman wants to merge 7 commits intomasterfrom
Conversation
anandkumarpatel
left a comment
There was a problem hiding this comment.
Also add unit test please
| } | ||
|
|
||
| mongoose.connect(process.env.MONGO, mongooseOptions, cb) | ||
| mongoose.connection.on('disconnected', function () { |
There was a problem hiding this comment.
nice, to clean this up a bit can you move this to a helper function?
mongooseControl._handleDisconnect()
uncle bob and I would also be very happy if you broke that down further into
mongooseControl._exitIfNotOpened() {...}
mongooseControl._exitIfConnectionNeverMade () {...}
lib/models/mongo/mongoose-control.js
Outdated
| } | ||
|
|
||
| mongooseControl._exitIfFailedToOpen = function () { | ||
| log.fatal({message: 'Failed to connect to ' + process.env.MONGO}, 'failed to establish a connection to mongodb') |
There was a problem hiding this comment.
nit: you dont need message,
log.fatal(Failed to connect to ${process.env.MONGO} failed to establish a connection to mongodb)
lib/models/mongo/mongoose-control.js
Outdated
| } | ||
|
|
||
| mongooseControl._exitIfFailedToReconnect = function() { | ||
| log.error({message: 'Lost connection to ' + process.env.MONGO}) |
| mongooseControl._exitIfFailedToReconnect = function() { | ||
| log.error({message: 'Lost connection to ' + process.env.MONGO}) | ||
| setTimeout(function () { | ||
| if (!mongoose.connection.readyState) { |
There was a problem hiding this comment.
lets add log here as well
| }) | ||
| }) | ||
|
|
||
| describe('handling mongodb disconnect events', function () { |
There was a problem hiding this comment.
add unit test for _exitIfFailedToReconnect and _exitIfFailedToOpen please.
you can use sinon to mock timers as well.
fe7541a to
bfa69b9
Compare
643c43b to
a96af0e
Compare
configs/.env.staging
Outdated
| AWS_SECRET_ACCESS_KEY=pba1hML8v59SYMF90zBF/luXMagSuNg0TPFfv3e0 | ||
| BUNYAN_BATCH_LOG_COUNT=5 | ||
| DATADOG_HOST=datadog-staging-codenow.runnableapp.com | ||
| DB_CONNECTION_TIMEOUT=10000 |
There was a problem hiding this comment.
if values are the same across env's you should put them in the .env file which is shared by all env's
|
@henrymollman can we merge this? |
Added disconnect listener on the mongoose db connector to exit the node process when the db fails to connect on startup or disconnects while api is running and does not reconnect in 10 seconds.
To test, API must be run with