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.

Add cron to sync link.clicked webhook workspace cache with plan filtering

Kiran K21a259530209
diff --git a/apps/web/app/(ee)/api/cron/webhooks/sync-click-workspaces/route.ts b/apps/web/app/(ee)/api/cron/webhooks/sync-click-workspaces/route.ts
new file mode 100644
index 0000000000..7b21b1c7f1
--- /dev/null
+++ b/apps/web/app/(ee)/api/cron/webhooks/sync-click-workspaces/route.ts
@@ -0,0 +1,18 @@
+import { withCron } from "@/lib/cron/with-cron";
+import { rebuildClickWebhookWorkspaces } from "@/lib/webhook/click-webhook-workspaces";
+import { logAndRespond } from "../../utils";
+
+export const dynamic = "force-dynamic";
+
+// GET /api/cron/webhooks/sync-click-workspaces
+// Rebuild the Redis set of workspaces with active link.clicked webhooks.
+// Runs every minute (* * * * *)
+export const GET = withCron(async () => {
+ const count = await rebuildClickWebhookWorkspaces();
+
+ return logAndRespond(
+ count === 0
+ ? "No workspaces with link.clicked webhooks found. Cleared cache."
+ : `Synced ${count} workspace(s) with link.clicked webhooks.`,
+ );
+});
diff --git a/apps/web/lib/upstash/redis-streams/workspace-clicks.ts b/apps/web/lib/upstash/redis-streams/workspace-clicks.ts
index b1e51ddbb8..b0ceb4c365 100644
--- a/apps/web/lib/upstash/redis-streams/workspace-clicks.ts
+++ b/apps/web/lib/upstash/redis-streams/workspace-clicks.ts
@@ -22,7 +22,6 @@ export const publishClickEvent = async (
try {
return await redis.xadd(STREAM_KEY, "*", event);
} catch (error) {
- console.error("Failed to publish click event to stream:", error);
throw error;
// TODO
diff --git a/apps/web/lib/webhook/click-webhook-workspaces.ts b/apps/web/lib/webhook/click-webhook-workspaces.ts
index b7e7c5e7e3..82ba78a00d 100644
--- a/apps/web/lib/webhook/click-webhook-workspaces.ts
+++ b/apps/web/lib/webhook/click-webhook-workspaces.ts
@@ -1,6 +1,9 @@
+import { prisma } from "@/lib/prisma";
import { redis } from "@/lib/upstash";
+import { LINK_CLICK_WEBHOOK_TRIGGER } from "./constants";
export const REDIS_KEY = "webhookClickWorkspaces";
+const TMP_REDIS_KEY = `${REDIS_KEY}:tmp`;
class ClickWebhookWorkspaces {
async set(workspaceId: string) {
@@ -16,4 +19,38 @@ class ClickWebhookWorkspaces {
}
}
+// Rebuild the Redis set of workspaces with active link.clicked webhooks.
+export const rebuildClickWebhookWorkspaces = async () => {
+ const webhooks = await prisma.webhook.findMany({
+ where: {
+ disabledAt: null,
+ triggers: {
+ array_contains: [LINK_CLICK_WEBHOOK_TRIGGER],
+ },
+ project: {
+ plan: {
+ notIn: ["free", "pro"],
+ },
+ },
+ },
+ select: {
+ projectId: true,
+ },
+ distinct: ["projectId"],
+ });
+
+ const workspaceIds = webhooks.map((webhook) => webhook.projectId);
+
+ if (workspaceIds.length === 0) {
+ await redis.del(REDIS_KEY);
+ return 0;
+ }
+
+ await redis.del(TMP_REDIS_KEY);
+ await redis.sadd(TMP_REDIS_KEY, ...(workspaceIds as [string, ...string[]]));
+ await redis.rename(TMP_REDIS_KEY, REDIS_KEY);
+
+ return workspaceIds.length;
+};
+
export const clickWebhookWorkspaces = new ClickWebhookWorkspaces();
diff --git a/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts b/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts
index 2bdd345e25..d2ff1045f6 100644
--- a/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts
+++ b/apps/web/scripts/migrations/backfill-click-webhook-workspaces.ts
@@ -1,32 +1,9 @@
-import { prisma } from "@/lib/prisma";
-import { redis } from "@/lib/upstash";
-import { REDIS_KEY } from "@/lib/webhook/click-webhook-workspaces";
-import { LINK_CLICK_WEBHOOK_TRIGGER } from "@/lib/webhook/constants";
+import { rebuildClickWebhookWorkspaces } from "@/lib/webhook/click-webhook-workspaces";
import "dotenv-flow/config";
async function main() {
- const webhooks = await prisma.webhook.findMany({
- where: {
- triggers: {
- array_contains: [LINK_CLICK_WEBHOOK_TRIGGER],
- },
- },
- select: {
- projectId: true,
- },
- distinct: ["projectId"],
- });
-
- const workspaceIds = webhooks.map((webhook) => webhook.projectId);
-
- if (workspaceIds.length === 0) {
- console.log("No workspaces with link.clicked webhooks found.");
- return;
- }
-
- await redis.sadd(REDIS_KEY, ...(workspaceIds as [string, ...string[]]));
-
- console.log(`Added ${workspaceIds.length} workspace(s) to ${REDIS_KEY}.`);
+ const count = await rebuildClickWebhookWorkspaces();
+ console.log(`Rebuilt webhookClickWorkspaces with ${count} workspace(s).`);
}
main();
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
index 9efda2aae5..a4a0070c35 100644
--- a/apps/web/vercel.json
+++ b/apps/web/vercel.json
@@ -75,6 +75,10 @@
{
"path": "/api/cron/sitemaps/queue",
"schedule": "0 2 * * *"
+ },
+ {
+ "path": "/api/cron/webhooks/sync-click-workspaces",
+ "schedule": "* * * * *"
}
],
"functions": {