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 usage cron, fix #466

Steven Teyf72b2fc714fe
diff --git a/apps/web/app/api/cron/domains/route.ts b/apps/web/app/api/cron/domains/route.ts
index b5558525a9..c79b4d6dae 100644
--- a/apps/web/app/api/cron/domains/route.ts
+++ b/apps/web/app/api/cron/domains/route.ts
@@ -3,7 +3,7 @@ import {
getDomainResponse,
verifyDomain,
} from "@/lib/api/domains";
-import { receiver, verifySignature } from "@/lib/cron";
+import { verifySignature } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { log } from "@dub/utils";
import { NextResponse } from "next/server";
@@ -30,12 +30,13 @@ export async function GET(req: Request) {
// exclude domains that belong to us
notIn: [
"dub.sh",
+ "chatg.pt",
+ "amzn.id",
+ "spti.fi",
"stey.me",
"steven.yt",
- "vercel.fyi",
- "vercel.link",
+ "steven.blue",
"owd.li",
- "chatg.pt",
"elegance.ai",
],
},
diff --git a/apps/web/app/api/cron/domains/utils.ts b/apps/web/app/api/cron/domains/utils.ts
index 63e2cb37b9..1376dcf733 100644
--- a/apps/web/app/api/cron/domains/utils.ts
+++ b/apps/web/app/api/cron/domains/utils.ts
@@ -161,7 +161,7 @@ export const handleDomainUpdates = async ({
if (invalidDays >= 28) {
const sentSecondDomainInvalidEmail = sentEmails.includes(
- "secondDomainInvalidEmail",
+ `secondDomainInvalidEmail:${domain}`,
);
if (!sentSecondDomainInvalidEmail) {
return sendDomainInvalidEmail({
@@ -176,7 +176,7 @@ export const handleDomainUpdates = async ({
if (invalidDays >= 14) {
const sentFirstDomainInvalidEmail = sentEmails.includes(
- "firstDomainInvalidEmail",
+ `firstDomainInvalidEmail:${domain}`,
);
if (!sentFirstDomainInvalidEmail) {
return sendDomainInvalidEmail({
@@ -230,7 +230,7 @@ const sendDomainInvalidEmail = async ({
slug: projectSlug,
},
},
- type: `${type}DomainInvalidEmail`,
+ type: `${type}DomainInvalidEmail:${domain}`,
},
}),
]);
diff --git a/apps/web/app/api/cron/usage/route.ts b/apps/web/app/api/cron/usage/route.ts
index 8701fae5a4..4829eab66c 100644
--- a/apps/web/app/api/cron/usage/route.ts
+++ b/apps/web/app/api/cron/usage/route.ts
@@ -1,5 +1,5 @@
-import { receiver, verifySignature } from "@/lib/cron";
-import { log } from "@dub/utils";
+import { verifySignature } from "@/lib/cron";
+import { getSearchParams, log } from "@dub/utils";
import { NextResponse } from "next/server";
import { updateUsage } from "./utils";
@@ -12,9 +12,14 @@ export async function GET(req: Request) {
return new Response("Unauthorized", { status: 401 });
}
+ const { skip } = getSearchParams(req.url);
+
try {
- const results = await updateUsage();
- return NextResponse.json(results);
+ await updateUsage(skip ? parseInt(skip) : undefined);
+
+ return NextResponse.json({
+ response: "success",
+ });
} catch (error) {
await log({
message: "Usage cron failed. Error: " + error.message,
diff --git a/apps/web/app/api/cron/usage/utils.ts b/apps/web/app/api/cron/usage/utils.ts
index b0bec915f1..41f15c93a8 100644
--- a/apps/web/app/api/cron/usage/utils.ts
+++ b/apps/web/app/api/cron/usage/utils.ts
@@ -1,13 +1,20 @@
-import { limiter } from "@/lib/cron";
+import { limiter, qstash } from "@/lib/cron";
import prisma from "@/lib/prisma";
import { getStats } from "@/lib/stats";
import { ProjectProps } from "@/lib/types";
-import { getAdjustedBillingCycleStart, linkConstructor, log } from "@dub/utils";
+import {
+ APP_DOMAIN_WITH_NGROK,
+ getAdjustedBillingCycleStart,
+ linkConstructor,
+ log,
+} from "@dub/utils";
import { sendEmail } from "emails";
import ClicksSummary from "emails/clicks-summary";
import UsageExceeded from "emails/usage-exceeded";
-export const updateUsage = async () => {
+const limit = 250;
+
+export const updateUsage = async (skip?: number) => {
const projects = await prisma.project.findMany({
where: {
domains: {
@@ -37,8 +44,15 @@ export const updateUsage = async () => {
sentEmails: true,
createdAt: true,
},
+ skip: skip || 0,
+ take: limit,
});
+ // if no projects left, meaning cron is complete
+ if (projects.length === 0) {
+ return;
+ }
+
// Reset billing cycles for projects that:
// - Are not on the free plan
// - Are on the free plan but have not exceeded usage
@@ -56,7 +70,7 @@ export const updateUsage = async () => {
);
// Send email to notify overages
- const notifyOveragesResponse = await Promise.allSettled(
+ await Promise.allSettled(
exceedingUsage.map(async (project) => {
const { name, usage, usageLimit, users, sentEmails } = project;
const emails = users.map((user) => user.user.email) as string[];
@@ -95,7 +109,7 @@ export const updateUsage = async () => {
// Reset usage for projects that have billingCycleStart today
// also delete sentEmails for those projects
- const resetBillingResponse = await Promise.allSettled(
+ await Promise.allSettled(
billingReset.map(async (project) => {
// Only send the 30-day summary email if the project was created more than 30 days ago
if (
@@ -186,12 +200,12 @@ export const updateUsage = async () => {
}),
);
- return {
- billingReset,
- exceedingUsage,
- notifyOveragesResponse,
- resetBillingResponse,
- };
+ return await qstash.publishJSON({
+ url: `${APP_DOMAIN_WITH_NGROK}/api/cron/usage?skip=${
+ skip ? skip + limit : limit
+ }`,
+ method: "GET",
+ });
};
const sendUsageLimitEmail = async (
diff --git a/apps/web/prisma/schema.prisma b/apps/web/prisma/schema.prisma
index c8e1256f0a..c7d205cd14 100644
--- a/apps/web/prisma/schema.prisma
+++ b/apps/web/prisma/schema.prisma
@@ -134,16 +134,9 @@ model ProjectUsers {
@@index([projectId])
}
-enum EmailType {
- firstDomainInvalidEmail
- secondDomainInvalidEmail
- firstUsageLimitEmail
- secondUsageLimitEmail
-}
-
model SentEmail {
id String @id @default(cuid())
- type EmailType
+ type String
createdAt DateTime @default(now())
project Project? @relation(fields: [projectId], references: [id], onDelete: Cascade)
projectId String?