@@ -24,6 +24,8 @@ export class WAMonitoringService {
2424 private readonly repository : RepositoryBroker ,
2525 private readonly cache : RedisCache ,
2626 ) {
27+ this . logger . verbose ( 'instance created' ) ;
28+
2729 this . removeInstance ( ) ;
2830 this . noConnection ( ) ;
2931 this . delInstanceFiles ( ) ;
@@ -47,6 +49,10 @@ export class WAMonitoringService {
4749 public delInstanceTime ( instance : string ) {
4850 const time = this . configService . get < DelInstance > ( 'DEL_INSTANCE' ) ;
4951 if ( typeof time === 'number' && time > 0 ) {
52+ this . logger . verbose (
53+ `Instance "${ instance } " don't have connection, will be removed in ${ time } minutes` ,
54+ ) ;
55+
5056 setTimeout ( async ( ) => {
5157 if ( this . waInstances [ instance ] ?. connectionStatus ?. state !== 'open' ) {
5258 if ( this . waInstances [ instance ] ?. connectionStatus ?. state === 'connecting' ) {
@@ -66,6 +72,7 @@ export class WAMonitoringService {
6672 }
6773
6874 public async instanceInfo ( instanceName ?: string ) {
75+ this . logger . verbose ( 'get instance info' ) ;
6976 if ( instanceName && ! this . waInstances [ instanceName ] ) {
7077 throw new NotFoundException ( `Instance "${ instanceName } " not found` ) ;
7178 }
@@ -74,9 +81,14 @@ export class WAMonitoringService {
7481
7582 for await ( const [ key , value ] of Object . entries ( this . waInstances ) ) {
7683 if ( value ) {
84+ this . logger . verbose ( 'get instance info: ' + key ) ;
7785 if ( value . connectionStatus . state === 'open' ) {
86+ this . logger . verbose ( 'instance: ' + key + ' - connectionStatus: open' ) ;
7887 let apikey : string ;
7988 if ( this . configService . get < Auth > ( 'AUTHENTICATION' ) . EXPOSE_IN_FETCH_INSTANCES ) {
89+ this . logger . verbose (
90+ 'instance: ' + key + ' - hash exposed in fetch instances' ,
91+ ) ;
8092 const tokenStore = await this . repository . auth . find ( key ) ;
8193 apikey = tokenStore . apikey || 'Apikey not found' ;
8294
@@ -91,6 +103,9 @@ export class WAMonitoringService {
91103 } ,
92104 } ) ;
93105 } else {
106+ this . logger . verbose (
107+ 'instance: ' + key + ' - hash not exposed in fetch instances' ,
108+ ) ;
94109 instances . push ( {
95110 instance : {
96111 instanceName : key ,
@@ -102,8 +117,14 @@ export class WAMonitoringService {
102117 } ) ;
103118 }
104119 } else {
120+ this . logger . verbose (
121+ 'instance: ' + key + ' - connectionStatus: ' + value . connectionStatus . state ,
122+ ) ;
105123 let apikey : string ;
106124 if ( this . configService . get < Auth > ( 'AUTHENTICATION' ) . EXPOSE_IN_FETCH_INSTANCES ) {
125+ this . logger . verbose (
126+ 'instance: ' + key + ' - hash exposed in fetch instances' ,
127+ ) ;
107128 const tokenStore = await this . repository . auth . find ( key ) ;
108129 apikey = tokenStore . apikey || 'Apikey not found' ;
109130
@@ -115,6 +136,9 @@ export class WAMonitoringService {
115136 } ,
116137 } ) ;
117138 } else {
139+ this . logger . verbose (
140+ 'instance: ' + key + ' - hash not exposed in fetch instances' ,
141+ ) ;
118142 instances . push ( {
119143 instance : {
120144 instanceName : key ,
@@ -126,10 +150,13 @@ export class WAMonitoringService {
126150 }
127151 }
128152
153+ this . logger . verbose ( 'return instance info: ' + instances . length ) ;
154+
129155 return instances . find ( ( i ) => i . instance . instanceName === instanceName ) ?? instances ;
130156 }
131157
132158 private delInstanceFiles ( ) {
159+ this . logger . verbose ( 'cron to delete instance files started' ) ;
133160 setInterval ( async ( ) => {
134161 if ( this . db . ENABLED && this . db . SAVE_DATA . INSTANCE ) {
135162 const collections = await this . dbInstance . collections ( ) ;
@@ -141,6 +168,7 @@ export class WAMonitoringService {
141168 { _id : { $regex : / ^ s e s s i o n - .* / } } ,
142169 ] ,
143170 } ) ;
171+ this . logger . verbose ( 'instance files deleted: ' + name ) ;
144172 } ) ;
145173 } else if ( this . redis . ENABLED ) {
146174 } else {
@@ -158,14 +186,17 @@ export class WAMonitoringService {
158186 } ) ;
159187 }
160188 } ) ;
189+ this . logger . verbose ( 'instance files deleted: ' + dirent . name ) ;
161190 }
162191 }
163192 }
164193 } , 3600 * 1000 * 2 ) ;
165194 }
166195
167196 public async cleaningUp ( instanceName : string ) {
197+ this . logger . verbose ( 'cleaning up instance: ' + instanceName ) ;
168198 if ( this . db . ENABLED && this . db . SAVE_DATA . INSTANCE ) {
199+ this . logger . verbose ( 'cleaning up instance in database: ' + instanceName ) ;
169200 await this . repository . dbServer . connect ( ) ;
170201 const collections : any [ ] = await this . dbInstance . collections ( ) ;
171202 if ( collections . length > 0 ) {
@@ -175,14 +206,18 @@ export class WAMonitoringService {
175206 }
176207
177208 if ( this . redis . ENABLED ) {
209+ this . logger . verbose ( 'cleaning up instance in redis: ' + instanceName ) ;
178210 this . cache . reference = instanceName ;
179211 await this . cache . delAll ( ) ;
180212 return ;
181213 }
214+
215+ this . logger . verbose ( 'cleaning up instance in files: ' + instanceName ) ;
182216 rmSync ( join ( INSTANCE_DIR , instanceName ) , { recursive : true , force : true } ) ;
183217 }
184218
185219 public async loadInstance ( ) {
220+ this . logger . verbose ( 'load instances' ) ;
186221 const set = async ( name : string ) => {
187222 const instance = new WAStartupService (
188223 this . configService ,
@@ -191,38 +226,50 @@ export class WAMonitoringService {
191226 this . cache ,
192227 ) ;
193228 instance . instanceName = name ;
229+ this . logger . verbose ( 'instance loaded: ' + name ) ;
230+
194231 await instance . connectToWhatsapp ( ) ;
232+ this . logger . verbose ( 'connectToWhatsapp: ' + name ) ;
233+
195234 this . waInstances [ name ] = instance ;
196235 } ;
197236
198237 try {
199238 if ( this . redis . ENABLED ) {
239+ this . logger . verbose ( 'redis enabled' ) ;
200240 await this . cache . connect ( this . redis as Redis ) ;
201241 const keys = await this . cache . instanceKeys ( ) ;
202242 if ( keys ?. length > 0 ) {
243+ this . logger . verbose ( 'reading instance keys and setting instances' ) ;
203244 keys . forEach ( async ( k ) => await set ( k . split ( ':' ) [ 1 ] ) ) ;
204245 } else {
246+ this . logger . verbose ( 'no instance keys found' ) ;
205247 initInstance ( ) ;
206248 }
207249 return ;
208250 }
209251
210252 if ( this . db . ENABLED && this . db . SAVE_DATA . INSTANCE ) {
253+ this . logger . verbose ( 'database enabled' ) ;
211254 await this . repository . dbServer . connect ( ) ;
212255 const collections : any [ ] = await this . dbInstance . collections ( ) ;
213256 if ( collections . length > 0 ) {
257+ this . logger . verbose ( 'reading collections and setting instances' ) ;
214258 collections . forEach (
215259 async ( coll ) => await set ( coll . namespace . replace ( / ^ [ \w - ] + \. / , '' ) ) ,
216260 ) ;
217261 } else {
262+ this . logger . verbose ( 'no collections found' ) ;
218263 initInstance ( ) ;
219264 }
220265 return ;
221266 }
222267
268+ this . logger . verbose ( 'store in files enabled' ) ;
223269 const dir = opendirSync ( INSTANCE_DIR , { encoding : 'utf-8' } ) ;
224270 for await ( const dirent of dir ) {
225271 if ( dirent . isDirectory ( ) ) {
272+ this . logger . verbose ( 'reading instance files and setting instances' ) ;
226273 const files = readdirSync ( join ( INSTANCE_DIR , dirent . name ) , {
227274 encoding : 'utf-8' ,
228275 } ) ;
@@ -233,6 +280,7 @@ export class WAMonitoringService {
233280
234281 await set ( dirent . name ) ;
235282 } else {
283+ this . logger . verbose ( 'no instance files found' ) ;
236284 initInstance ( ) ;
237285 }
238286 }
@@ -243,18 +291,23 @@ export class WAMonitoringService {
243291
244292 private removeInstance ( ) {
245293 this . eventEmitter . on ( 'remove.instance' , async ( instanceName : string ) => {
294+ this . logger . verbose ( 'remove instance: ' + instanceName ) ;
246295 try {
296+ this . logger . verbose ( 'instance: ' + instanceName + ' - removing from memory' ) ;
247297 this . waInstances [ instanceName ] = undefined ;
248298 } catch { }
249299
250300 try {
301+ this . logger . verbose ( 'request cleaning up instance: ' + instanceName ) ;
251302 this . cleaningUp ( instanceName ) ;
252303 } finally {
253304 this . logger . warn ( `Instance "${ instanceName } " - REMOVED` ) ;
254305 }
255306 } ) ;
256307 this . eventEmitter . on ( 'logout.instance' , async ( instanceName : string ) => {
308+ this . logger . verbose ( 'logout instance: ' + instanceName ) ;
257309 try {
310+ this . logger . verbose ( 'request cleaning up instance: ' + instanceName ) ;
258311 this . cleaningUp ( instanceName ) ;
259312 } finally {
260313 this . logger . warn ( `Instance "${ instanceName } " - LOGOUT` ) ;
@@ -263,9 +316,13 @@ export class WAMonitoringService {
263316 }
264317
265318 private noConnection ( ) {
319+ this . logger . verbose ( 'checking instances without connection' ) ;
266320 this . eventEmitter . on ( 'no.connection' , async ( instanceName ) => {
267321 try {
322+ this . logger . verbose ( 'instance: ' + instanceName + ' - removing from memory' ) ;
268323 this . waInstances [ instanceName ] = undefined ;
324+
325+ this . logger . verbose ( 'request cleaning up instance: ' + instanceName ) ;
269326 this . cleaningUp ( instanceName ) ;
270327 } catch ( error ) {
271328 this . logger . error ( {
0 commit comments