Skip to content

Commit cadecf6

Browse files
committed
build: wait for mysql server start with node
1 parent a191631 commit cadecf6

File tree

3 files changed

+53
-1
lines changed

3 files changed

+53
-1
lines changed

.travis.yml

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -65,13 +65,14 @@ before_install:
6565

6666
# Setup environment
6767
- "export MYSQL_DATABASE=node_mysql"
68+
- "export MYSQL_HOST=localhost"
6869
- "export MYSQL_PORT=$(node tool/free-port.js)"
6970
- "export MYSQL_USER=root"
7071

7172
install:
7273
- "docker run -d --name mysql -e MYSQL_ALLOW_EMPTY_PASSWORD=yes -e MYSQL_DATABASE=$MYSQL_DATABASE -p $MYSQL_PORT:3306 $DOCKER_MYSQL_TYPE:$DOCKER_MYSQL_VERSION"
7374
- "npm install"
74-
- "docker run --link mysql:db -e CHECK_PORT=3306 -e CHECK_HOST=db giorgos/takis"
75+
- "node tool/wait-mysql.js $MYSQL_PORT $MYSQL_HOST"
7576

7677
before_script:
7778
- "docker --version"

appveyor.yml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ environment:
44
MYSQL_USER: root
55
MYSQL_PASSWORD: Password12!
66
MYSQL_PATH: C:\Program Files\MySQL\MySQL Server 5.7
7+
MYSQL_PORT: 3306
78

89
matrix:
910
- nodejs_version: "0.8"
@@ -41,6 +42,7 @@ build: off
4142

4243
before_test:
4344
- SET PATH=%MYSQL_PATH%\bin;%PATH%
45+
- node tool/wait-mysql.js %MYSQL_PORT% %MYSQL_HOST%
4446
- mysqladmin --host=%MYSQL_HOST% --user=%MYSQL_USER% --password=%MYSQL_PASSWORD% create %MYSQL_DATABASE%
4547

4648
test_script:

tool/wait-mysql.js

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
var Net = require('net');
2+
3+
var CHECK_INTERVAL_MS = 200;
4+
var CHECK_TIMEOUT = 120000;
5+
var TCP_TIMEOUT = 1000;
6+
7+
process.nextTick(run);
8+
9+
function check(host, port, callback) {
10+
var socket = Net.createConnection(port, host);
11+
var timer = setTimeout(function () {
12+
socket.destroy();
13+
callback(false);
14+
}, TCP_TIMEOUT);
15+
16+
socket.once('data', function () {
17+
clearTimeout(timer);
18+
socket.destroy();
19+
callback(true);
20+
});
21+
22+
socket.on('error', function () {
23+
clearTimeout(timer);
24+
callback(false);
25+
});
26+
}
27+
28+
function run() {
29+
var host = process.argv[3] || 'localhost';
30+
var port = Number(process.argv[2]);
31+
32+
function next() {
33+
check(host, port, function (connected) {
34+
if (connected) {
35+
console.log('connected to %s:%d', host, port);
36+
process.exit(0);
37+
} else {
38+
setTimeout(next, CHECK_INTERVAL_MS);
39+
}
40+
});
41+
}
42+
43+
setTimeout(function () {
44+
console.error('timeout waiting for %s:%d', host, port);
45+
process.exit(1);
46+
}, CHECK_TIMEOUT);
47+
48+
next();
49+
}

0 commit comments

Comments
 (0)