-
Notifications
You must be signed in to change notification settings - Fork 2.5k
/
Copy pathwait-mysql.js
49 lines (40 loc) · 1 KB
/
wait-mysql.js
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
var Net = require('net');
var CHECK_INTERVAL_MS = 200;
var CHECK_TIMEOUT = 120000;
var TCP_TIMEOUT = 1000;
process.nextTick(run);
function check(host, port, callback) {
var socket = Net.createConnection(port, host);
var timer = setTimeout(function () {
socket.destroy();
callback(false);
}, TCP_TIMEOUT);
socket.once('data', function () {
clearTimeout(timer);
socket.destroy();
callback(true);
});
socket.on('error', function () {
clearTimeout(timer);
callback(false);
});
}
function run() {
var host = process.argv[3] || 'localhost';
var port = Number(process.argv[2]);
function next() {
check(host, port, function (connected) {
if (connected) {
console.log('connected to %s:%d', host, port);
process.exit(0);
} else {
setTimeout(next, CHECK_INTERVAL_MS);
}
});
}
setTimeout(function () {
console.error('timeout waiting for %s:%d', host, port);
process.exit(1);
}, CHECK_TIMEOUT);
next();
}