Skip to content

Commit 35637b6

Browse files
committed
setting up mongo for reconnect policy and lenient first connect
1 parent 53d9a86 commit 35637b6

File tree

2 files changed

+40
-43
lines changed

2 files changed

+40
-43
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,9 @@
44
.idea
55
*.iml
66

7+
# VSCODE
8+
.vscode
9+
710
# Eclipse specific
811
.jshintrc
912
.project

index.js

Lines changed: 37 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,29 @@ const stdLogger = {
1212
warn: consoleLogger('warn'),
1313
error: consoleLogger('error')
1414
}
15+
const RECONNECT_TIMEOUT = 30000
16+
17+
let standardOptions = {
18+
db: {
19+
native_parser: true
20+
},
21+
server: {
22+
socketOptions: {
23+
keepAlive: 1
24+
},
25+
ssl: false,
26+
authenticationDatabase: '',
27+
auto_reconnect: true,
28+
reconnectInterval: RECONNECT_TIMEOUT
29+
},
30+
replset: {
31+
socketOptions: {
32+
keepAlive: 1
33+
}
34+
},
35+
user: '',
36+
pass: ''
37+
}
1538

1639
function consoleLogger (level) {
1740
return function (file, msg) {
@@ -41,9 +64,13 @@ function _connect (options, sslOptions) {
4164
log.info('Connect DB: ' + options.dbUri)
4265

4366
mongoose.connection.on('error', dbErr => {
44-
log.warn({ err: dbErr }, 'DB connection error')
67+
log.warn({ err: dbErr.message }, 'DB connection error, retrying in 30 seconds')
4568
isOk = false
46-
reject(dbErr)
69+
// reject(dbErr)
70+
setTimeout(function () {
71+
mongoose.connect(dbUri, dbOptions)
72+
log.info('Attempting to reconnect')
73+
}, options.reconnectInterval || RECONNECT_TIMEOUT)
4774
})
4875

4976
mongoose.connection.on('connected', () => {
@@ -65,52 +92,19 @@ function _connect (options, sslOptions) {
6592
}
6693

6794
function getMongoOptions (options) {
68-
var dbOptions = {
69-
db: {
70-
native_parser: true
71-
},
72-
server: {
73-
socketOptions: {
74-
keepAlive: 1
75-
}
76-
},
77-
reconnectTries: options.reconnectTries || 1000,
78-
reconnectInterval: options.reconnectInterval || 30000,
79-
replset: {
80-
socketOptions: {
81-
keepAlive: 1
82-
}
83-
},
84-
user: options.dbUsername,
85-
pass: options.dbPassword
86-
}
95+
var dbOptions = standardOptions
96+
dbOptions.reconnectInterval = options.reconnectInterval || RECONNECT_TIMEOUT
97+
dbOptions.reconnectTries = options.reconnectTries || 0
98+
dbOptions.user = options.dbUsername
99+
dbOptions.pass = options.dbPassword
87100

88101
return dbOptions
89102
}
90103

91104
function getMongoOptionsSsl (options, sslOptions) {
92-
var dbOptions = {
93-
db: {
94-
native_parser: true
95-
},
96-
server: {
97-
socketOptions: {
98-
keepAlive: 1
99-
},
100-
ssl: sslOptions.ssl,
101-
authenticationDatabase: sslOptions.authDatabase,
102-
sslCA: sslOptions.sslCA
103-
},
104-
reconnectTries: options.reconnectTries || 1000,
105-
reconnectInterval: options.reconnectInterval || 30000,
106-
replset: {
107-
socketOptions: {
108-
keepAlive: 1
109-
}
110-
},
111-
user: options.dbUsername,
112-
pass: options.dbPassword
113-
}
105+
var dbOptions = getMongoOptions(options)
106+
dbOptions.ssl = sslOptions.ssl
107+
dbOptions.authenticationDatabase = sslOptions.authDatabase
114108

115109
return dbOptions
116110
}

0 commit comments

Comments
 (0)