Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
15 changes: 12 additions & 3 deletions src/modules/webhooks/webhook.service.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -137,6 +137,7 @@ describe('dispatchWebhookEvent', () => {
beforeEach(() => {
jest.useFakeTimers();
global.fetch = jest.fn();
jest.spyOn(Math, 'random').mockReturnValue(0);
});

afterEach(() => {
Expand Down Expand Up @@ -313,17 +314,25 @@ describe('dispatchWebhookEvent', () => {
}),
'Webhook delivery failed, retrying'
);
expect(logger.error).toHaveBeenCalledWith(
const exhaustionLogCalls = (logger.error as jest.Mock).mock.calls.filter(
([, message]) =>
message === 'Webhook delivery exhausted all retries, flagged as failing'
);
expect(exhaustionLogCalls).toHaveLength(1);

const [exhaustionLogFields] = exhaustionLogCalls[0];
expect(exhaustionLogFields).toEqual(
expect.objectContaining({
webhook_id: 'wh-1',
creator_id: 'creator-1',
event_type: 'sell',
total_attempts: envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS,
last_error_code: 'Network error',
flagged_at: expect.any(String),
}),
'Webhook delivery exhausted all retries, flagged as failing'
})
);
expect(exhaustionLogFields.callback_url).toBeUndefined();
expect(exhaustionLogFields.callbackUrl).toBeUndefined();

// Verify attempt log was emitted for every retry attempt (success: false, response_status: null)
for (let attempt = 1; attempt <= envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS; attempt++) {
Expand Down
8 changes: 7 additions & 1 deletion src/modules/webhooks/webhook.service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ import { prisma } from '../../utils/prisma.utils';
import { logger } from '../../utils/logger.utils';
import { envConfig } from '../../config';
import { maskWebhookUrl } from '../../utils/webhook-mask.utils';
import { computeRetryDelay } from '../../utils/retry-delay.utils';
import { buildWebhookPayload } from './webhook-payload.utils';
import type {
CreateWebhookInput,
Expand Down Expand Up @@ -193,6 +194,7 @@ async function attemptDelivery(
attempt = 1
): Promise<void> {
const maxAttempts = envConfig.WEBHOOK_RETRY_MAX_ATTEMPTS;
const maxDelayMs = 30_000;
const startTime = Date.now();
let responseStatus: number | null = null;
let responseTimeMs = 0;
Expand Down Expand Up @@ -282,7 +284,11 @@ async function attemptDelivery(
});

if (attempt < maxAttempts) {
const delay = Math.pow(2, attempt) * 1000;
const delay = computeRetryDelay(
attempt,
envConfig.WEBHOOK_RETRY_BASE_DELAY_MS,
maxDelayMs
);
logger.warn(
{
webhook_id: webhookId,
Expand Down
Loading