Skip to content

Commit cb08f6b

Browse files
committed
Refactor WebhookController to implement retry logic for webhook requests
- Introduced a new `retryWebhookRequest` method to handle retries for failed webhook requests, allowing up to 10 attempts with a delay of 30 seconds between each. - Updated error logging to provide detailed information on each retry attempt, including the attempt number and error details. - Enhanced existing webhook request handling to utilize the new retry logic, improving reliability in sending webhook data. - Modified error messages to be more informative, indicating when all retry attempts have failed.
1 parent a817d62 commit cb08f6b

File tree

1 file changed

+58
-5
lines changed

1 file changed

+58
-5
lines changed

src/api/integrations/event/webhook/webhook.controller.ts

Lines changed: 58 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ import { wa } from '@api/types/wa.types';
55
import { configService, Log, Webhook } from '@config/env.config';
66
import { Logger } from '@config/logger.config';
77
import { BadRequestException } from '@exceptions';
8-
import axios from 'axios';
8+
import axios, { AxiosInstance } from 'axios';
99
import { isURL } from 'class-validator';
1010

1111
import { EmitData, EventController, EventControllerInterface } from '../event.controller';
@@ -117,12 +117,12 @@ export class WebhookController extends EventController implements EventControlle
117117
headers: webhookHeaders as Record<string, string> | undefined,
118118
});
119119

120-
await httpService.post('', webhookData);
120+
await this.retryWebhookRequest(httpService, webhookData, `${origin}.sendData-Webhook`, baseURL, serverUrl);
121121
}
122122
} catch (error) {
123123
this.logger.error({
124124
local: `${origin}.sendData-Webhook`,
125-
message: error?.message,
125+
message: `Todas as tentativas falharam: ${error?.message}`,
126126
hostName: error?.hostname,
127127
syscall: error?.syscall,
128128
code: error?.code,
@@ -158,12 +158,18 @@ export class WebhookController extends EventController implements EventControlle
158158
if (isURL(globalURL)) {
159159
const httpService = axios.create({ baseURL: globalURL });
160160

161-
await httpService.post('', webhookData);
161+
await this.retryWebhookRequest(
162+
httpService,
163+
webhookData,
164+
`${origin}.sendData-Webhook-Global`,
165+
globalURL,
166+
serverUrl,
167+
);
162168
}
163169
} catch (error) {
164170
this.logger.error({
165171
local: `${origin}.sendData-Webhook-Global`,
166-
message: error?.message,
172+
message: `Todas as tentativas falharam: ${error?.message}`,
167173
hostName: error?.hostname,
168174
syscall: error?.syscall,
169175
code: error?.code,
@@ -177,4 +183,51 @@ export class WebhookController extends EventController implements EventControlle
177183
}
178184
}
179185
}
186+
187+
private async retryWebhookRequest(
188+
httpService: AxiosInstance,
189+
webhookData: any,
190+
origin: string,
191+
baseURL: string,
192+
serverUrl: string,
193+
maxRetries = 10,
194+
delaySeconds = 30,
195+
): Promise<void> {
196+
let attempts = 0;
197+
198+
while (attempts < maxRetries) {
199+
try {
200+
await httpService.post('', webhookData);
201+
if (attempts > 0) {
202+
this.logger.log({
203+
local: `${origin}`,
204+
message: `Sucesso no envio após ${attempts + 1} tentativas`,
205+
url: baseURL,
206+
});
207+
}
208+
return;
209+
} catch (error) {
210+
attempts++;
211+
212+
this.logger.error({
213+
local: `${origin}`,
214+
message: `Tentativa ${attempts}/${maxRetries} falhou: ${error?.message}`,
215+
hostName: error?.hostname,
216+
syscall: error?.syscall,
217+
code: error?.code,
218+
error: error?.errno,
219+
stack: error?.stack,
220+
name: error?.name,
221+
url: baseURL,
222+
server_url: serverUrl,
223+
});
224+
225+
if (attempts === maxRetries) {
226+
throw error;
227+
}
228+
229+
await new Promise((resolve) => setTimeout(resolve, delaySeconds * 1000));
230+
}
231+
}
232+
}
180233
}

0 commit comments

Comments
 (0)