From a7ba3340177218b7122616bb39b2a032b1536625 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uro=C5=A1=20Marolt?= Date: Tue, 3 Dec 2024 10:03:29 +0100 Subject: [PATCH 1/2] small fix --- backend/src/database/models/index.ts | 2 +- services/libs/database/src/connection.ts | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/backend/src/database/models/index.ts b/backend/src/database/models/index.ts index 0bf7b78e4a..20e18733ba 100644 --- a/backend/src/database/models/index.ts +++ b/backend/src/database/models/index.ts @@ -72,7 +72,7 @@ async function models( { dialect: DB_CONFIG.dialect, dialectOptions: { - application_name: SERVICE, + application_name: SERVICE ? `${SERVICE}-seq` : 'unknown-app-seq', connectionTimeoutMillis: 15000, query_timeout: queryTimeoutMilliseconds, idle_in_transaction_session_timeout: 20000, diff --git a/services/libs/database/src/connection.ts b/services/libs/database/src/connection.ts index 0e5cf7d23c..5bea53591a 100644 --- a/services/libs/database/src/connection.ts +++ b/services/libs/database/src/connection.ts @@ -86,7 +86,7 @@ export const getDbConnection = async ( max: maxPoolSize || (IS_DEV_ENV ? 5 : 20), idleTimeoutMillis: idleTimeoutMillis !== undefined ? idleTimeoutMillis : 10000, // query_timeout: 30000, - application_name: process.env.SERVICE || 'unknown-app', + application_name: process.env.SERVICE ? `${process.env.SERVICE}-pg` : 'unknown-app=pg', }) return dbConnection[cacheKey] From 3247a842e4128e512fe03a9995f9afca6cff7f7a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Uro=C5=A1=20Marolt?= Date: Wed, 4 Dec 2024 09:45:25 +0100 Subject: [PATCH 2/2] fix --- .../src/bin/jobs/refreshMaterializedViews.ts | 12 +++++-- backend/src/database/databaseConnection.ts | 9 +++-- backend/src/database/models/index.ts | 34 ++++++++----------- backend/src/middlewares/databaseMiddleware.ts | 5 ++- pnpm-lock.yaml | 9 ----- 5 files changed, 32 insertions(+), 37 deletions(-) diff --git a/backend/src/bin/jobs/refreshMaterializedViews.ts b/backend/src/bin/jobs/refreshMaterializedViews.ts index 5553dbd136..cfa24cf401 100644 --- a/backend/src/bin/jobs/refreshMaterializedViews.ts +++ b/backend/src/bin/jobs/refreshMaterializedViews.ts @@ -2,7 +2,7 @@ import { QueryTypes } from 'sequelize' import { Logger, getChildLogger, getServiceChildLogger, logExecutionTimeV2 } from '@crowd/logging' -import { databaseInit } from '@/database/databaseConnection' +import { databaseClose, databaseInit } from '@/database/databaseConnection' import { CrowdJob } from '../../types/jobTypes' @@ -63,8 +63,14 @@ const job: CrowdJob = { onTrigger: async () => { const database = await databaseInit(1000 * 60 * 15, true) const log = getServiceChildLogger('RefreshMVJob') - await refreshMaterializedView('member_segments_mv', database, log) - await refreshMaterializedView('organization_segments_mv', database, log) + try { + await refreshMaterializedView('member_segments_mv', database, log) + await refreshMaterializedView('organization_segments_mv', database, log) + } catch (err) { + log.error(err, 'Error while refreshing materialized views!') + } finally { + await databaseClose(database) + } }, } diff --git a/backend/src/database/databaseConnection.ts b/backend/src/database/databaseConnection.ts index c498d6f7bb..f4094cea2d 100644 --- a/backend/src/database/databaseConnection.ts +++ b/backend/src/database/databaseConnection.ts @@ -9,10 +9,9 @@ export async function databaseInit( queryTimeoutMilliseconds: number = 60000, forceNewInstance: boolean = false, databaseHostnameOverride: string = null, - profileQueries: boolean = false, ) { - if (profileQueries || forceNewInstance) { - return models(queryTimeoutMilliseconds, databaseHostnameOverride, profileQueries) + if (forceNewInstance) { + return models(queryTimeoutMilliseconds, databaseHostnameOverride) } if (!cached) { @@ -21,3 +20,7 @@ export async function databaseInit( return cached } + +export async function databaseClose(database) { + await database.sequelize.close() +} diff --git a/backend/src/database/models/index.ts b/backend/src/database/models/index.ts index 20e18733ba..9f67e637bb 100644 --- a/backend/src/database/models/index.ts +++ b/backend/src/database/models/index.ts @@ -7,7 +7,7 @@ import { IS_CLOUD_ENV } from '@crowd/common' * This module creates the Sequelize to the database and * exports all the models. */ -import { getServiceChildLogger, logExecutionTimeV2 } from '@crowd/logging' +import { getServiceChildLogger } from '@crowd/logging' import { DB_CONFIG, SERVICE } from '../../conf' import * as configTypes from '../../conf/configTypes' @@ -47,11 +47,7 @@ function getCredentials(): Credentials { } } -async function models( - queryTimeoutMilliseconds: number, - databaseHostnameOverride = null, - profileQueries = false, -) { +async function models(queryTimeoutMilliseconds: number, databaseHostnameOverride = null) { log.info('Initializing sequelize database connection!') const database = {} as any @@ -106,19 +102,19 @@ async function models( }, ) - if (profileQueries) { - const oldQuery = sequelize.query - sequelize.query = async (query, options) => { - const { replacements } = options || {} - const result = await logExecutionTimeV2( - () => oldQuery.apply(sequelize, [query, options]), - log, - `DB Query:\n${query}\n${replacements ? `Params: ${JSON.stringify(replacements)}` : ''}`, - ) - - return result - } - } + // if (profileQueries) { + // const oldQuery = sequelize.query + // sequelize.query = async (query, options) => { + // const { replacements } = options || {} + // const result = await logExecutionTimeV2( + // () => oldQuery.apply(sequelize, [query, options]), + // log, + // `DB Query:\n${query}\n${replacements ? `Params: ${JSON.stringify(replacements)}` : ''}`, + // ) + + // return result + // } + // } const modelClasses = [ require('./auditLog').default, diff --git a/backend/src/middlewares/databaseMiddleware.ts b/backend/src/middlewares/databaseMiddleware.ts index 0ce8468010..fcff9d6868 100644 --- a/backend/src/middlewares/databaseMiddleware.ts +++ b/backend/src/middlewares/databaseMiddleware.ts @@ -8,11 +8,10 @@ const log = getServiceLogger() let qdb export async function databaseMiddleware(req, res, next) { try { - const profileQueries = !!req.profileSql - const database = await databaseInit(undefined, undefined, undefined, profileQueries) + const database = await databaseInit() req.database = database if (!qdb) { - qdb = await getClientSQL(profileQueries) + qdb = await getClientSQL() } req.qdb = qdb } catch (error) { diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml index 9ec9bdef96..15221284d2 100644 --- a/pnpm-lock.yaml +++ b/pnpm-lock.yaml @@ -1881,15 +1881,6 @@ importers: specifier: ^5.6.3 version: 5.6.3 - services/libs/ioc: - devDependencies: - '@types/node': - specifier: ^18.16.3 - version: 18.19.31 - typescript: - specifier: ^5.6.3 - version: 5.6.3 - services/libs/logging: dependencies: '@crowd/common':