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.

use cron job

Kiran Kd8feceee95ed
diff --git a/apps/web/app/api/cron/domains/delete/route.ts b/apps/web/app/api/cron/domains/delete/route.ts
new file mode 100644
index 0000000000..ccb53c2326
--- /dev/null
+++ b/apps/web/app/api/cron/domains/delete/route.ts
@@ -0,0 +1,55 @@
+import { handleAndReturnErrorResponse } from "@/lib/api/errors";
+import { qstash } from "@/lib/cron";
+import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
+import { redis } from "@/lib/upstash";
+import z from "@/lib/zod";
+import { APP_DOMAIN_WITH_NGROK } from "@dub/utils";
+
+export const dynamic = "force-dynamic";
+
+const schema = z.object({
+ domain: z.string(),
+ cursor: z.any().default(0),
+});
+
+const BATCH_SIZE = 500;
+
+// Delete the links from Redis for a domain
+export async function POST(req: Request) {
+ try {
+ const body = await req.json();
+ await verifyQstashSignature(req, body);
+
+ const { domain, cursor } = schema.parse(body);
+
+ // Scan the database for keys
+ const [newCursor, keys] = await redis.scan(cursor, {
+ match: `${domain}*`,
+ count: BATCH_SIZE,
+ });
+
+ // Remove keys from Redis
+ if (keys.length > 0) {
+ const pipeline = redis.pipeline();
+ keys.map((key) => pipeline.del(key));
+ await pipeline.exec();
+ }
+
+ if (newCursor === 0) {
+ return new Response("No keys to delete. Skipping...");
+ }
+
+ // Schedule next job
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/delete`,
+ body: {
+ domain,
+ cursor: String(newCursor),
+ },
+ });
+
+ return new Response("Ok");
+ } catch (error) {
+ return handleAndReturnErrorResponse(error);
+ }
+}
diff --git a/apps/web/lib/api/domains/delete-domain-links.ts b/apps/web/lib/api/domains/delete-domain-links.ts
index 775fafca69..caf1807384 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 { R2_URL } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, R2_URL } from "@dub/utils";
import { waitUntil } from "@vercel/functions";
import { storage } from "../../storage";
import { recordLink } from "../../tinybird";
-import { linkCache } from "../links/cache";
import { removeDomainFromVercel } from "./remove-domain-vercel";
/* Delete a domain and all links & images associated with it */
@@ -50,7 +50,12 @@ export async function deleteDomainAndLinks(domain: string) {
(async () => {
await Promise.allSettled([
// delete all links from redis
- linkCache.deleteMany(allLinks),
+ qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/domains/delete`,
+ body: {
+ domain,
+ },
+ }),
// record deletes in tinybird for domain & links
recordLink([