@@ -2,23 +2,20 @@ import { execSync } from 'child_process';
22import EventEmitter2 from 'eventemitter2' ;
33import { opendirSync , readdirSync , rmSync } from 'fs' ;
44import { Db } from 'mongodb' ;
5+ import { Collection } from 'mongoose' ;
56import { join } from 'path' ;
67
78import { Auth , ConfigService , Database , DelInstance , HttpServer , Redis } from '../../config/env.config' ;
89import { Logger } from '../../config/logger.config' ;
910import { INSTANCE_DIR , STORE_DIR } from '../../config/path.config' ;
1011import { NotFoundException } from '../../exceptions' ;
11- import { dbserver } from '../../libs/db.connect' ;
1212import { RedisCache } from '../../libs/redis.client' ;
1313import {
1414 AuthModel ,
1515 ChamaaiModel ,
16- // ChatModel,
1716 ChatwootModel ,
17+ ContactModel ,
1818 LabelModel ,
19- // ContactModel,
20- // MessageModel,
21- // MessageUpModel,
2219 ProxyModel ,
2320 RabbitmqModel ,
2421 SettingsModel ,
@@ -42,7 +39,6 @@ export class WAMonitoringService {
4239
4340 this . removeInstance ( ) ;
4441 this . noConnection ( ) ;
45- // this.delInstanceFiles();
4642
4743 Object . assign ( this . db , configService . get < Database > ( 'DATABASE' ) ) ;
4844 Object . assign ( this . redis , configService . get < Redis > ( 'REDIS' ) ) ;
@@ -57,8 +53,6 @@ export class WAMonitoringService {
5753
5854 private dbInstance : Db ;
5955
60- private dbStore = dbserver ;
61-
6256 private readonly logger = new Logger ( WAMonitoringService . name ) ;
6357 public readonly waInstances : Record < string , WAStartupService > = { } ;
6458
@@ -328,11 +322,6 @@ export class WAMonitoringService {
328322
329323 this . logger . verbose ( 'cleaning store database instance: ' + instanceName ) ;
330324
331- // await ChatModel.deleteMany({ owner: instanceName });
332- // await ContactModel.deleteMany({ owner: instanceName });
333- // await MessageUpModel.deleteMany({ owner: instanceName });
334- // await MessageModel.deleteMany({ owner: instanceName });
335-
336325 await AuthModel . deleteMany ( { _id : instanceName } ) ;
337326 await WebhookModel . deleteMany ( { _id : instanceName } ) ;
338327 await ChatwootModel . deleteMany ( { _id : instanceName } ) ;
@@ -343,6 +332,7 @@ export class WAMonitoringService {
343332 await WebsocketModel . deleteMany ( { _id : instanceName } ) ;
344333 await SettingsModel . deleteMany ( { _id : instanceName } ) ;
345334 await LabelModel . deleteMany ( { owner : instanceName } ) ;
335+ await ContactModel . deleteMany ( { owner : instanceName } ) ;
346336
347337 return ;
348338 }
@@ -396,7 +386,7 @@ export class WAMonitoringService {
396386 this . logger . verbose ( 'Database enabled' ) ;
397387 await this . repository . dbServer . connect ( ) ;
398388 const collections : any [ ] = await this . dbInstance . collections ( ) ;
399-
389+ await this . deleteTempInstances ( collections ) ;
400390 if ( collections . length > 0 ) {
401391 this . logger . verbose ( 'Reading collections and setting instances' ) ;
402392 await Promise . all ( collections . map ( ( coll ) => this . setInstance ( coll . namespace . replace ( / ^ [ \w - ] + \. / , '' ) ) ) ) ;
@@ -485,4 +475,22 @@ export class WAMonitoringService {
485475 }
486476 } ) ;
487477 }
478+
479+ private async deleteTempInstances ( collections : Collection < Document > [ ] ) {
480+ this . logger . verbose ( 'Cleaning up temp instances' ) ;
481+ const auths = await this . repository . auth . list ( ) ;
482+ if ( auths . length === 0 ) {
483+ this . logger . verbose ( 'No temp instances found' ) ;
484+ return ;
485+ }
486+ let tempInstances = 0 ;
487+ auths . forEach ( ( auth ) => {
488+ if ( collections . find ( ( coll ) => coll . namespace . replace ( / ^ [ \w - ] + \. / , '' ) === auth . _id ) ) {
489+ return ;
490+ }
491+ tempInstances ++ ;
492+ this . eventEmitter . emit ( 'remove.instance' , auth . _id , 'inner' ) ;
493+ } ) ;
494+ this . logger . verbose ( 'Temp instances removed: ' + tempInstances ) ;
495+ }
488496}
0 commit comments