-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathwaitForMongo.js
More file actions
35 lines (31 loc) · 802 Bytes
/
waitForMongo.js
File metadata and controls
35 lines (31 loc) · 802 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
// waitForMongo.js
import net from 'net';
const host = 'mongo';
const port = 27017;
const timeout = 1000;
function checkMongoConnection() {
return new Promise((resolve) => {
const socket = new net.Socket();
socket.setTimeout(timeout);
socket.once('connect', () => {
socket.destroy();
resolve(true);
});
socket.once('timeout', () => {
socket.destroy();
resolve(false);
});
socket.once('error', () => {
resolve(false);
});
socket.connect(port, host);
});
}
async function waitUntilAvailable() {
console.log('⏳ Aguardando MongoDB ficar pronto...');
while (!(await checkMongoConnection())) {
await new Promise(res => setTimeout(res, timeout));
}
console.log('✅ MongoDB está pronto!');
}
await waitUntilAvailable();