1- import { Injectable } from '@nestjs/common' ;
1+ import { Injectable , Logger } from '@nestjs/common' ;
22import { Cron , CronExpression } from '@nestjs/schedule' ;
33
44import { QueuesEnum } from '@impler/shared' ;
@@ -7,6 +7,7 @@ import { FailedWebhookRetryRequestsEntity, FailedWebhookRetryRequestsRepository
77
88@Injectable ( )
99export class FailedWebhookRetry {
10+ private readonly logger = new Logger ( FailedWebhookRetry . name ) ;
1011 constructor (
1112 private failedWebhookRetryRequestsRepository : FailedWebhookRetryRequestsRepository = new FailedWebhookRetryRequestsRepository ( ) ,
1213 private queueService : QueueService
@@ -18,20 +19,20 @@ export class FailedWebhookRetry {
1819 const memUsageStart = process . memoryUsage ( ) ;
1920 const cpuUsageStart = process . cpuUsage ( ) ;
2021
21- console . log ( '========================================' ) ;
22- console . log ( `[FAILED-WEBHOOK-RETRY] Cron Started at ${ startTime . toISOString ( ) } ` ) ;
23- console . log ( `[FAILED-WEBHOOK-RETRY] Memory Usage (Start): RSS=${ ( memUsageStart . rss / 1024 / 1024 ) . toFixed ( 2 ) } MB, Heap=${ ( memUsageStart . heapUsed / 1024 / 1024 ) . toFixed ( 2 ) } MB` ) ;
24- console . log ( '========================================' ) ;
22+ this . logger . log ( '========================================' ) ;
23+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Cron Started at ${ startTime . toISOString ( ) } ` ) ;
24+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Memory Usage (Start): RSS=${ ( memUsageStart . rss / 1024 / 1024 ) . toFixed ( 2 ) } MB, Heap=${ ( memUsageStart . heapUsed / 1024 / 1024 ) . toFixed ( 2 ) } MB` ) ;
25+ this . logger . log ( '========================================' ) ;
2526
2627 try {
2728 const failedWebhooks : FailedWebhookRetryRequestsEntity [ ] = await this . failedWebhookRetryRequestsRepository . find ( {
2829 nextRequestTime : { $lt : new Date ( ) } ,
2930 } ) ;
3031
31- console . log ( `[FAILED-WEBHOOK-RETRY] Found ${ failedWebhooks . length } failed webhooks to retry` ) ;
32+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Found ${ failedWebhooks . length } failed webhooks to retry` ) ;
3233
3334 if ( ! failedWebhooks . length ) {
34- console . log ( `[FAILED-WEBHOOK-RETRY] No webhooks to process, exiting` ) ;
35+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] No webhooks to process, exiting` ) ;
3536 return ;
3637 }
3738
@@ -47,49 +48,49 @@ export class FailedWebhookRetry {
4748 const memUsageEnd = process . memoryUsage ( ) ;
4849 const cpuUsageEnd = process . cpuUsage ( cpuUsageStart ) ;
4950
50- console . log ( '========================================' ) ;
51- console . log ( `[FAILED-WEBHOOK-RETRY] Cron Completed at ${ endTime . toISOString ( ) } ` ) ;
52- console . log ( `[FAILED-WEBHOOK-RETRY] Results - Successful: ${ successful } , Failed: ${ failed } , Total: ${ failedWebhooks . length } ` ) ;
53- console . log ( `[FAILED-WEBHOOK-RETRY] Processing Duration: ${ processDuration } ms` ) ;
54- console . log ( `[FAILED-WEBHOOK-RETRY] Total Duration: ${ duration } ms` ) ;
55- console . log ( `[FAILED-WEBHOOK-RETRY] Memory Usage (End): RSS=${ ( memUsageEnd . rss / 1024 / 1024 ) . toFixed ( 2 ) } MB, Heap=${ ( memUsageEnd . heapUsed / 1024 / 1024 ) . toFixed ( 2 ) } MB` ) ;
56- console . log ( `[FAILED-WEBHOOK-RETRY] Memory Delta: RSS=${ ( ( memUsageEnd . rss - memUsageStart . rss ) / 1024 / 1024 ) . toFixed ( 2 ) } MB, Heap=${ ( ( memUsageEnd . heapUsed - memUsageStart . heapUsed ) / 1024 / 1024 ) . toFixed ( 2 ) } MB` ) ;
57- console . log ( `[FAILED-WEBHOOK-RETRY] CPU Usage: User=${ ( cpuUsageEnd . user / 1000 ) . toFixed ( 2 ) } ms, System=${ ( cpuUsageEnd . system / 1000 ) . toFixed ( 2 ) } ms` ) ;
51+ this . logger . log ( '========================================' ) ;
52+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Cron Completed at ${ endTime . toISOString ( ) } ` ) ;
53+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Results - Successful: ${ successful } , Failed: ${ failed } , Total: ${ failedWebhooks . length } ` ) ;
54+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Processing Duration: ${ processDuration } ms` ) ;
55+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Total Duration: ${ duration } ms` ) ;
56+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Memory Usage (End): RSS=${ ( memUsageEnd . rss / 1024 / 1024 ) . toFixed ( 2 ) } MB, Heap=${ ( memUsageEnd . heapUsed / 1024 / 1024 ) . toFixed ( 2 ) } MB` ) ;
57+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Memory Delta: RSS=${ ( ( memUsageEnd . rss - memUsageStart . rss ) / 1024 / 1024 ) . toFixed ( 2 ) } MB, Heap=${ ( ( memUsageEnd . heapUsed - memUsageStart . heapUsed ) / 1024 / 1024 ) . toFixed ( 2 ) } MB` ) ;
58+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] CPU Usage: User=${ ( cpuUsageEnd . user / 1000 ) . toFixed ( 2 ) } ms, System=${ ( cpuUsageEnd . system / 1000 ) . toFixed ( 2 ) } ms` ) ;
5859
5960 if ( duration > 5000 ) {
60- console . warn ( `[FAILED-WEBHOOK-RETRY] ⚠️ WARNING: Cron execution took ${ duration } ms (>5s threshold)` ) ;
61+ this . logger . warn ( `[FAILED-WEBHOOK-RETRY] ⚠️ WARNING: Cron execution took ${ duration } ms (>5s threshold)` ) ;
6162 }
6263
6364 if ( failedWebhooks . length > 100 ) {
64- console . warn ( `[FAILED-WEBHOOK-RETRY] ⚠️ WARNING: Processing large batch of ${ failedWebhooks . length } webhooks` ) ;
65+ this . logger . warn ( `[FAILED-WEBHOOK-RETRY] ⚠️ WARNING: Processing large batch of ${ failedWebhooks . length } webhooks` ) ;
6566 }
6667
67- console . log ( '========================================' ) ;
68+ this . logger . log ( '========================================' ) ;
6869 } catch ( error ) {
6970 const endTime = new Date ( ) ;
7071 const duration = endTime . getTime ( ) - startTime . getTime ( ) ;
7172
72- console . error ( '========================================' ) ;
73- console . error ( `[FAILED-WEBHOOK-RETRY] ❌ ERROR at ${ endTime . toISOString ( ) } ` ) ;
74- console . error ( `[FAILED-WEBHOOK-RETRY] Duration before error: ${ duration } ms` ) ;
75- console . error ( '[FAILED-WEBHOOK-RETRY] Error details:' , error ) ;
76- console . error ( '========================================' ) ;
73+ this . logger . error ( '========================================' ) ;
74+ this . logger . error ( `[FAILED-WEBHOOK-RETRY] ❌ ERROR at ${ endTime . toISOString ( ) } ` ) ;
75+ this . logger . error ( `[FAILED-WEBHOOK-RETRY] Duration before error: ${ duration } ms` ) ;
76+ this . logger . error ( '[FAILED-WEBHOOK-RETRY] Error details:' , error ) ;
77+ this . logger . error ( '========================================' ) ;
7778 throw error ;
7879 }
7980 }
8081
8182 private async processWebhook ( webhook : FailedWebhookRetryRequestsEntity ) {
8283 const webhookStartTime = Date . now ( ) ;
8384 try {
84- console . log ( `[FAILED-WEBHOOK-RETRY] Processing webhook - ID: ${ webhook . _id } , Time: ${ new Date ( ) . toISOString ( ) } ` ) ;
85+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Processing webhook - ID: ${ webhook . _id } , Time: ${ new Date ( ) . toISOString ( ) } ` ) ;
8586
8687 this . queueService . publishToQueue ( QueuesEnum . SEND_FAILED_WEBHOOK_DATA , webhook . _id as string ) ;
8788
8889 const webhookDuration = Date . now ( ) - webhookStartTime ;
89- console . log ( `[FAILED-WEBHOOK-RETRY] Webhook queued - ID: ${ webhook . _id } , Duration: ${ webhookDuration } ms` ) ;
90+ this . logger . log ( `[FAILED-WEBHOOK-RETRY] Webhook queued - ID: ${ webhook . _id } , Duration: ${ webhookDuration } ms` ) ;
9091 } catch ( error ) {
9192 const webhookDuration = Date . now ( ) - webhookStartTime ;
92- console . error ( `[FAILED-WEBHOOK-RETRY] ❌ Error processing webhook - ID: ${ webhook . _id } , Duration: ${ webhookDuration } ms, Time: ${ new Date ( ) . toISOString ( ) } ` , error ) ;
93+ this . logger . error ( `[FAILED-WEBHOOK-RETRY] ❌ Error processing webhook - ID: ${ webhook . _id } , Duration: ${ webhookDuration } ms, Time: ${ new Date ( ) . toISOString ( ) } ` , error ) ;
9394 throw error ;
9495 }
9596 }
0 commit comments