Skip to content

armgit / Cap

Public Imported from GitHub
0

Open source Loom alternative. Beautiful, shareable screen recordings.

perf(api): batch developer storage cron with Promise.allSettled

Richie McIlroy26f874172427
diff --git a/apps/web/app/api/cron/developer-storage/route.ts b/apps/web/app/api/cron/developer-storage/route.ts
index 89a19fd69..3e8fdb0f0 100644
--- a/apps/web/app/api/cron/developer-storage/route.ts
+++ b/apps/web/app/api/cron/developer-storage/route.ts
@@ -85,103 +85,115 @@ export async function GET(request: Request) {
const accountsByApp = new Map(accounts.map((a) => [a.appId, a]));
- let processed = 0;
-
- for (const app of apps) {
+ const appsToProcess = apps.filter((app) => {
const existing = snapshotsByApp.get(app.id);
- if (existing?.processedAt) continue;
+ if (existing?.processedAt) return false;
const stats = statsByApp.get(app.id);
const totalMinutes = stats?.totalDurationMinutes ?? 0;
- const videoCount = Number(stats?.videoCount ?? 0);
-
- if (totalMinutes <= 0) continue;
+ if (totalMinutes <= 0) return false;
const microCreditsToCharge = Math.floor(
totalMinutes * MICRO_CREDITS_PER_MINUTE_PER_DAY,
);
-
- if (microCreditsToCharge <= 0) continue;
+ if (microCreditsToCharge <= 0) return false;
const account = accountsByApp.get(app.id);
- if (!account) continue;
-
- await db().transaction(async (tx) => {
- const [current] = await tx
- .select({
- balanceMicroCredits: developerCreditAccounts.balanceMicroCredits,
- })
- .from(developerCreditAccounts)
- .where(eq(developerCreditAccounts.id, account.id))
- .limit(1);
-
- if (!current || current.balanceMicroCredits < microCreditsToCharge) {
- return;
- }
-
- await tx
- .update(developerCreditAccounts)
- .set({
- balanceMicroCredits: sql`${developerCreditAccounts.balanceMicroCredits} - ${microCreditsToCharge}`,
- })
- .where(
- and(
- eq(developerCreditAccounts.id, account.id),
- sql`${developerCreditAccounts.balanceMicroCredits} >= ${microCreditsToCharge}`,
- ),
- );
+ return !!account;
+ });
- const [updated] = await tx
- .select({
- balanceMicroCredits: developerCreditAccounts.balanceMicroCredits,
- })
- .from(developerCreditAccounts)
- .where(eq(developerCreditAccounts.id, account.id))
- .limit(1);
+ const BATCH_SIZE = 10;
+ let processed = 0;
- if (updated.balanceMicroCredits === current.balanceMicroCredits) {
- return;
- }
+ for (let i = 0; i < appsToProcess.length; i += BATCH_SIZE) {
+ const batch = appsToProcess.slice(i, i + BATCH_SIZE);
+ const results = await Promise.allSettled(
+ batch.map(async (app) => {
+ const existing = snapshotsByApp.get(app.id);
+ const stats = statsByApp.get(app.id);
+ const totalMinutes = stats?.totalDurationMinutes ?? 0;
+ const videoCount = Number(stats?.videoCount ?? 0);
+ const microCreditsToCharge = Math.floor(
+ totalMinutes * MICRO_CREDITS_PER_MINUTE_PER_DAY,
+ );
+ const account = accountsByApp.get(app.id);
+ if (!account) return;
+
+ await db().transaction(async (tx) => {
+ await tx
+ .update(developerCreditAccounts)
+ .set({
+ balanceMicroCredits: sql`${developerCreditAccounts.balanceMicroCredits} - ${microCreditsToCharge}`,
+ })
+ .where(
+ and(
+ eq(developerCreditAccounts.id, account.id),
+ sql`${developerCreditAccounts.balanceMicroCredits} >= ${microCreditsToCharge}`,
+ ),
+ );
+
+ const [updated] = await tx
+ .select({
+ balanceMicroCredits: developerCreditAccounts.balanceMicroCredits,
+ })
+ .from(developerCreditAccounts)
+ .where(eq(developerCreditAccounts.id, account.id))
+ .limit(1);
+
+ if (
+ !updated ||
+ updated.balanceMicroCredits === account.balanceMicroCredits
+ ) {
+ return;
+ }
+
+ await tx.insert(developerCreditTransactions).values({
+ id: nanoId(),
+ accountId: account.id,
+ type: "storage_daily",
+ amountMicroCredits: -microCreditsToCharge,
+ balanceAfterMicroCredits: updated.balanceMicroCredits,
+ referenceType: "manual",
+ metadata: {
+ snapshotDate: today,
+ totalDurationMinutes: totalMinutes,
+ videoCount,
+ },
+ });
+
+ const snapshotId = existing?.id ?? nanoId();
+ if (existing) {
+ await tx
+ .update(developerDailyStorageSnapshots)
+ .set({
+ totalDurationMinutes: totalMinutes,
+ videoCount,
+ microCreditsCharged: microCreditsToCharge,
+ processedAt: new Date(),
+ })
+ .where(eq(developerDailyStorageSnapshots.id, snapshotId));
+ } else {
+ await tx.insert(developerDailyStorageSnapshots).values({
+ id: snapshotId,
+ appId: app.id,
+ snapshotDate: today,
+ totalDurationMinutes: totalMinutes,
+ videoCount,
+ microCreditsCharged: microCreditsToCharge,
+ processedAt: new Date(),
+ });
+ }
+ });
+ }),
+ );
- await tx.insert(developerCreditTransactions).values({
- id: nanoId(),
- accountId: account.id,
- type: "storage_daily",
- amountMicroCredits: -microCreditsToCharge,
- balanceAfterMicroCredits: updated.balanceMicroCredits,
- referenceType: "manual",
- metadata: {
- snapshotDate: today,
- totalDurationMinutes: totalMinutes,
- videoCount,
- },
- });
-
- const snapshotId = existing?.id ?? nanoId();
- if (existing) {
- await tx
- .update(developerDailyStorageSnapshots)
- .set({
- totalDurationMinutes: totalMinutes,
- videoCount,
- microCreditsCharged: microCreditsToCharge,
- processedAt: new Date(),
- })
- .where(eq(developerDailyStorageSnapshots.id, snapshotId));
+ for (const result of results) {
+ if (result.status === "fulfilled") {
+ processed++;
} else {
- await tx.insert(developerDailyStorageSnapshots).values({
- id: snapshotId,
- appId: app.id,
- snapshotDate: today,
- totalDurationMinutes: totalMinutes,
- videoCount,
- microCreditsCharged: microCreditsToCharge,
- processedAt: new Date(),
- });
+ console.error("Failed to process app in cron:", result.reason);
}
- });
-
- processed++;
+ }
}
return NextResponse.json({