@hvedinich/db 1.0.0 → 1.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/package.json +1 -1
- package/src/index.js +18 -17
package/package.json
CHANGED
package/src/index.js
CHANGED
|
@@ -3,8 +3,10 @@ const migration = require('./migration');
|
|
|
3
3
|
|
|
4
4
|
const init = ({ config, setStatus }) => {
|
|
5
5
|
const db = mongoose.connection;
|
|
6
|
+
|
|
7
|
+
const maxCount = 30;
|
|
6
8
|
let retry = 0;
|
|
7
|
-
|
|
9
|
+
let timerId;
|
|
8
10
|
|
|
9
11
|
const state = { connected: false };
|
|
10
12
|
const opt = {
|
|
@@ -20,23 +22,17 @@ const init = ({ config, setStatus }) => {
|
|
|
20
22
|
});
|
|
21
23
|
|
|
22
24
|
db.on('disconnected', async () => {
|
|
23
|
-
console.log(`Can not connect to mongo retry: ${retry}`);
|
|
24
25
|
state.connected = false;
|
|
25
|
-
|
|
26
|
-
retry
|
|
27
|
-
|
|
28
|
-
|
|
29
|
-
|
|
30
|
-
|
|
31
|
-
|
|
32
|
-
|
|
33
|
-
}
|
|
34
|
-
console.log(`retry error: ${err?.message || JSON.stringify(err)}`);
|
|
26
|
+
timerId = setInterval(() => {
|
|
27
|
+
retry += 1;
|
|
28
|
+
console.log(`Can not connect to mongo retry: ${retry}`);
|
|
29
|
+
if (retry >= maxCount) {
|
|
30
|
+
clearInterval(timerId);
|
|
31
|
+
timerId = undefined;
|
|
32
|
+
retry = 0;
|
|
33
|
+
setStatus(false);
|
|
35
34
|
}
|
|
36
|
-
}
|
|
37
|
-
if (retry >= maxCount) {
|
|
38
|
-
setStatus(false);
|
|
39
|
-
}
|
|
35
|
+
}, 1000);
|
|
40
36
|
});
|
|
41
37
|
|
|
42
38
|
db.on('error', () => {
|
|
@@ -44,8 +40,13 @@ const init = ({ config, setStatus }) => {
|
|
|
44
40
|
mongoose.disconnect();
|
|
45
41
|
});
|
|
46
42
|
db.on('open', () => {
|
|
47
|
-
console.log(
|
|
43
|
+
console.log('Mongo is connected');
|
|
48
44
|
state.connected = true;
|
|
45
|
+
if (timerId) {
|
|
46
|
+
clearInterval(timerId);
|
|
47
|
+
timerId = undefined;
|
|
48
|
+
}
|
|
49
|
+
|
|
49
50
|
setStatus(true);
|
|
50
51
|
retry = 0;
|
|
51
52
|
});
|