@@ -28,6 +28,27 @@ import { env } from "../../shared/utils/env";
2828const handler : Processor < string , void , string > = async ( job : Job < string > ) => {
2929 const { data, webhook } = superjson . parse < WebhookJob > ( job . data ) ;
3030
31+ // Extract transaction ID if available
32+ let transactionId : string | undefined ;
33+ if ( "queueId" in data ) {
34+ transactionId = data . queueId ;
35+ }
36+
37+ // Log webhook attempt with HMAC info
38+ const hmacMode = env . ENABLE_CUSTOM_HMAC_AUTH ? "custom" : "standard" ;
39+ logger ( {
40+ service : "worker" ,
41+ level : "info" ,
42+ message : `[Webhook] Attempting to send webhook for transaction ${ transactionId } at destination ${ webhook . url } ` ,
43+ queueId : transactionId ,
44+ data : {
45+ eventType : data . type ,
46+ destination : webhook . url ,
47+ webhookId : webhook . id ,
48+ hmacMode,
49+ } ,
50+ } ) ;
51+
3152 let resp : WebhookResponse | undefined ;
3253 switch ( data . type ) {
3354 case WebhooksEventTypes . CONTRACT_SUBSCRIPTION : {
@@ -61,6 +82,17 @@ const handler: Processor<string, void, string> = async (job: Job<string>) => {
6182 const transaction = await TransactionDB . get ( data . queueId ) ;
6283 if ( ! transaction ) {
6384 job . log ( "Transaction not found." ) ;
85+ logger ( {
86+ service : "worker" ,
87+ level : "warn" ,
88+ message : `[Webhook] Transaction not found for webhook` ,
89+ queueId : data . queueId ,
90+ data : {
91+ eventType : data . type ,
92+ destination : webhook . url ,
93+ webhookId : webhook . id ,
94+ } ,
95+ } ) ;
6496 return ;
6597 }
6698 const webhookBody : Static < typeof TransactionSchema > =
@@ -85,16 +117,44 @@ const handler: Processor<string, void, string> = async (job: Job<string>) => {
85117 }
86118 }
87119
120+ // Log the response
121+ if ( resp ) {
122+ const logLevel = resp . ok ? "info" : resp . status >= 500 ? "error" : "warn" ;
123+ logger ( {
124+ service : "worker" ,
125+ level : logLevel ,
126+ message : `[Webhook] Webhook response received: ${ resp . status } for transaction ${ transactionId } at destination ${ webhook . url } ` ,
127+ queueId : transactionId ,
128+ data : {
129+ eventType : data . type ,
130+ destination : webhook . url ,
131+ webhookId : webhook . id ,
132+ responseCode : resp . status ,
133+ responseOk : resp . ok ,
134+ hmacMode,
135+ responseBody : resp . body . substring ( 0 , 200 ) , // Truncate response body to first 200 chars
136+ } ,
137+ } ) ;
138+ }
139+
88140 // Throw on 5xx so it remains in the queue to retry later.
89141 if ( resp && resp . status >= 500 ) {
90142 const error = new Error (
91143 `Received status ${ resp . status } from webhook ${ webhook . url } .` ,
92144 ) ;
93145 job . log ( error . message ) ;
94146 logger ( {
95- level : "debug " ,
96- message : error . message ,
147+ level : "error " ,
148+ message : `[Webhook] 5xx error, will retry` ,
97149 service : "worker" ,
150+ queueId : transactionId ,
151+ data : {
152+ eventType : data . type ,
153+ destination : webhook . url ,
154+ webhookId : webhook . id ,
155+ responseCode : resp . status ,
156+ hmacMode,
157+ } ,
98158 } ) ;
99159 throw error ;
100160 }
0 commit comments