From 33276805203c972805da4b7b264a845162ee77cd Mon Sep 17 00:00:00 2001 From: Daneryl Date: Mon, 12 Aug 2024 16:03:29 +0200 Subject: [PATCH] tenants tearDown, closes mongo model change stream --- app/api/tenants/tenantContext.ts | 7 +++++++ app/api/tenants/tenantsModel.ts | 14 ++++++++++---- .../generateAutomaticTranslationConfig.ts | 4 ++-- 3 files changed, 19 insertions(+), 6 deletions(-) diff --git a/app/api/tenants/tenantContext.ts b/app/api/tenants/tenantContext.ts index 1b18384bf27..60dce472b34 100644 --- a/app/api/tenants/tenantContext.ts +++ b/app/api/tenants/tenantContext.ts @@ -23,6 +23,8 @@ class Tenants { defaultTenant: Tenant; + model?: TenantsModel; + constructor(defaultTenant: Tenant) { this.defaultTenant = defaultTenant; this.tenants = { @@ -32,12 +34,17 @@ class Tenants { async setupTenants() { const model = await tenantsModel(); + this.model = model; model.on('change', () => { this.updateTenants(model).catch(handleError); }); await this.updateTenants(model); } + async tearDownTenants() { + await this.model?.closeChangeStream(); + } + async updateTenants(model: TenantsModel) { const tenants = await model.get(); diff --git a/app/api/tenants/tenantsModel.ts b/app/api/tenants/tenantsModel.ts index 373c4663cca..0a791c9717f 100644 --- a/app/api/tenants/tenantsModel.ts +++ b/app/api/tenants/tenantsModel.ts @@ -3,7 +3,7 @@ import mongoose, { Model, Document } from 'mongoose'; import { config } from 'api/config'; import { DB } from 'api/odm/DB'; import { handleError } from 'api/utils'; -import { MongoError } from 'mongodb'; +import { ChangeStream, MongoError } from 'mongodb'; import { Tenant } from './tenantContext'; @@ -45,6 +45,8 @@ class TenantsModel extends EventEmitter { collectionName: string; + changeStream?: ChangeStream; + constructor() { super(); this.collectionName = 'tenants'; @@ -54,12 +56,12 @@ class TenantsModel extends EventEmitter { private initializeModel() { this.model = this.tenantsDB.model(this.collectionName, mongoSchema); - const changeStream = this.model.watch(); - changeStream.on('change', () => { + this.changeStream = this.model.watch(); + this.changeStream.on('change', () => { this.change().catch(handleError); }); - changeStream.on('error', (error: MongoError) => { + this.changeStream.on('error', (error: MongoError) => { //The $changeStream stage is only supported on replica sets if (error.code === 40573) { // mongo documentation and ts types says changeStream.close returns a promise @@ -90,6 +92,10 @@ class TenantsModel extends EventEmitter { this.initializeModel(); } + async closeChangeStream() { + await this.changeStream?.close(); + } + async change() { const tenants = await this.get(); this.emit('change', tenants); diff --git a/scripts/scripts.v2/generateAutomaticTranslationConfig.ts b/scripts/scripts.v2/generateAutomaticTranslationConfig.ts index d837e1cbf75..4314fd4bbec 100644 --- a/scripts/scripts.v2/generateAutomaticTranslationConfig.ts +++ b/scripts/scripts.v2/generateAutomaticTranslationConfig.ts @@ -35,6 +35,6 @@ const semanticConfig = require(config); await tenants.run(async () => { await AutomaticTranslationFactory.defaultGenerateATConfig().execute(semanticConfig); }, tenant); - process.exit(); - // await DB.disconnect(); + await tenants.tearDownTenants(); + await DB.disconnect(); })();