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.

simplify usage cron

Steven Tey5c8d978723fc
diff --git a/apps/web/app/(ee)/api/cron/usage/route.ts b/apps/web/app/(ee)/api/cron/usage/route.ts
index 816a1df04a..4aa4cb21b2 100644
--- a/apps/web/app/(ee)/api/cron/usage/route.ts
+++ b/apps/web/app/(ee)/api/cron/usage/route.ts
@@ -1,40 +1,14 @@
-import { handleAndReturnErrorResponse } from "@/lib/api/errors";
-import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
-import { verifyVercelSignature } from "@/lib/cron/verify-vercel";
-import { log } from "@dub/utils";
-import { NextResponse } from "next/server";
+import { withCron } from "@/lib/cron/with-cron";
+import { logAndRespond } from "../utils";
import { updateUsage } from "./utils";
+export const dynamic = "force-dynamic";
+
/*
This route is used to update the usage stats of each workspace.
Runs once every day at noon UTC (0 12 * * *)
*/
-export const dynamic = "force-dynamic";
-
-async function handler(req: Request) {
- try {
- if (req.method === "GET") {
- await verifyVercelSignature(req);
- } else if (req.method === "POST") {
- await verifyQstashSignature({
- req,
- rawBody: await req.text(),
- });
- }
-
- await updateUsage();
-
- return NextResponse.json({
- response: "success",
- });
- } catch (error) {
- await log({
- message: `Error updating usage: ${error.message}`,
- type: "cron",
- });
-
- return handleAndReturnErrorResponse(error);
- }
-}
-
-export { handler as GET, handler as POST };
+export const POST = withCron(async () => {
+ const result = await updateUsage();
+ return logAndRespond(result);
+});
diff --git a/apps/web/app/(ee)/api/cron/usage/utils.ts b/apps/web/app/(ee)/api/cron/usage/utils.ts
index b880b62509..e06e3488b7 100644
--- a/apps/web/app/(ee)/api/cron/usage/utils.ts
+++ b/apps/web/app/(ee)/api/cron/usage/utils.ts
@@ -55,7 +55,7 @@ export const updateUsage = async () => {
// if no workspaces left, meaning cron is complete
if (workspaces.length === 0) {
- return;
+ return "No workspaces left to update";
}
// Reset billing cycles for workspaces that have
@@ -240,9 +240,11 @@ export const updateUsage = async () => {
}),
);
- return await qstash.publishJSON({
+ await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/usage`,
method: "POST",
body: {},
});
+
+ return `Updated usage stats for ${workspaces.length} workspaces`;
};