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.

update cron approach

Steven Teya3c62cf17bf5
diff --git a/apps/web/app/(ee)/api/cron/link-retention-cleanup/route.ts b/apps/web/app/(ee)/api/cron/link-retention-cleanup/route.ts
index 3ee55db328..4ffe8bd9d5 100644
--- a/apps/web/app/(ee)/api/cron/link-retention-cleanup/route.ts
+++ b/apps/web/app/(ee)/api/cron/link-retention-cleanup/route.ts
@@ -2,64 +2,35 @@ import { handleAndReturnErrorResponse } from "@/lib/api/errors";
import { verifyVercelSignature } from "@/lib/cron/verify-vercel";
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
+// Runs every minute (* * * * *)
// GET /api/cron/link-retention-cleanup
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",
+ const domains = await prisma.domain.findMany({
+ where: {
+ linkRetentionDays: {
+ not: null,
},
- 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 && domain.linkRetentionDays > 0)
- await deleteOldLinks(domain.slug, domain.linkRetentionDays);
- }
-
- ++processedBatches;
- cursor = domains[domains.length - 1].id;
-
- console.log(
- `[Delete old links] Deleted old links for ${domains.length} ${pluralize("domain", domains.length)}`,
- );
+ },
+ select: {
+ id: true,
+ slug: true,
+ linkRetentionDays: true,
+ },
+ });
- // Pause for 2 seconds before the next batch of domains
- await new Promise((resolve) => setTimeout(resolve, 2000));
- }
+ await Promise.all(
+ domains.map((domain) =>
+ deleteOldLinks(domain.slug, domain.linkRetentionDays!),
+ ),
+ );
return NextResponse.json("OK");
} catch (error) {
@@ -67,8 +38,8 @@ export async function GET(req: Request) {
}
}
-const LINKS_PER_BATCH = 200;
-const MAX_LINK_BATCHES = 100;
+const LINKS_PER_BATCH = 100;
+const MAX_LINK_BATCHES = 10;
async function deleteOldLinks(domain: string, linkRetentionDays: number) {
let processedBatches = 0;
@@ -77,17 +48,13 @@ async function deleteOldLinks(domain: string, linkRetentionDays: number) {
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",
+ createdAt: "asc",
},
take: LINKS_PER_BATCH,
});
@@ -95,50 +62,29 @@ async function deleteOldLinks(domain: string, linkRetentionDays: number) {
if (links.length === 0) break;
console.log(
- `[Delete old links] Deleting ${links.length} links for ${domain} (batch ${processedBatches + 1})...`,
+ `[Link retention cleanup] Deleting ${links.length} links for ${domain} (batch ${processedBatches + 1})...`,
);
- const [recordedLinks, deletedLinks] = await Promise.allSettled([
- // 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(({ id }) => id),
- },
- },
- }),
- ]);
- if (recordedLinks.status !== "fulfilled") {
- console.error(
- `[Delete old links] Failed to record links deletion for ${domain}: ${recordedLinks.reason}`,
- );
- }
-
- if (deletedLinks.status !== "fulfilled") {
- console.error(
- `[Delete old links] Failed to delete links for ${domain}: ${deletedLinks.reason}`,
- );
- }
+ await prisma.link.deleteMany({
+ where: {
+ id: {
+ in: links.map(({ id }) => id),
+ },
+ },
+ });
+ // 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?
+ await recordLinkTB(
+ links.map((link) => ({
+ ...transformLinkTB(link),
+ deleted: true,
+ })),
+ );
- if (
- deletedLinks.status === "fulfilled" &&
- recordedLinks.status === "fulfilled"
- ) {
- console.log(
- `[Delete old links] Deleted ${deletedLinks.value.count} links for ${domain}!`,
- );
- }
+ console.log(
+ `[Link retention cleanup] Deleted ${links.length} links for ${domain}!`,
+ );
++processedBatches;
-
- // Pause for 2 seconds before the next batch of links
- await new Promise((resolve) => setTimeout(resolve, 2000));
}
}
diff --git a/apps/web/vercel.json b/apps/web/vercel.json
index b15a7b1a2a..af212e4fbd 100644
--- a/apps/web/vercel.json
+++ b/apps/web/vercel.json
@@ -50,7 +50,7 @@
},
{
"path": "/api/cron/link-retention-cleanup",
- "schedule": "0 0 * * *"
+ "schedule": "* * * * *"
}
],
"functions": {