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.

Migrate domain deletion cleanup to background job (#4196)

Kiran Kb8cb26bffa5e
diff --git a/apps/web/app/(ee)/api/cron/domains/delete/route.ts b/apps/web/app/(ee)/api/cron/domains/delete/route.ts
index aefe05832a..10c2928b0a 100644
--- a/apps/web/app/(ee)/api/cron/domains/delete/route.ts
+++ b/apps/web/app/(ee)/api/cron/domains/delete/route.ts
@@ -1,14 +1,5 @@
-import { queueDomainDeletion } from "@/lib/api/domains/queue-domain-update";
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { linkCache } from "@/lib/api/links/cache";
-import { includeProgramEnrollment } from "@/lib/api/links/include-program-enrollment";
-import { includeTags } from "@/lib/api/links/include-tags";
-import { limiter } from "@/lib/cron/limiter";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
-import { prisma } from "@/lib/prisma";
-import { storage } from "@/lib/storage";
-import { recordLink } from "@/lib/tinybird/record-link";
-import { R2_URL } from "@dub/utils";
+import { withCron } from "@/lib/cron/with-cron";
+import { domainDeletedJob } from "@/lib/jobs/handlers/domain-deleted-job";
import * as z from "zod/v4";
import { logAndRespond } from "../../utils";
@@ -18,123 +9,18 @@ const schema = z.object({
domain: z.string(),
});
-// POST /api/cron/domains/delete
-export async function POST(req: Request) {
- try {
- const rawBody = await req.text();
- await verifyQstashSignature({ req, rawBody });
-
- const { domain } = schema.parse(JSON.parse(rawBody));
-
- const domainRecord = await prisma.domain.findUnique({
- where: {
- slug: domain,
- },
- });
-
- if (!domainRecord) {
- return logAndRespond(`Domain ${domain} not found. Skipping...`);
- }
-
- const links = await prisma.link.findMany({
- where: {
- domain,
- },
- include: {
- ...includeTags,
- ...includeProgramEnrollment,
- },
- take: 100,
- orderBy: {
- createdAt: "desc",
- },
- });
-
- console.log(`Found ${links.length} links to delete`);
-
- if (links.length > 0) {
- const response = await Promise.allSettled([
- // Remove the link from Redis
- linkCache.deleteMany(links),
-
- // Record link in Tinybird
- recordLink(links, { deleted: true }),
+// Legacy QStash endpoint kept for in-flight messages published before the
+// jobs migration
- // Remove image from R2 storage if it exists
- links
- .filter((link) =>
- link.image?.startsWith(`${R2_URL}/images/${link.id}`),
- )
- .map((link) =>
- limiter.schedule(() =>
- storage.delete({ key: link.image!.replace(`${R2_URL}/`, "") }),
- ),
- ),
+// TODO: Remove this route after old QStash traffic has drained.
- // Remove the link from MySQL
- prisma.link.deleteMany({
- where: {
- id: { in: links.map((link) => link.id) },
- },
- }),
-
- // Update the project's total links count
- links[0].projectId &&
- prisma.project.update({
- where: {
- id: links[0].projectId,
- },
- data: {
- totalLinks: { decrement: links.length },
- },
- }),
- ]);
-
- console.log(response);
-
- response.forEach((promise) => {
- if (promise.status === "rejected") {
- console.error("deleteDomainAndLinks", {
- reason: promise.reason,
- domain,
- });
- }
- });
- }
-
- const remainingLinks = await prisma.link.count({
- where: {
- domain,
- },
- });
-
- console.log(`Found ${remainingLinks} remaining links for ${domain}.`);
-
- if (remainingLinks > 0) {
- await queueDomainDeletion({
- domain,
- delay: 2,
- });
- return logAndRespond(
- `Deleted ${links.length} links, ${remainingLinks} remaining. Starting next batch...`,
- );
- }
+// POST /api/cron/domains/delete
+export const POST = withCron(async ({ rawBody }) => {
+ const { domain } = schema.parse(JSON.parse(rawBody));
- // After all links are deleted, delete the domain and image
- await Promise.all([
- prisma.domain.delete({
- where: {
- slug: domain,
- },
- }),
- domainRecord.logo &&
- storage.delete({ key: domainRecord.logo.replace(`${R2_URL}/`, "") }),
- ]);
+ await domainDeletedJob.execute({ domain });
- return logAndRespond(
- `Deleted ${links.length} links, no more links remaining. Domain deleted.`,
- );
- } catch (error) {
- return handleAndReturnErrorResponse(error);
- }
-}
+ return logAndRespond(
+ `Forwarded domain ${domain} deletion to domainDeletedJob.`,
+ );
+});
diff --git a/apps/web/lib/api/domains/mark-domain-deleted.ts b/apps/web/lib/api/domains/mark-domain-deleted.ts
index 7b34cd06ed..24634a8772 100644
--- a/apps/web/lib/api/domains/mark-domain-deleted.ts
+++ b/apps/web/lib/api/domains/mark-domain-deleted.ts
@@ -1,33 +1,24 @@
+import { domainDeletedJob } from "@/lib/jobs/handlers/domain-deleted-job";
import { prisma } from "@/lib/prisma";
-import { queueDomainDeletion } from "./queue-domain-update";
-import { removeDomainFromVercel } from "./remove-domain-vercel";
// Mark the domain as deleted
-// We'll delete the domain and its links via a cron job
+// We'll delete the domain and its links via a background job
export async function markDomainAsDeleted({ domain }: { domain: string }) {
- const response = await Promise.allSettled([
- removeDomainFromVercel(domain),
-
- prisma.domain.update({
- where: {
- slug: domain,
- },
- data: {
- projectId: null,
- },
- }),
- ]);
-
- await queueDomainDeletion({
- domain,
+ await prisma.domain.update({
+ where: {
+ slug: domain,
+ },
+ data: {
+ projectId: null,
+ },
});
- response.forEach((promise) => {
- if (promise.status === "rejected") {
- console.error("markDomainAsDeleted", {
- reason: promise.reason,
- domain,
- });
- }
- });
+ await domainDeletedJob.dispatch(
+ {
+ domain,
+ },
+ {
+ label: domain,
+ },
+ );
}
diff --git a/apps/web/lib/api/domains/queue-domain-update.ts b/apps/web/lib/api/domains/queue-domain-update.ts
index 37dc1670d3..9a1215aa57 100644
--- a/apps/web/lib/api/domains/queue-domain-update.ts
+++ b/apps/web/lib/api/domains/queue-domain-update.ts
@@ -32,19 +32,3 @@ export async function queueDomainUpdate({
},
});
}
-
-export async function queueDomainDeletion({
- domain,
- delay,
-}: {
- domain: string;
- delay?: number;
-}) {
- return await qstash.publishJSON({
- url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/delete`,
- ...(delay && { delay }),
- body: {
- domain,
- },
- });
-}
diff --git a/apps/web/lib/jobs/handlers/domain-deleted-job.ts b/apps/web/lib/jobs/handlers/domain-deleted-job.ts
new file mode 100644
index 0000000000..0fa920e81d
--- /dev/null
+++ b/apps/web/lib/jobs/handlers/domain-deleted-job.ts
@@ -0,0 +1,163 @@
+import { removeDomainFromVercel } from "@/lib/api/domains/remove-domain-vercel";
+import { R2_URL } from "@dub/utils";
+import * as z from "zod/v4";
+import { linkCache } from "../../api/links/cache";
+import { includeProgramEnrollment } from "../../api/links/include-program-enrollment";
+import { includeTags } from "../../api/links/include-tags";
+import { CRON_BATCH_SIZE } from "../../cron";
+import { limiter } from "../../cron/limiter";
+import { prisma } from "../../prisma";
+import { storage } from "../../storage";
+import { recordLink } from "../../tinybird";
+import { defineJob } from "../index";
+
+const inputSchema = z.object({
+ domain: z.string(),
+});
+
+// Job to delete a domain and all its links
+export const domainDeletedJob = defineJob({
+ name: "domain-deleted-job",
+ schema: inputSchema,
+ async handle(input) {
+ const { domain } = input;
+
+ const domainRecord = await prisma.domain.findUnique({
+ where: {
+ slug: domain,
+ },
+ select: {
+ projectId: true,
+ logo: true,
+ },
+ });
+
+ if (!domainRecord) {
+ console.error(
+ `[domainDeletedJob] Domain ${domain} not found. Skipping...`,
+ );
+ return;
+ }
+
+ if (domainRecord.projectId !== null) {
+ console.error(
+ `[domainDeletedJob] Domain ${domain} not marked for deletion. Skipping...`,
+ );
+ return;
+ }
+
+ const links = await prisma.link.findMany({
+ where: {
+ domain,
+ },
+ include: {
+ ...includeTags,
+ ...includeProgramEnrollment,
+ },
+ take: CRON_BATCH_SIZE,
+ orderBy: {
+ createdAt: "desc",
+ },
+ });
+
+ console.log(`[domainDeletedJob] Found ${links.length} links to delete.`);
+
+ if (links.length > 0) {
+ const workspaceId = links[0].projectId;
+
+ const { count } = await prisma.$transaction(async (tx) => {
+ const result = await tx.link.deleteMany({
+ where: {
+ id: {
+ in: links.map((link) => link.id),
+ },
+ },
+ });
+
+ if (result.count === 0) {
+ return result;
+ }
+
+ // Update the workspace's total links count
+ if (workspaceId) {
+ await tx.project.update({
+ where: {
+ id: workspaceId,
+ },
+ data: {
+ totalLinks: {
+ decrement: result.count,
+ },
+ },
+ });
+ }
+
+ return result;
+ });
+
+ if (count > 0) {
+ await Promise.allSettled([
+ // Remove the link from Redis
+ linkCache.deleteMany(links),
+
+ // Record link in Tinybird
+ recordLink(links, { deleted: true }),
+
+ // Remove image from R2 storage if it exists
+ ...links
+ .filter((link) =>
+ link.image?.startsWith(`${R2_URL}/images/${link.id}`),
+ )
+ .map((link) =>
+ limiter.schedule(() =>
+ storage.delete({
+ key: link.image!.replace(`${R2_URL}/`, ""),
+ }),
+ ),
+ ),
+ ]);
+ }
+
+ // More links remain — queue the next batch.
+ if (links.length === CRON_BATCH_SIZE) {
+ await domainDeletedJob.dispatch(
+ {
+ domain,
+ },
+ {
+ delay: 1,
+ label: domain,
+ },
+ );
+ return;
+ }
+ }
+
+ // No more links left – delete the domain
+ await prisma.domain.delete({
+ where: {
+ slug: domain,
+ },
+ });
+
+ // side effects: remove from Vercel, and delete the logo (if it exists)
+ const response = await Promise.allSettled([
+ removeDomainFromVercel(domain),
+ domainRecord.logo &&
+ storage.delete({
+ key: domainRecord.logo.replace(`${R2_URL}/`, ""),
+ }),
+ ]);
+
+ response.forEach((promise) => {
+ if (promise.status === "rejected") {
+ console.error("domainDeletedJob", {
+ reason: promise.reason,
+ domain,
+ });
+ }
+ });
+
+ console.log(`[domainDeletedJob] Domain "${domain}" fully deleted.`);
+ },
+});
diff --git a/apps/web/lib/jobs/registry.ts b/apps/web/lib/jobs/registry.ts
index f916e4073c..53098226cb 100644
--- a/apps/web/lib/jobs/registry.ts
+++ b/apps/web/lib/jobs/registry.ts
@@ -17,6 +17,9 @@ const jobLoaders = {
"link-tag-deleted-job": () =>
import("./handlers/link-tag-deleted-job").then((m) => m.linkTagDeletedJob),
+ "domain-deleted-job": () =>
+ import("./handlers/domain-deleted-job").then((m) => m.domainDeletedJob),
+
"default-link-deleted-job": () =>
import("./handlers/default-link-deleted-job").then(
(m) => m.defaultLinkDeletedJob,