Skip to content

armgit / dub

Public Imported from GitHub
0

The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.

Prevent overlapping click webhook cron runs and localize link target options

Kiran K4c37b8732ccd
diff --git a/apps/web/app/(ee)/api/cron/streams/send-link-click-webhooks/route.ts b/apps/web/app/(ee)/api/cron/streams/send-link-click-webhooks/route.ts
index 4748aff0b5..423a3fed0c 100644
--- a/apps/web/app/(ee)/api/cron/streams/send-link-click-webhooks/route.ts
+++ b/apps/web/app/(ee)/api/cron/streams/send-link-click-webhooks/route.ts
@@ -2,6 +2,7 @@ import { transformLink } from "@/lib/api/links/utils/transform-link";
import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { ClickEventTB } from "@/lib/types";
+import { redis } from "@/lib/upstash/redis";
import { RedisStreamEntry } from "@/lib/upstash/redis-streams/client";
import { workspaceClickEventStream } from "@/lib/upstash/redis-streams/workspace-click-events";
import { LINK_CLICK_WEBHOOK_TRIGGER } from "@/lib/webhook/constants";
@@ -9,6 +10,7 @@ import { sendWebhooks } from "@/lib/webhook/qstash";
import { transformClickEventData } from "@/lib/webhook/transform";
import { chunk, groupBy } from "@dub/utils";
import { NextResponse } from "next/server";
+import { logAndRespond } from "../../utils";
export const dynamic = "force-dynamic";
@@ -18,23 +20,43 @@ const BATCH_SIZE = 500;
// How many webhook deliveries to enqueue to QStash in parallel
const SEND_CONCURRENCY = 100;
+// Lock for the cron job
+const LOCK_KEY = "lock:send-link-click-webhooks";
+const LOCK_TTL_SECONDS = 300;
+
// Drains the workspace:click:events stream and delivers link.clicked webhooks.
// Runs every minute (see vercel.json).
+// GET /api/cron/streams/send-link-click-webhooks
export const GET = withCron(async () => {
- const { processed, sent, failed, processedEntryIds } =
- await processStreamBatch();
-
- const streamInfo = await workspaceClickEventStream.getStreamInfo();
-
- console.log({
- processed,
- sent,
- failed,
- processedEntryIds,
- streamInfo,
+ const acquired = await redis.set(LOCK_KEY, "1", {
+ nx: true,
+ ex: LOCK_TTL_SECONDS,
});
- return NextResponse.json("Finished processing stream.");
+ if (!acquired) {
+ return logAndRespond(
+ "[send-link-click-webhooks] Another run is in progress. Skipping...",
+ );
+ }
+
+ try {
+ const { processed, sent, failed, processedEntryIds } =
+ await processStreamBatch();
+
+ const streamInfo = await workspaceClickEventStream.getStreamInfo();
+
+ console.log({
+ processed,
+ sent,
+ failed,
+ processedEntryIds,
+ streamInfo,
+ });
+
+ return NextResponse.json("Finished processing stream.");
+ } finally {
+ await redis.del(LOCK_KEY);
+ }
});
const processStreamBatch = (): Promise<{
diff --git a/apps/web/lib/webhook/constants.ts b/apps/web/lib/webhook/constants.ts
index 423bfb1b88..2fe8c84e79 100644
--- a/apps/web/lib/webhook/constants.ts
+++ b/apps/web/lib/webhook/constants.ts
@@ -1,4 +1,3 @@
-import { LinkTarget } from "@prisma/client";
import type { WebhookTrigger } from "./types";
export const WEBHOOK_SECRET_LENGTH = 16;
@@ -28,28 +27,6 @@ export const PROGRAM_LEVEL_WEBHOOK_TRIGGERS = [
"payout.confirmed",
] as const;
-export const CLICK_WEBHOOK_LINK_TARGET_OPTIONS = [
- {
- value: "workspace",
- label: "All links",
- description: "Trigger webhooks for all links in your workspace",
- },
- {
- value: "folders",
- label: "Include specific folders",
- description: "Trigger webhooks for all links from a selected folder",
- },
- {
- value: "links",
- label: "Include specific links",
- description: "Trigger webhooks for the links you explicitly select",
- },
-] as const satisfies ReadonlyArray<{
- value: LinkTarget;
- label: string;
- description: string;
-}>;
-
export const WEBHOOK_TRIGGERS = [
...WORKSPACE_LEVEL_WEBHOOK_TRIGGERS,
...PROGRAM_LEVEL_WEBHOOK_TRIGGERS,
diff --git a/apps/web/ui/webhooks/webhook-trigger-selector.tsx b/apps/web/ui/webhooks/webhook-trigger-selector.tsx
index 9dcbca6494..2fc7754d52 100644
--- a/apps/web/ui/webhooks/webhook-trigger-selector.tsx
+++ b/apps/web/ui/webhooks/webhook-trigger-selector.tsx
@@ -2,7 +2,6 @@
import useWorkspace from "@/lib/swr/use-workspace";
import {
- CLICK_WEBHOOK_LINK_TARGET_OPTIONS,
LINK_CLICK_WEBHOOK_TRIGGER,
WEBHOOK_TRIGGER_DESCRIPTIONS,
} from "@/lib/webhook/constants";
@@ -15,6 +14,28 @@ import useSWR from "swr";
import { FoldersSelector } from "./folder-selector";
import { LinksSelector } from "./link-selector";
+const CLICK_WEBHOOK_LINK_TARGET_OPTIONS = [
+ {
+ value: "workspace",
+ label: "All links",
+ description: "Trigger webhooks for all links in your workspace",
+ },
+ {
+ value: "folders",
+ label: "Include specific folders (Recommended)",
+ description: "Trigger webhooks for all links from a selected folders",
+ },
+ {
+ value: "links",
+ label: "Include specific links",
+ description: "Trigger webhooks for the links you explicitly select",
+ },
+] as const satisfies ReadonlyArray<{
+ value: LinkTarget;
+ label: string;
+ description: string;
+}>;
+
export type WebhookTriggerSelectorValue = {
triggers: WebhookTrigger[];
linkTarget: LinkTarget;