Skip to content

Commit 4ac795b

Browse files
authored
Merge pull request #731 from nomsoscript/fix/webhook-retry-delay-597
fix: use shared webhook retry delay helper
2 parents d4706fe + 78ab785 commit 4ac795b

2 files changed

Lines changed: 19 additions & 4 deletions

File tree

src/modules/webhooks/webhook.service.test.ts

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -137,6 +137,7 @@ describe('dispatchWebhookEvent', () => {
137137
beforeEach(() => {
138138
jest.useFakeTimers();
139139
global.fetch = jest.fn();
140+
jest.spyOn(Math, 'random').mockReturnValue(0);
140141
});
141142

142143
afterEach(() => {
@@ -313,17 +314,25 @@ describe('dispatchWebhookEvent', () => {
313314
}),
314315
'Webhook delivery failed, retrying'
315316
);
316-
expect(logger.error).toHaveBeenCalledWith(
317+
const exhaustionLogCalls = (logger.error as jest.Mock).mock.calls.filter(
318+
([, message]) =>
319+
message === 'Webhook delivery exhausted all retries, flagged as failing'
320+
);
321+
expect(exhaustionLogCalls).toHaveLength(1);
322+
323+
const [exhaustionLogFields] = exhaustionLogCalls[0];
324+
expect(exhaustionLogFields).toEqual(
317325
expect.objectContaining({
318326
webhook_id: 'wh-1',
319327
creator_id: 'creator-1',
320328
event_type: 'sell',
321329
total_attempts: envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS,
322330
last_error_code: 'Network error',
323331
flagged_at: expect.any(String),
324-
}),
325-
'Webhook delivery exhausted all retries, flagged as failing'
332+
})
326333
);
334+
expect(exhaustionLogFields.callback_url).toBeUndefined();
335+
expect(exhaustionLogFields.callbackUrl).toBeUndefined();
327336

328337
// Verify attempt log was emitted for every retry attempt (success: false, response_status: null)
329338
for (let attempt = 1; attempt <= envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS; attempt++) {

src/modules/webhooks/webhook.service.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ import { prisma } from '../../utils/prisma.utils';
22
import { logger } from '../../utils/logger.utils';
33
import { envConfig } from '../../config';
44
import { maskWebhookUrl } from '../../utils/webhook-mask.utils';
5+
import { computeRetryDelay } from '../../utils/retry-delay.utils';
56
import { buildWebhookPayload } from './webhook-payload.utils';
67
import type {
78
CreateWebhookInput,
@@ -193,6 +194,7 @@ async function attemptDelivery(
193194
attempt = 1
194195
): Promise<void> {
195196
const maxAttempts = envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS;
197+
const maxDelayMs = 30_000;
196198
const startTime = Date.now();
197199
let responseStatus: number | null = null;
198200
let responseTimeMs = 0;
@@ -282,7 +284,11 @@ async function attemptDelivery(
282284
});
283285

284286
if (attempt < maxAttempts) {
285-
const delay = Math.pow(2, attempt) * 1000;
287+
const delay = computeRetryDelay(
288+
attempt,
289+
envConfig.WEBHOOK_RETRY_BASE_DELAY_MS,
290+
maxDelayMs
291+
);
286292
logger.warn(
287293
{
288294
webhook_id: webhookId,

0 commit comments

Comments
 (0)