The modern link attribution platform. Loved by world-class engineering teams at Twilio, Zillow, Jobber, Vercel, Raycast, and more.
TypeScript clickhousemysqlnextjsopen-sourceplanetscaleprismatailwindcsstinybirdtypescriptupstashvercel
Add cron
Tim Wilson856c0bddc10c
diff --git a/apps/web/app/(ee)/api/cron/links/delete-old-links/route.ts b/apps/web/app/(ee)/api/cron/links/delete-old-links/route.tsnew file mode 100644index 0000000000..e7ed0392a3--- /dev/null+++ b/apps/web/app/(ee)/api/cron/links/delete-old-links/route.ts@@ -0,0 +1,133 @@+import { handleAndReturnErrorResponse } from "@/lib/api/errors";+import { recordLinkTB, transformLinkTB } from "@/lib/tinybird";+import { prisma } from "@dub/prisma";+import { pluralize } from "@dub/utils";+import { NextResponse } from "next/server";++export const dynamic = "force-dynamic";++const DOMAINS_PER_BATCH = 10;+const MAX_DOMAIN_BATCHES = 1_000;++// This route is used to delete old links for domains with linkRetentionDays set+// GET /api/cron/links/delete-old-links+export async function GET(req: Request) {+ try {+ // await verifyVercelSignature(req);++ let processedBatches = 0;+ let cursor: string | null = null;++ while (processedBatches < MAX_DOMAIN_BATCHES) {+ const domains = await prisma.domain.findMany({+ select: {+ id: true,+ slug: true,+ linkRetentionDays: true,+ },+ where: {+ linkRetentionDays: {+ not: null,+ },+ },+ orderBy: {+ id: "asc",+ },+ take: DOMAINS_PER_BATCH,+ skip: cursor ? 1 : 0,+ ...(cursor && {+ cursor: {+ id: cursor,+ },+ }),+ });++ if (domains.length === 0) break;++ for (const domain of domains) {+ if (domain.linkRetentionDays)+ await deleteOldLinks(domain.slug, domain.linkRetentionDays);+ }++ // Pause for 2 seconds before the next batch+ await new Promise((resolve) => setTimeout(resolve, 2000));++ ++processedBatches;+ cursor = domains[domains.length - 1].id;++ console.log(+ `[Delete old links] Deleted old links for ${domains.length} ${pluralize("domain", domains.length)}`,+ );+ }++ return NextResponse.json("OK");+ } catch (error) {+ return handleAndReturnErrorResponse(error);+ }+}++const LINKS_PER_BATCH = 100;+const MAX_LINK_BATCHES = 100;++async function deleteOldLinks(domain: string, linkRetentionDays: number) {+ let processedBatches = 0;+ let cursor: string | null = null;++ while (processedBatches < MAX_LINK_BATCHES) {+ const links = await prisma.link.findMany({+ where: {+ domain,+ key: {+ not: "_root",+ },+ archived: false,+ createdAt: {+ lt: new Date(Date.now() - 1000 * 60 * 60 * 24 * linkRetentionDays),+ },+ linkRetentionCleanupDisabledAt: null,+ },+ orderBy: {+ id: "asc",+ },+ take: LINKS_PER_BATCH,+ skip: cursor ? 1 : 0,+ ...(cursor && {+ cursor: {+ id: cursor,+ },+ }),+ });++ if (links.length === 0) break;++ console.log(+ `[Delete old links] Deleting ${links.length} links for ${domain}...`,+ );+ const [_, deletedLinks] = await Promise.all([+ // Record the links deletion in Tinybird+ // not 100% sure if we need this yet, maybe we should just delete the link completely from TB to save space?+ recordLinkTB(+ links.map((link) => ({+ ...transformLinkTB(link),+ deleted: true,+ })),+ ),+ prisma.link.deleteMany({+ where: {+ id: {+ in: links.map((link) => link.id),+ },+ },+ }),+ ]);+ console.log(+ `[Delete old links] Deleted ${deletedLinks.count} links for ${domain}!`,+ );++ // Pause for 2 seconds before the next batch+ await new Promise((resolve) => setTimeout(resolve, 2000));++ ++processedBatches;+ cursor = links[links.length - 1].id;+ }+}diff --git a/apps/web/vercel.json b/apps/web/vercel.jsonindex f868647fb4..7fad4bc2f2 100644--- a/apps/web/vercel.json+++ b/apps/web/vercel.json@@ -47,6 +47,10 @@{"path": "/api/cron/online-presence/youtube","schedule": "0 0 * * *"+ },+ {+ "path": "/api/cron/links/delete-old-links",+ "schedule": "0 0 * * *"}],"functions": {diff --git a/packages/prisma/schema/link.prisma b/packages/prisma/schema/link.prismaindex eac54cff26..b407e34b37 100644--- a/packages/prisma/schema/link.prisma+++ b/packages/prisma/schema/link.prisma@@ -26,7 +26,7 @@ model Link {rewrite Boolean @default(false) // whether to rewrite the link or not- evergreen Boolean? // true means it's evergreen (for domain.linkRetentionDays); default is null (not applicable)+ linkRetentionCleanupDisabledAt DateTime? // When link retention cleanup (see domain.linkRetentionDays) was explicitly disableddoIndex Boolean @default(false) // we don't index short links by default// Custom device targeting