gitpod/components/gitpod-db/src/wait-for-db.ts
2022-03-14 21:48:21 +05:30

48 lines
1.4 KiB
TypeScript

/**
* 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 = {
...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);