-
Notifications
You must be signed in to change notification settings - Fork 1.3k
/
Copy pathwait-for-db.ts
47 lines (42 loc) · 1.38 KB
/
wait-for-db.ts
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
/**
* Copyright (c) 2020 Gitpod GmbH. All rights reserved.
* Licensed under the GNU Affero General Public License (AGPL).
* See License.AGPL.txt in the project root for license information.
*/
/**
* Script that waits for a database to become available
*/
import "reflect-metadata";
import { Config } from "./config";
import * as mysql from "mysql";
const retryPeriod = 5000; // [ms]
const totalAttempts = 30;
const connCfg: mysql.ConnectionConfig = {
...new Config().mysqlConfig,
timeout: retryPeriod,
};
function connectOrReschedule(attempt: number) {
const con = mysql.createConnection(connCfg);
try {
con.connect((err) => {
if (err) {
rescheduleConnectionAttempt(attempt, err);
} else {
console.log("DB is available");
con.destroy();
process.exit(0);
}
});
} catch (err) {
rescheduleConnectionAttempt(attempt, err);
}
}
function rescheduleConnectionAttempt(attempt: number, err: Error) {
if (attempt == totalAttempts) {
console.log(`Could not connect within ${totalAttempts} attempts. Stopping.`);
process.exit(1);
}
console.log(`Connection attempt ${attempt}/${totalAttempts} failed. Retrying in ${retryPeriod / 1000} seconds.`);
setTimeout(() => connectOrReschedule(attempt + 1), retryPeriod);
}
connectOrReschedule(0);