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
Open
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 22 additions & 0 deletions lib/models/mongo/mongoose-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ 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 () {...}

if (!mongoose.connection._hasOpened) {
mongooseControl._exitIfFailedToOpen()
} else {
mongooseControl._exitIfFailedToReconnect()
}
})
}

mongooseControl.stop = function (cb) {
Expand All @@ -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)

process.exit(1)
}

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

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

process.exit(1)
}
}, 10000)
}
29 changes: 29 additions & 0 deletions unit/models/mongo/mongoose-control.js
Original file line number Diff line number Diff line change
Expand Up @@ -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.


beforeEach(function (done) {
sinon.stub(mongoose.connection, 'on').yields()
sinon.stub(mongooseControl, '_exitIfFailedToReconnect')
sinon.stub(mongooseControl, '_exitIfFailedToOpen')
done()
})

it('should exit if it cannot connect', function (done) {
mongooseControl.start(function (err) {
expect(err).to.not.exist()
sinon.assert.notCalled(mongooseControl._exitIfFailedToReconnect)
sinon.assert.calledOnce(mongooseControl._exitIfFailedToOpen)
done()
})
})

it('should attempt a retry if connection existed', function (done) {
mongoose.connection._hasOpened = true
mongooseControl.start(function (err) {
expect(err).to.not.exist()
sinon.assert.notCalled(mongooseControl._exitIfFailedToOpen)
sinon.assert.calledOnce(mongooseControl._exitIfFailedToReconnect)
done()
})
})
})
})
})