Skip to content
Closed
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
5 changes: 5 additions & 0 deletions apps/web/src/server/service/webhook-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -80,6 +80,11 @@ export class WebhookQueueService {
}

public static async enqueueCall(callId: string, teamId: number) {
// BullMQ ignores add() when a job with the same id already exists, in any state.
// DEFAULT_QUEUE_OPTIONS keeps failed jobs for 30 days, so a retry that reuses the
// call id as the job id finds the retained failed job and adds nothing. remove()
// clears it first (it returns 0 for a locked or missing job), so the retry is queued.
await this.queue.remove(callId);
await this.queue.add(
callId,
{
Expand Down
78 changes: 77 additions & 1 deletion apps/web/src/server/service/webhook-service.unit.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ const {
mockLimitService,
mockLogger,
mockQueueAdd,
mockQueueRemove,
mockRedis,
mockTxWebhookUpdate,
} = vi.hoisted(() => ({
Expand Down Expand Up @@ -43,6 +44,7 @@ const {
warn: vi.fn(),
},
mockQueueAdd: vi.fn(),
mockQueueRemove: vi.fn(),
mockRedis: {
eval: vi.fn(),
set: vi.fn(),
Expand All @@ -53,6 +55,7 @@ const {
vi.mock("bullmq", () => ({
Queue: class {
public add = mockQueueAdd;
public remove = mockQueueRemove;
},
Worker: class {
public on = vi.fn();
Expand Down Expand Up @@ -94,7 +97,10 @@ vi.mock("~/server/redis", () => ({
redisKey: (key: string) => key,
}));

import { WebhookService } from "~/server/service/webhook-service";
import {
WebhookQueueService,
WebhookService,
} from "~/server/service/webhook-service";

function buildCall(overrides?: {
consecutiveFailures?: number;
Expand Down Expand Up @@ -154,6 +160,7 @@ describe("WebhookService documented behavior", () => {
mockLogger.warn.mockReset();
mockLimitService.checkWebhookLimit.mockReset();
mockQueueAdd.mockReset();
mockQueueRemove.mockReset();
mockRedis.eval.mockReset();
mockRedis.set.mockReset();
mockTxWebhookUpdate.mockReset();
Expand All @@ -166,6 +173,7 @@ describe("WebhookService documented behavior", () => {
mockRedis.set.mockResolvedValue("OK");
mockRedis.eval.mockResolvedValue(1);
mockQueueAdd.mockResolvedValue(undefined);
mockQueueRemove.mockResolvedValue(1);
mockDb.webhookCall.update.mockResolvedValue({});
mockDb.webhook.update.mockResolvedValue({
id: "wh_123",
Expand Down Expand Up @@ -567,11 +575,13 @@ describe("WebhookService.emit domain filters", () => {
mockDb.webhook.findMany.mockReset();
mockDb.webhookCall.create.mockReset();
mockQueueAdd.mockReset();
mockQueueRemove.mockReset();

mockDb.webhookCall.create.mockImplementation(async ({ data }: any) => ({
id: `call_${data.webhookId}`,
}));
mockQueueAdd.mockResolvedValue(undefined);
mockQueueRemove.mockResolvedValue(1);
});

it("does not apply domain filtering when the event has no domain context", async () => {
Expand Down Expand Up @@ -734,3 +744,69 @@ describe("WebhookService.emit domain filters", () => {
expect(mockQueueAdd).toHaveBeenCalledTimes(2);
});
});

describe("WebhookQueueService.enqueueCall retry behavior", () => {
beforeEach(() => {
mockDb.webhookCall.findFirst.mockReset();
mockDb.webhookCall.update.mockReset();
mockQueueAdd.mockReset();
mockQueueRemove.mockReset();

mockDb.webhookCall.update.mockResolvedValue({});
mockQueueAdd.mockResolvedValue(undefined);
mockQueueRemove.mockResolvedValue(1);
});

it("clears a retained failed job before re-enqueueing", async () => {
await WebhookQueueService.enqueueCall("call_123", 77);

expect(mockQueueRemove).toHaveBeenCalledWith("call_123");
expect(mockQueueAdd).toHaveBeenCalledWith(
"call_123",
{ callId: "call_123", teamId: 77 },
{ jobId: "call_123" },
);
expect(mockQueueRemove.mock.invocationCallOrder[0]).toBeLessThan(
mockQueueAdd.mock.invocationCallOrder[0]!,
);
});

it("redelivers a failed call when retryCall runs", async () => {
mockDb.webhookCall.findFirst.mockResolvedValue({
id: "call_123",
teamId: 77,
});

await WebhookService.retryCall({ callId: "call_123", teamId: 77 });

expect(mockDb.webhookCall.update).toHaveBeenCalledWith({
where: { id: "call_123" },
data: {
status: WebhookCallStatus.PENDING,
attempt: 0,
nextAttemptAt: null,
lastError: null,
responseStatus: null,
responseTimeMs: null,
responseText: null,
},
});
expect(mockQueueRemove).toHaveBeenCalledWith("call_123");
expect(mockQueueAdd).toHaveBeenCalledWith(
"call_123",
{ callId: "call_123", teamId: 77 },
{ jobId: "call_123" },
);
});

it("rejects a retry for a call that is not on the team", async () => {
mockDb.webhookCall.findFirst.mockResolvedValue(null);

await expect(
WebhookService.retryCall({ callId: "missing", teamId: 77 }),
).rejects.toThrow("Webhook call not found");

expect(mockQueueRemove).not.toHaveBeenCalled();
expect(mockQueueAdd).not.toHaveBeenCalled();
});
});
Loading