Skip to content
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

Open
wants to merge 7 commits into
base: master
Choose a base branch
from

Conversation

henrymollman
Copy link
Contributor

@henrymollman henrymollman commented Oct 7, 2016

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.

  • I added the listener immediately after mongoose begins attempting to make a connection to mongodb.

To test, API must be run with

  1. without a connection to mongodb, so that node exits immediately on start,
  2. with a valid connection that is severed so that node exits after 10 seconds, and
  3. with a valid connection that is severed, then reconnected, so that api functionality continues as normal.
  • Integration Tested @ commitsh by person_3 on (gamma|epsilon|staging)

Copy link
Contributor

@anandkumarpatel anandkumarpatel left a 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

@@ -52,6 +52,19 @@ mongooseControl.start = function (cb) {
}

mongoose.connect(process.env.MONGO, mongooseOptions, cb)
mongoose.connection.on('disconnected', function () {
Copy link
Contributor

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 () {...}

@@ -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')
Copy link
Contributor

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)

}

mongooseControl._exitIfFailedToReconnect = function() {
log.error({message: 'Lost connection to ' + process.env.MONGO})
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

same as above

mongooseControl._exitIfFailedToReconnect = function() {
log.error({message: 'Lost connection to ' + process.env.MONGO})
setTimeout(function () {
if (!mongoose.connection.readyState) {
Copy link
Contributor

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 () {
Copy link
Contributor

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.

@henrymollman henrymollman force-pushed the shutdown-api-on-db-disconnect branch from fe7541a to bfa69b9 Compare October 11, 2016 01:06
@henrymollman henrymollman force-pushed the shutdown-api-on-db-disconnect branch from 643c43b to a96af0e Compare October 11, 2016 17:24
@@ -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
Copy link
Contributor

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

@anandkumarpatel
Copy link
Contributor

@henrymollman can we merge this?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants