-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Added listener to exit process when db connection fails #1752
base: master
Are you sure you want to change the base?
Conversation
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Also add unit test please
lib/models/mongo/mongoose-control.js
Outdated
@@ -52,6 +52,19 @@ mongooseControl.start = function (cb) { | |||
} | |||
|
|||
mongoose.connect(process.env.MONGO, mongooseOptions, cb) | |||
mongoose.connection.on('disconnected', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -63,3 +71,17 @@ mongooseControl.stop = function (cb) { | |||
}) | |||
}) | |||
} | |||
|
|||
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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}) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
same as above
lib/models/mongo/mongoose-control.js
Outdated
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.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
lets add log here as well
@@ -103,5 +103,34 @@ describe('mongoose-control', function () { | |||
done() | |||
}) | |||
}) | |||
|
|||
describe('handling mongodb disconnect events', function () { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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
@@ -3,6 +3,7 @@ AWS_ACCESS_KEY_ID=AKIAJXF6CNCWBWNX7JZQ | |||
AWS_SECRET_ACCESS_KEY=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 | |||
BUNYAN_BATCH_LOG_COUNT=5 | |||
DATADOG_HOST=datadog-staging-codenow.runnableapp.com | |||
DB_CONNECTION_TIMEOUT=10000 |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
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