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.

reduce batch size to 1000, process in chunks of 50

Steven Teyfc70a9073e4d
diff --git a/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/route.ts b/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/route.ts
index 5af27764f9..efd6905b19 100644
--- a/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/route.ts
@@ -4,12 +4,12 @@ import { qstash } from "@/lib/cron";
import { verifyQstashSignature } from "@/lib/cron/verify-qstash";
import { verifyVercelSignature } from "@/lib/cron/verify-vercel";
import { prisma } from "@dub/prisma";
-import { APP_DOMAIN_WITH_NGROK, log } from "@dub/utils";
+import { APP_DOMAIN_WITH_NGROK, chunk, log } from "@dub/utils";
import { logAndRespond } from "../../utils";
export const dynamic = "force-dynamic";
-const BATCH_SIZE = 10000;
+const BATCH_SIZE = 1000;
// This cron job aggregates due commissions (pending commissions that are past the program holding period) into payouts.
// Runs once every hour (0 * * * *) + calls itself recursively to look through all pending commissions available.
@@ -144,74 +144,77 @@ async function handler(req: Request) {
);
let totalProcessed = 0;
- for (const {
- partnerId,
- programId,
- commissions,
- } of partnerProgramCommissionsArray) {
- // sort the commissions by createdAt
- const sortedCommissions = commissions.sort(
- (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
- );
+ const chunks = chunk(partnerProgramCommissionsArray, 50);
+ for (let i = 0; i < chunks.length; i++) {
+ const chunk = chunks[i];
+ await Promise.allSettled(
+ chunk.map(async ({ partnerId, programId, commissions }) => {
+ // sort the commissions by createdAt
+ const sortedCommissions = commissions.sort(
+ (a, b) => a.createdAt.getTime() - b.createdAt.getTime(),
+ );
- // sum the earnings of the commissions
- const totalEarnings = sortedCommissions.reduce(
- (total, commission) => total + commission.earnings,
- 0,
- );
+ // sum the earnings of the commissions
+ const totalEarnings = sortedCommissions.reduce(
+ (total, commission) => total + commission.earnings,
+ 0,
+ );
- // earliest commission date
- const periodStart = sortedCommissions[0].createdAt;
+ // earliest commission date
+ const periodStart = sortedCommissions[0].createdAt;
- // last commission date
- const periodEnd =
- sortedCommissions[sortedCommissions.length - 1].createdAt;
+ // last commission date
+ const periodEnd =
+ sortedCommissions[sortedCommissions.length - 1].createdAt;
- let payoutToUse = existingPendingPayouts.find(
- (p) => p.partnerId === partnerId && p.programId === programId,
- );
+ let payoutToUse = existingPendingPayouts.find(
+ (p) => p.partnerId === partnerId && p.programId === programId,
+ );
- if (!payoutToUse) {
- payoutToUse = await prisma.payout.create({
- data: {
- id: createId({ prefix: "po_" }),
- programId,
- partnerId,
- periodStart,
- periodEnd,
- amount: totalEarnings,
- description: `Dub Partners payout (${programs.find((p) => p.id === programId)?.name})`,
- },
- });
- }
-
- // update the commissions to have the payoutId
- await prisma.commission.updateMany({
- where: {
- id: { in: commissions.map((c) => c.id) },
- },
- data: {
- status: "processed",
- payoutId: payoutToUse.id,
- },
- });
+ if (!payoutToUse) {
+ payoutToUse = await prisma.payout.create({
+ data: {
+ id: createId({ prefix: "po_" }),
+ programId,
+ partnerId,
+ periodStart,
+ periodEnd,
+ amount: totalEarnings,
+ description: `Dub Partners payout (${programs.find((p) => p.id === programId)?.name})`,
+ },
+ });
+ }
- // if we're reusing a pending payout, we need to update the amount and periodEnd
- if (existingPendingPayouts.find((p) => p.id === payoutToUse.id)) {
- await prisma.payout.update({
- where: {
- id: payoutToUse.id,
- },
- data: {
- amount: {
- increment: totalEarnings,
+ // update the commissions to have the payoutId
+ await prisma.commission.updateMany({
+ where: {
+ id: { in: commissions.map((c) => c.id) },
},
- periodEnd,
- },
- });
- }
+ data: {
+ status: "processed",
+ payoutId: payoutToUse.id,
+ },
+ });
- totalProcessed++;
+ // if we're reusing a pending payout, we need to update the amount and periodEnd
+ if (existingPendingPayouts.find((p) => p.id === payoutToUse.id)) {
+ await prisma.payout.update({
+ where: {
+ id: payoutToUse.id,
+ },
+ data: {
+ amount: {
+ increment: totalEarnings,
+ },
+ periodEnd,
+ },
+ });
+ }
+
+ totalProcessed++;
+ }),
+ );
+ console.log(`Processed chunk ${i + 1} of ${chunks.length}`);
}
const successRate =