@@ -74,7 +74,7 @@ export class WAMonitoringService {
7474 }
7575 }
7676
77- public async instanceInfo ( instanceName ?: string ) {
77+ public async instanceInfo ( instanceName ?: string ) {
7878 this . logger . verbose ( 'get instance info' ) ;
7979
8080 const urlServer = this . configService . get < HttpServer > ( 'SERVER' ) . URL ;
@@ -129,7 +129,7 @@ export class WAMonitoringService {
129129
130130 return instances ;
131131 }
132-
132+
133133 private delInstanceFiles ( ) {
134134 this . logger . verbose ( 'cron to delete instance files started' ) ;
135135 setInterval ( async ( ) => {
@@ -226,67 +226,85 @@ export class WAMonitoringService {
226226 }
227227
228228 public async loadInstance ( ) {
229- this . logger . verbose ( 'load instances' ) ;
230- const set = async ( name : string ) => {
231- const instance = new WAStartupService ( this . configService , this . eventEmitter , this . repository , this . cache ) ;
232- instance . instanceName = name ;
233- this . logger . verbose ( 'instance loaded: ' + name ) ;
234-
235- await instance . connectToWhatsapp ( ) ;
236- this . logger . verbose ( 'connectToWhatsapp: ' + name ) ;
237-
238- this . waInstances [ name ] = instance ;
239- } ;
229+ this . logger . verbose ( 'Loading instances' ) ;
240230
241231 try {
242232 if ( this . redis . ENABLED ) {
243- this . logger . verbose ( 'redis enabled' ) ;
244- await this . cache . connect ( this . redis as Redis ) ;
245- const keys = await this . cache . instanceKeys ( ) ;
246- if ( keys ?. length > 0 ) {
247- this . logger . verbose ( 'reading instance keys and setting instances' ) ;
248- keys . forEach ( async ( k ) => await set ( k . split ( ':' ) [ 1 ] ) ) ;
249- } else {
250- this . logger . verbose ( 'no instance keys found' ) ;
251- }
252- this . cache . disconnect ( ) ;
253- return ;
233+ await this . loadInstancesFromRedis ( ) ;
234+ } else if ( this . db . ENABLED && this . db . SAVE_DATA . INSTANCE ) {
235+ await this . loadInstancesFromDatabase ( ) ;
236+ } else {
237+ await this . loadInstancesFromFiles ( ) ;
254238 }
239+ } catch ( error ) {
240+ this . logger . error ( error ) ;
241+ }
242+ }
255243
256- if ( this . db . ENABLED && this . db . SAVE_DATA . INSTANCE ) {
257- this . logger . verbose ( 'database enabled' ) ;
258- await this . repository . dbServer . connect ( ) ;
259- const collections : any [ ] = await this . dbInstance . collections ( ) ;
260- if ( collections . length > 0 ) {
261- this . logger . verbose ( 'reading collections and setting instances' ) ;
262- collections . forEach ( async ( coll ) => await set ( coll . namespace . replace ( / ^ [ \w - ] + \. / , '' ) ) ) ;
263- } else {
264- this . logger . verbose ( 'no collections found' ) ;
265- }
266- return ;
244+ private async setInstance ( name : string ) {
245+ const instance = new WAStartupService ( this . configService , this . eventEmitter , this . repository , this . cache ) ;
246+ instance . instanceName = name ;
247+ this . logger . verbose ( 'Instance loaded: ' + name ) ;
248+
249+ await instance . connectToWhatsapp ( ) ;
250+ this . logger . verbose ( 'connectToWhatsapp: ' + name ) ;
251+
252+ this . waInstances [ name ] = instance ;
253+ }
254+
255+ private async loadInstancesFromRedis ( ) {
256+ this . logger . verbose ( 'Redis enabled' ) ;
257+ await this . cache . connect ( this . redis as Redis ) ;
258+ const keys = await this . cache . instanceKeys ( ) ;
259+
260+ if ( keys ?. length > 0 ) {
261+ this . logger . verbose ( 'Reading instance keys and setting instances' ) ;
262+ await Promise . all ( keys . map ( ( k ) => this . setInstance ( k . split ( ':' ) [ 1 ] ) ) ) ;
263+ } else {
264+ this . logger . verbose ( 'No instance keys found' ) ;
265+ }
266+
267+ this . cache . disconnect ( ) ;
268+ }
269+
270+ private async loadInstancesFromDatabase ( ) {
271+ this . logger . verbose ( 'Database enabled' ) ;
272+ await this . repository . dbServer . connect ( ) ;
273+ const collections : any [ ] = await this . dbInstance . collections ( ) ;
274+
275+ if ( collections . length > 0 ) {
276+ this . logger . verbose ( 'Reading collections and setting instances' ) ;
277+ await Promise . all ( collections . map ( ( coll ) => this . setInstance ( coll . namespace . replace ( / ^ [ \w - ] + \. / , '' ) ) ) ) ;
278+ } else {
279+ this . logger . verbose ( 'No collections found' ) ;
280+ }
281+ }
282+
283+ private async loadInstancesFromFiles ( ) {
284+ this . logger . verbose ( 'Store in files enabled' ) ;
285+ const dir = opendirSync ( INSTANCE_DIR , { encoding : 'utf-8' } ) ;
286+ const instanceDirs = [ ] ;
287+
288+ for await ( const dirent of dir ) {
289+ if ( dirent . isDirectory ( ) ) {
290+ instanceDirs . push ( dirent . name ) ;
291+ } else {
292+ this . logger . verbose ( 'No instance files found' ) ;
267293 }
294+ }
268295
269- this . logger . verbose ( 'store in files enabled' ) ;
270- const dir = opendirSync ( INSTANCE_DIR , { encoding : 'utf-8' } ) ;
271- for await ( const dirent of dir ) {
272- if ( dirent . isDirectory ( ) ) {
273- this . logger . verbose ( 'reading instance files and setting instances' ) ;
274- const files = readdirSync ( join ( INSTANCE_DIR , dirent . name ) , {
275- encoding : 'utf-8' ,
276- } ) ;
277- if ( files . length === 0 ) {
278- rmSync ( join ( INSTANCE_DIR , dirent . name ) , { recursive : true , force : true } ) ;
279- break ;
280- }
296+ await Promise . all (
297+ instanceDirs . map ( async ( instanceName ) => {
298+ this . logger . verbose ( 'Reading instance files and setting instances: ' + instanceName ) ;
299+ const files = readdirSync ( join ( INSTANCE_DIR , instanceName ) , { encoding : 'utf-8' } ) ;
281300
282- await set ( dirent . name ) ;
301+ if ( files . length === 0 ) {
302+ rmSync ( join ( INSTANCE_DIR , instanceName ) , { recursive : true , force : true } ) ;
283303 } else {
284- this . logger . verbose ( 'no instance files found' ) ;
304+ await this . setInstance ( instanceName ) ;
285305 }
286- }
287- } catch ( error ) {
288- this . logger . error ( error ) ;
289- }
306+ } ) ,
307+ ) ;
290308 }
291309
292310 private removeInstance ( ) {
0 commit comments