applay-utils 1.8.33 → 1.9.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/utils/mdb.js +52 -31
package/package.json
CHANGED
package/utils/mdb.js
CHANGED
|
@@ -7,47 +7,68 @@ var MDB = {
|
|
|
7
7
|
clients: {}
|
|
8
8
|
}
|
|
9
9
|
|
|
10
|
+
var shutdownHandlersRegistered = false;
|
|
11
|
+
|
|
12
|
+
// Fecha todos os pools de conexão conhecidos (um por URL distinta), não só o
|
|
13
|
+
// da conexão que originou o evento. Antes disso, cada connect() registrava
|
|
14
|
+
// seu próprio listener de SIGTERM/SIGINT/uncaughtException no `process`
|
|
15
|
+
// (um a mais por URL), então uma única uncaughtException não relacionada a
|
|
16
|
+
// Mongo derrubava todos os pools simultaneamente e o processo inteiro, além
|
|
17
|
+
// de acumular listeners duplicados (MaxListenersExceededWarning) a cada
|
|
18
|
+
// nova URL conectada.
|
|
19
|
+
var closeAllPools = (finish) => {
|
|
20
|
+
for (var url of Object.keys(MDB.clients)) {
|
|
21
|
+
if (!MDB.status[url]) { continue; }
|
|
22
|
+
MDB.status[url] = false;
|
|
23
|
+
try {
|
|
24
|
+
MDB.clients[url].close();
|
|
25
|
+
} catch (e) {
|
|
26
|
+
console.log('Erro ao fechar conexão Mongo', url, e);
|
|
27
|
+
}
|
|
28
|
+
console.log('Mongodb', url, 'OFF');
|
|
29
|
+
}
|
|
30
|
+
|
|
31
|
+
if (!finish) { return; }
|
|
32
|
+
|
|
33
|
+
if (MDB.stopCustom) {
|
|
34
|
+
MDB.stop('all', () => {
|
|
35
|
+
setTimeout(() => { process.exit(); }, 100);
|
|
36
|
+
});
|
|
37
|
+
} else {
|
|
38
|
+
setTimeout(() => { process.exit(); }, 100);
|
|
39
|
+
}
|
|
40
|
+
}
|
|
41
|
+
|
|
42
|
+
var registerShutdownHandlersOnce = () => {
|
|
43
|
+
if (shutdownHandlersRegistered) { return; }
|
|
44
|
+
shutdownHandlersRegistered = true;
|
|
45
|
+
process.on('SIGTERM', () => closeAllPools(true));
|
|
46
|
+
process.on('SIGINT', () => closeAllPools(true));
|
|
47
|
+
process.on('uncaughtException', (err) => {
|
|
48
|
+
console.log('ERRO MDB -- uncaughtException -- TRACKING -- ', err);
|
|
49
|
+
closeAllPools(true);
|
|
50
|
+
});
|
|
51
|
+
}
|
|
52
|
+
|
|
10
53
|
var connect = (alias, url, callback) => {
|
|
11
54
|
if (MDB.status[url]) { return callback(MDB.clients[url]); }
|
|
12
|
-
console.log('===============>>>>>>> NOVO POOL DE CONEXÃO MDB ',url,alias)
|
|
13
|
-
|
|
14
|
-
|
|
55
|
+
console.log('===============>>>>>>> NOVO POOL DE CONEXÃO MDB ', url, alias);
|
|
56
|
+
|
|
57
|
+
const separator = url.includes('?') ? '&' : '/?';
|
|
58
|
+
const uriparams = `${separator}appName=${alias}&minPoolSize=2&maxPoolSize=10&maxIdleTimeMS=30000&serverSelectionTimeoutMS=5000`;
|
|
59
|
+
|
|
60
|
+
registerShutdownHandlersOnce();
|
|
61
|
+
|
|
62
|
+
MongoBase.MongoClient.connect(url + uriparams, (err, client) => {
|
|
15
63
|
if (err) { return console.log('ERRO MDB -- TRACKING -- ', err); }
|
|
16
64
|
MDB.status[url] = true;
|
|
17
65
|
MDB.clients[url] = client;
|
|
18
|
-
|
|
19
|
-
process.on('SIGINT', internalStop(url, true, 'SIGINT'));
|
|
20
|
-
process.on('uncaughtException', internalStop(url, true, 'uncaughtException'));
|
|
21
|
-
// console.log('Conectado com ', alias);
|
|
22
|
-
callback(client)
|
|
66
|
+
callback(client);
|
|
23
67
|
});
|
|
24
68
|
}
|
|
25
69
|
|
|
26
70
|
var connectAsync = (alias, url) => new Promise(resolve => connect(alias, url, (client) => resolve(client)));
|
|
27
71
|
|
|
28
|
-
var internalStop = (alias, finish) => {
|
|
29
|
-
return (code) => {
|
|
30
|
-
// console.log('Desconnecting...');
|
|
31
|
-
if (!MDB.status[alias]) { console.log('Mongodb OFF'); return }
|
|
32
|
-
MDB.status[alias] = false;
|
|
33
|
-
MDB.clients[alias].close();
|
|
34
|
-
if (finish && MDB.stopCustom) {
|
|
35
|
-
MDB.stop(alias, () => {
|
|
36
|
-
console.log('Mongodb', alias, 'OFF');
|
|
37
|
-
setTimeout(() => { process.exit(); },100)
|
|
38
|
-
});
|
|
39
|
-
}
|
|
40
|
-
if (finish && !MDB.stopCustom) {
|
|
41
|
-
console.log('Mongodb', alias, 'OFF');
|
|
42
|
-
setTimeout(() => { process.exit(); }, 100)
|
|
43
|
-
}
|
|
44
|
-
if (!finish) {
|
|
45
|
-
console.log('Mongodb', alias, 'OFF');
|
|
46
|
-
}
|
|
47
|
-
}
|
|
48
|
-
|
|
49
|
-
}
|
|
50
|
-
|
|
51
72
|
|
|
52
73
|
var stopCustom = (customCallback) => {
|
|
53
74
|
MDB.stopCustom = true;
|