diff --git a/backend/src/apis/gms/ExportUsers.ts b/backend/src/apis/gms/ExportUsers.ts index b5b79b479..981f9c90e 100644 --- a/backend/src/apis/gms/ExportUsers.ts +++ b/backend/src/apis/gms/ExportUsers.ts @@ -66,7 +66,7 @@ async function main() { } logger.info('##gms## publishing all local users successful...') - await con.close() + await con.destroy() } main().catch((e) => { diff --git a/backend/src/apis/humhub/ExportUsers.ts b/backend/src/apis/humhub/ExportUsers.ts index 908e3f424..dc1f4fb5d 100644 --- a/backend/src/apis/humhub/ExportUsers.ts +++ b/backend/src/apis/humhub/ExportUsers.ts @@ -101,7 +101,7 @@ async function main() { } while (userCount === USER_BULK_SIZE) process.stdout.write('\n') - await con.close() + await con.destroy() const elapsed = new Date().getTime() - start logger.info('export user to humhub, statistics:', { timeSeconds: elapsed / 1000.0, diff --git a/backend/src/util/executeKlicktipp.ts b/backend/src/util/executeKlicktipp.ts index 41e3624b5..eec525bac 100644 --- a/backend/src/util/executeKlicktipp.ts +++ b/backend/src/util/executeKlicktipp.ts @@ -3,10 +3,11 @@ import { AppDatabase } from 'database' import { exportEventDataToKlickTipp } from './klicktipp' async function executeKlicktipp(): Promise { - const connection = await AppDatabase.getInstance() + const connection = AppDatabase.getInstance() + await connection.init() if (connection.isConnected()) { await exportEventDataToKlickTipp() - await connection.close() + await connection.destroy() return true } else { return false diff --git a/database/src/AppDatabase.ts b/database/src/AppDatabase.ts index d06a02230..529321649 100644 --- a/database/src/AppDatabase.ts +++ b/database/src/AppDatabase.ts @@ -3,11 +3,14 @@ import { Migration, entities } from './entity' import { latestDbVersion } from '.' import { CONFIG } from './config' -import { logger } from './logging' +import { getLogger } from 'log4js' +import { LOG4JS_BASE_CATEGORY_NAME } from './config/const' + +const logger = getLogger(`${LOG4JS_BASE_CATEGORY_NAME}.AppDatabase`) export class AppDatabase { private static instance: AppDatabase - private connection: DBDataSource | undefined + private dataSource: DBDataSource | undefined /** * The Singleton's constructor should always be private to prevent direct @@ -29,48 +32,49 @@ export class AppDatabase { } public isConnected(): boolean { - return this.connection?.isInitialized ?? false + return this.dataSource?.isInitialized ?? false } public getDataSource(): DBDataSource { - if (!this.connection) { + if (!this.dataSource) { throw new Error('Connection not initialized') } - return this.connection + return this.dataSource } // create database connection, initialize with automatic retry and check for correct database version public async init(): Promise { - if (this.connection?.isInitialized) { + if (this.dataSource?.isInitialized) { return } - - this.connection = new DBDataSource({ - type: 'mysql', - legacySpatialSupport: false, - host: CONFIG.DB_HOST, - port: CONFIG.DB_PORT, - username: CONFIG.DB_USER, - password: CONFIG.DB_PASSWORD, - database: CONFIG.DB_DATABASE, - entities, - synchronize: false, - logging: CONFIG.TYPEORM_LOGGING_ACTIVE, - logger: CONFIG.TYPEORM_LOGGING_ACTIVE - ? new FileLogger('all', { - // workaround to let previous path working, because with esbuild the script root path has changed - logPath: (CONFIG.PRODUCTION ? '../' : '') + CONFIG.TYPEORM_LOGGING_RELATIVE_PATH, - }) - : undefined, - extra: { - charset: 'utf8mb4_unicode_ci', - }, - }) + if (!this.dataSource) { + this.dataSource = new DBDataSource({ + type: 'mysql', + legacySpatialSupport: false, + host: CONFIG.DB_HOST, + port: CONFIG.DB_PORT, + username: CONFIG.DB_USER, + password: CONFIG.DB_PASSWORD, + database: CONFIG.DB_DATABASE, + entities, + synchronize: false, + logging: CONFIG.TYPEORM_LOGGING_ACTIVE, + logger: CONFIG.TYPEORM_LOGGING_ACTIVE + ? new FileLogger('all', { + // workaround to let previous path working, because with esbuild the script root path has changed + logPath: (CONFIG.PRODUCTION ? '../' : '') + CONFIG.TYPEORM_LOGGING_RELATIVE_PATH, + }) + : undefined, + extra: { + charset: 'utf8mb4_unicode_ci', + }, + }) + } // retry connection on failure some times to allow database to catch up for (let attempt = 1; attempt <= CONFIG.DB_CONNECT_RETRY_COUNT; attempt++) { try { - await this.connection.initialize() - if (this.connection.isInitialized) { + await this.dataSource.initialize() + if (this.dataSource.isInitialized) { logger.info(`Database connection established on attempt ${attempt}`) break } @@ -79,15 +83,15 @@ export class AppDatabase { await new Promise((resolve) => setTimeout(resolve, CONFIG.DB_CONNECT_RETRY_DELAY_MS)) } } - if (!this.connection?.isInitialized) { + if (!this.dataSource?.isInitialized) { throw new Error('Could not connect to database') } // check for correct database version await this.checkDBVersion() } - public async close(): Promise { - await this.connection?.destroy() + public async destroy(): Promise { + await this.dataSource?.destroy() } // ###################################### // private methods