Skip to content

Commit 5ad6477

Browse files
authored
[OGUI-1544] Check db connection on server start (#2584)
adds initial check for connection on DB to enable service in the GUI for users;
1 parent 8cc14ae commit 5ad6477

File tree

2 files changed

+17
-8
lines changed

2 files changed

+17
-8
lines changed

InfoLogger/lib/api.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -39,6 +39,7 @@ module.exports.attachTo = async (http, ws) => {
3939

4040
if (config.mysql) {
4141
queryService = new QueryService(config.mysql);
42+
queryService.checkConnection(1, false);
4243
}
4344
const queryController = new QueryController(queryService);
4445

InfoLogger/lib/services/QueryService.js

Lines changed: 16 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -22,13 +22,15 @@ class QueryService {
2222
* @param {object} configMySql - mysql config
2323
*/
2424
constructor(configMySql = {}) {
25-
configMySql._user = configMySql?.user ?? 'gui';
26-
configMySql._password = configMySql?.password ?? '';
27-
configMySql._host = configMySql?.host ?? 'localhost';
28-
configMySql._port = configMySql?.port ?? 3306;
29-
configMySql._database = configMySql?.database ?? 'info_logger';
30-
configMySql._connectionLimit = configMySql?.connectionLimit ?? 25;
25+
configMySql.user = configMySql?.user ?? 'gui';
26+
configMySql.password = configMySql?.password ?? '';
27+
configMySql.host = configMySql?.host ?? 'localhost';
28+
configMySql.port = configMySql?.port ?? 3306;
29+
configMySql.database = configMySql?.database ?? 'info_logger';
30+
configMySql.connectionLimit = configMySql?.connectionLimit ?? 25;
3131
this._timeout = configMySql?.timeout ?? 10000;
32+
this._host = configMySql.host;
33+
this._port = configMySql.port;
3234

3335
this._pool = mariadb.createPool(configMySql);
3436
this._isAvailable = false;
@@ -38,18 +40,24 @@ class QueryService {
3840
/**
3941
* Method to test connection of mysql connector once initialized
4042
* @param {number} timeout - timeout for the connection test
43+
* @param {boolean} shouldThrow - whether an error should be thrown on failure
4144
* @returns {Promise} - a promise that resolves if connection is successful
4245
*/
43-
async checkConnection(timeout = this._timeout) {
46+
async checkConnection(timeout = this._timeout, shouldThrow = true) {
4447
try {
4548
await this._pool.query({
4649
sql: 'SELECT 1',
4750
timeout,
4851
});
4952
this._isAvailable = true;
53+
this._logger.infoMessage(`Connection to DB successfully established: ${this._host}:${this._port}`);
5054
} catch (error) {
5155
this._isAvailable = false;
52-
fromSqlToNativeError(error);
56+
if (shouldThrow) {
57+
fromSqlToNativeError(error);
58+
} else {
59+
this._logger.errorMessage(error);
60+
}
5361
}
5462
}
5563

0 commit comments

Comments
 (0)