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.

Add qstash and skip functionality to links usage cron route

Kiran K3c6cf642222b
diff --git a/apps/web/app/api/cron/links/usage/route.ts b/apps/web/app/api/cron/links/usage/route.ts
index 6e7f0dfc50..843c60ea07 100644
--- a/apps/web/app/api/cron/links/usage/route.ts
+++ b/apps/web/app/api/cron/links/usage/route.ts
@@ -1,12 +1,14 @@
-import { sendLimitEmail, verifySignature } from "@/lib/cron";
-import { log } from "@dub/utils";
+import { sendLimitEmail, verifySignature, qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { WorkspaceProps } from "@/lib/types";
import { Project } from "@prisma/client";
+import { APP_DOMAIN_WITH_NGROK, getSearchParams, log } from "@dub/utils";
// Cron to update the links usage of each workspace.
// Runs once every 5 mins (*/5 * * * *)
+const limit = 30;
+
export async function GET(req: Request) {
const validSignature = await verifySignature(req);
@@ -14,6 +16,8 @@ export async function GET(req: Request) {
return new Response("Unauthorized", { status: 401 });
}
+ const { skip } = getSearchParams(req.url);
+
const workspaces = await prisma.project.findMany({
select: {
id: true,
@@ -23,26 +27,25 @@ export async function GET(req: Request) {
linksUsage: true,
linksLimit: true,
},
- orderBy: {
- usageLastChecked: "asc",
- },
- take: 30,
+ take: limit,
+ skip: skip ? parseInt(skip) : 0,
});
+ if (workspaces.length === 0) {
+ return new Response("OK");
+ }
+
try {
- await Promise.allSettled([
- prisma.project.updateMany({
- where: {
- id: {
- in: workspaces.map(({ id }) => id),
- },
- },
- data: {
- usageLastChecked: new Date(),
- },
- }),
- ...workspaces.map((workspace) => sendUsageEmail(workspace)),
- ]);
+ await Promise.allSettled(
+ workspaces.map((workspace) => sendUsageEmail(workspace)),
+ );
+
+ await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/links/usage?skip=${
+ skip ? parseInt(skip) + limit : limit
+ }`,
+ method: "GET",
+ });
return new Response("OK");
} catch (error) {
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index b90ff560a7..e8cdd1b49e 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -109,11 +109,8 @@ model Project {
createdAt DateTime @default(now())
updatedAt DateTime @updatedAt
- usageLastChecked DateTime @default(now())
inviteCode String? @unique
defaultDomains DefaultDomains[]
-
- @@index(usageLastChecked(sort: Asc))
}
model ProjectInvite {