Skip to content

Commit

Permalink
tenants tearDown, closes mongo model change stream
Browse files Browse the repository at this point in the history
  • Loading branch information
daneryl committed Aug 12, 2024
1 parent 65b02a7 commit 3327680
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 6 deletions.
7 changes: 7 additions & 0 deletions app/api/tenants/tenantContext.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,8 @@ class Tenants {

defaultTenant: Tenant;

model?: TenantsModel;

constructor(defaultTenant: Tenant) {
this.defaultTenant = defaultTenant;
this.tenants = {
Expand All @@ -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();

Expand Down
14 changes: 10 additions & 4 deletions app/api/tenants/tenantsModel.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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';

Expand Down Expand Up @@ -45,6 +45,8 @@ class TenantsModel extends EventEmitter {

collectionName: string;

changeStream?: ChangeStream;

constructor() {
super();
this.collectionName = 'tenants';
Expand All @@ -54,12 +56,12 @@ class TenantsModel extends EventEmitter {
private initializeModel() {
this.model = this.tenantsDB.model<TenantDocument>(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
Expand Down Expand Up @@ -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);
Expand Down
4 changes: 2 additions & 2 deletions scripts/scripts.v2/generateAutomaticTranslationConfig.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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();
})();

0 comments on commit 3327680

Please sign in to comment.