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.

wip

Kiran K72ae227dcc43
diff --git a/apps/web/app/api/cron/domains/update/route.ts b/apps/web/app/api/cron/domains/update/route.ts
new file mode 100644
index 0000000000..8894a0e66e
--- /dev/null
+++ b/apps/web/app/api/cron/domains/update/route.ts
@@ -0,0 +1,95 @@
+import { queueDomainUpdate } from "@/lib/api/domains/queue";
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { linkCache } from "@/lib/api/links/cache";
+import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { prisma } from "@/lib/prisma";
+import { recordLink } from "@/lib/tinybird";
+import { z } from "zod";
+
+export const dynamic = "force-dynamic";
+
+const schema = z.object({
+ newDomain: z.string(),
+ oldDomain: z.string(),
+ workspaceId: z.string(),
+ page: z.number(),
+});
+
+const pageSize = 2;
+
+// POST /api/cron/domains/update
+export async function POST(req: Request) {
+ try {
+ const body = await req.json();
+
+ await verifyQstashSignature(req, body);
+
+ const { newDomain, oldDomain, workspaceId, page } = schema.parse(body);
+
+ const newDomainRecord = await prisma.domain.findUnique({
+ where: {
+ slug: newDomain,
+ },
+ });
+
+ if (!newDomainRecord) {
+ return new Response(`Domain ${newDomain} not found. Skipping update...`);
+ }
+
+ const links = await prisma.link.findMany({
+ where: {
+ domain: newDomain,
+ },
+ include: {
+ tags: true,
+ },
+ skip: (page - 1) * pageSize,
+ take: pageSize,
+ });
+
+ if (links.length === 0) {
+ return new Response("No more links to update. Exiting...");
+ }
+
+ await linkCache.rename({
+ links,
+ oldDomain,
+ });
+
+ await Promise.all([
+ // rename redis keys
+ linkCache.rename({
+ links,
+ oldDomain,
+ }),
+
+ // update links in Tinybird
+ recordLink(
+ links.map((link) => ({
+ link_id: link.id,
+ domain: link.domain,
+ key: link.key,
+ url: link.url,
+ tag_ids: link.tags.map((tag) => tag.tagId),
+ program_id: link.programId ?? "",
+ workspace_id: link.projectId,
+ created_at: link.createdAt,
+ })),
+ ),
+ ]);
+
+ await queueDomainUpdate({
+ workspaceId,
+ oldDomain,
+ newDomain,
+ page: page + 1,
+ delay: 1000,
+ });
+
+ console.log(`Updated page ${page + 1} of links for domain ${oldDomain}`);
+
+ return new Response("Domain's links updated.");
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/app/api/domains/[domain]/route.ts b/apps/web/app/api/domains/[domain]/route.ts
index 1ce954a797..bf08451ff4 100644
--- a/apps/web/app/api/domains/[domain]/route.ts
+++ b/apps/web/app/api/domains/[domain]/route.ts
@@ -5,12 +5,11 @@ import {
validateDomain,
} from "@/lib/api/domains";
import { getDomainOrThrow } from "@/lib/api/domains/get-domain-or-throw";
+import { queueDomainUpdate } from "@/lib/api/domains/queue";
import { DubApiError } from "@/lib/api/errors";
-import { linkCache } from "@/lib/api/links/cache";
import { parseRequestBody } from "@/lib/api/utils";
import { withWorkspace } from "@/lib/auth";
import { prisma } from "@/lib/prisma";
-import { recordLink } from "@/lib/tinybird";
import {
DomainSchema,
updateDomainBodySchema,
@@ -103,43 +102,22 @@ export const PATCH = withWorkspace(
},
});
- waitUntil(
- (async () => {
- if (domainUpdated) {
+ if (domainUpdated) {
+ waitUntil(
+ Promise.all([
// remove old domain from Vercel
- await removeDomainFromVercel(domain);
-
- const allLinks = await prisma.link.findMany({
- where: {
- domain: newDomain,
- },
- include: {
- tags: true,
- },
- });
+ removeDomainFromVercel(domain),
- // rename redis keys
- await linkCache.rename({
- links: allLinks,
+ // trigger the queue to rename the redis keys and update the links in Tinybird
+ queueDomainUpdate({
+ workspaceId: workspace.id,
oldDomain: domain,
- });
-
- // update all links in Tinybird
- recordLink(
- allLinks.map((link) => ({
- link_id: link.id,
- domain: link.domain,
- key: link.key,
- url: link.url,
- tag_ids: link.tags.map((tag) => tag.tagId),
- program_id: link.programId ?? "",
- workspace_id: link.projectId,
- created_at: link.createdAt,
- })),
- );
- }
- })(),
- );
+ newDomain: newDomain,
+ page: 1,
+ }),
+ ]),
+ );
+ }
return NextResponse.json(DomainSchema.parse(domainRecord));
},
diff --git a/apps/web/app/api/links/bulk/route.ts b/apps/web/app/api/links/bulk/route.ts
index 85298ff897..be8c764e3f 100644
--- a/apps/web/app/api/links/bulk/route.ts
+++ b/apps/web/app/api/links/bulk/route.ts
@@ -401,6 +401,8 @@ export const DELETE = withWorkspace(
},
});
+ console.log(links);
+
const { count: deletedCount } = await prisma.link.deleteMany({
where: {
id: { in: links.map((link) => link.id) },
diff --git a/apps/web/lib/api/domains/delete-domain-links.ts b/apps/web/lib/api/domains/delete-domain-links.ts
index 9e51d48093..5041c4b7ed 100644
--- a/apps/web/lib/api/domains/delete-domain-links.ts
+++ b/apps/web/lib/api/domains/delete-domain-links.ts
@@ -1,9 +1,9 @@
-import { qstash } from "@/lib/cron";
import { prisma } from "@/lib/prisma";
-import { APP_DOMAIN_WITH_NGROK, R2_URL } from "@dub/utils";
+import { R2_URL } from "@dub/utils";
import { storage } from "../../storage";
import { recordLink } from "../../tinybird";
import { linkCache } from "../links/cache";
+import { queueDomainDeletion } from "./queue";
import { removeDomainFromVercel } from "./remove-domain-vercel";
// Delete a domain and all links & images associated with it
@@ -152,22 +152,3 @@ export async function markDomainAsDeleted({
}
});
}
-
-async function queueDomainDeletion({
- workspaceId,
- domain,
- delay,
-}: {
- workspaceId: string;
- domain: string;
- delay?: number;
-}) {
- return await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/delete`,
- ...(delay && { delay }),
- body: {
- workspaceId,
- domain,
- },
- });
-}
diff --git a/apps/web/lib/api/domains/queue.ts b/apps/web/lib/api/domains/queue.ts
new file mode 100644
index 0000000000..8584580532
--- /dev/null
+++ b/apps/web/lib/api/domains/queue.ts
@@ -0,0 +1,46 @@
+import { qstash } from "@/lib/cron";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+
+export async function queueDomainUpdate({
+ workspaceId,
+ oldDomain,
+ newDomain,
+ page,
+ delay,
+}: {
+ workspaceId: string;
+ oldDomain: string;
+ newDomain: string;
+ page: number;
+ delay?: number;
+}) {
+ return await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/update`,
+ ...(delay && { delay }),
+ body: {
+ workspaceId,
+ oldDomain,
+ newDomain,
+ page,
+ },
+ });
+}
+
+export async function queueDomainDeletion({
+ workspaceId,
+ domain,
+ delay,
+}: {
+ workspaceId: string;
+ domain: string;
+ delay?: number;
+}) {
+ return await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/delete`,
+ ...(delay && { delay }),
+ body: {
+ workspaceId,
+ domain,
+ },
+ });
+}
diff --git a/apps/web/lib/api/links/cache.ts b/apps/web/lib/api/links/cache.ts
index 18aacc88d0..fc82a9f82d 100644
--- a/apps/web/lib/api/links/cache.ts
+++ b/apps/web/lib/api/links/cache.ts
@@ -73,18 +73,24 @@ class LinkCache {
}
async rename({
- oldDomain,
links,
+ oldDomain,
}: {
- oldDomain: string;
links: Pick<LinkProps, "domain" | "key">[];
+ oldDomain: string;
}) {
+ if (links.length === 0) {
+ return;
+ }
+
const pipeline = redis.pipeline();
links.forEach(({ domain, key }) => {
const oldCacheKey = this._createKey({ domain: oldDomain, key });
const newCacheKey = this._createKey({ domain, key });
+ console.log(`Renaming ${oldCacheKey} to ${newCacheKey}`);
+
pipeline.rename(oldCacheKey, newCacheKey);
});