-
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?
Changes from 4 commits
6457073
f193650
bfa69b9
a96af0e
a78bbf5
caea764
41f46d7
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -52,6 +52,14 @@ 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 commentThe 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?
uncle bob and I would also be very happy if you broke that down further into
|
||
if (!mongoose.connection._hasOpened) { | ||
mongooseControl._exitIfFailedToOpen() | ||
} else { | ||
mongooseControl._exitIfFailedToReconnect() | ||
} | ||
}) | ||
} | ||
|
||
mongooseControl.stop = function (cb) { | ||
|
@@ -63,3 +71,18 @@ mongooseControl.stop = function (cb) { | |
}) | ||
}) | ||
} | ||
|
||
mongooseControl._exitIfFailedToOpen = function () { | ||
log.fatal({message: 'Failed to connect to ' + process.env.MONGO + ' failed to establish a connection to mongodb'}) | ||
process.exit(1) | ||
} | ||
|
||
mongooseControl._exitIfFailedToReconnect = function () { | ||
log.error({message: 'Failed to connect to ' + process.env.MONGO + ' failed to establish a connection to mongodb'}) | ||
setTimeout(function () { | ||
if (!mongoose.connection.readyState) { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. lets add log here as well |
||
log.fatal({message: 'Exiting nodejs process due to mongodb connection failure'}) | ||
process.exit(1) | ||
} | ||
}, process.env.DB_CONNECTION_TIMEOUT) | ||
} |
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -11,6 +11,7 @@ var Code = require('code') | |
var fs = require('fs') | ||
var mongoose = require('mongoose') | ||
var sinon = require('sinon') | ||
var clock | ||
|
||
var mongooseControl = require('models/mongo/mongoose-control') | ||
|
||
|
@@ -103,5 +104,73 @@ describe('mongoose-control', function () { | |
done() | ||
}) | ||
}) | ||
|
||
describe('handling mongodb disconnect events', function () { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. add unit test for you can use sinon to mock timers as well. |
||
beforeEach(function (done) { | ||
sinon.stub(mongoose.connection, 'on').yields() | ||
sinon.stub(process, 'exit') | ||
sinon.stub(mongooseControl, '_exitIfFailedToReconnect') | ||
sinon.stub(mongooseControl, '_exitIfFailedToOpen') | ||
done() | ||
}) | ||
|
||
afterEach(function (done) { | ||
mongooseControl._exitIfFailedToReconnect.restore() | ||
mongooseControl._exitIfFailedToOpen.restore() | ||
mongoose.connection.on.restore() | ||
process.exit.restore() | ||
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() | ||
}) | ||
}) | ||
}) | ||
|
||
describe('exiting node process when db disconnects', function () { | ||
beforeEach(function (done) { | ||
clock = sinon.useFakeTimers() | ||
sinon.stub(mongoose.connection, 'on').yields() | ||
sinon.stub(process, 'exit') | ||
done() | ||
}) | ||
|
||
afterEach(function (done) { | ||
mongoose.connection.on.restore() | ||
process.exit.restore() | ||
clock.restore() | ||
done() | ||
}) | ||
|
||
it('should exit immediately if it cannot connect', function (done) { | ||
mongooseControl._exitIfFailedToOpen() | ||
sinon.assert.calledOnce(process.exit) | ||
done() | ||
}) | ||
|
||
it('should attempt to reconnect when it was connected once', function (done) { | ||
mongooseControl._exitIfFailedToReconnect() | ||
clock.tick(1000) | ||
sinon.assert.notCalled(process.exit) | ||
clock.tick(60000) | ||
sinon.assert.calledOnce(process.exit) | ||
done() | ||
}) | ||
}) | ||
}) | ||
}) |
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