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.

address CR feedback

Kiran Kfe5888d2636e
diff --git a/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/process/route.ts b/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/process/route.ts
index 7d306546a0..ab68bf6b07 100644
--- a/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/process/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/aggregate-due-commissions/process/route.ts
@@ -1,5 +1,6 @@
import { trackCommissionStatusUpdate } from "@/lib/api/commissions/track-commission-update-activity-log";
import { createId } from "@/lib/api/create-id";
+import { MUTABLE_PAYOUT_STATUSES } from "@/lib/constants/payouts";
import { withCron } from "@/lib/cron/with-cron";
import { prisma } from "@/lib/prisma";
import { chunk } from "@dub/utils";
@@ -266,16 +267,20 @@ async function aggregateDueCommissionsForPartner({
// Prisma has a reported MySQL issue where updateMany may drop WHERE predicates
// during the UPDATE, allowing concurrent workers to claim the same commissions.
// See: https://github.com/prisma/prisma/issues/28840
+ // Also join Payout so we never attach to a payout that left a mutable status
+ // (e.g. confirmed to processing between prefetch and claim).
const updatedCommissions = await prisma.$executeRaw`
- UPDATE Commission
+ UPDATE Commission c
+ INNER JOIN Payout p ON p.id = ${payoutToUse.id}
SET
- status = ${CommissionStatus.processed},
- payoutId = ${payoutToUse.id},
- updatedAt = NOW()
- WHERE id IN (${Prisma.join(commissionIds)})
- AND programId = ${program.id}
- AND partnerId = ${partnerId}
- AND status = ${CommissionStatus.pending}
+ c.status = ${CommissionStatus.processed},
+ c.payoutId = ${payoutToUse.id},
+ c.updatedAt = NOW()
+ WHERE c.id IN (${Prisma.join(commissionIds)})
+ AND c.programId = ${program.id}
+ AND c.partnerId = ${partnerId}
+ AND c.status = ${CommissionStatus.pending}
+ AND p.status IN (${Prisma.join(MUTABLE_PAYOUT_STATUSES)})
`;
if (updatedCommissions === 0) {
@@ -283,7 +288,7 @@ async function aggregateDueCommissionsForPartner({
`No commissions were updated for partner ${partnerId}. Skipping...`,
);
- // Another worker won the race — only delete if still empty.
+ // Lost race (claim or payout no longer mutable) — only delete if still empty.
if (!isReusingPendingPayout) {
await prisma.payout.deleteMany({
where: {
@@ -311,15 +316,23 @@ async function aggregateDueCommissionsForPartner({
},
});
- await prisma.payout.update({
- where: {
- id: payoutToUse.id,
- },
- data: {
- amount: totalEarningsForPayout ?? 0,
- ...(isReusingPendingPayout ? { periodEnd } : {}),
- },
- });
+ // Raw SQL: Prisma updateMany also drops status predicates on MySQL.
+ const updatedPayout = await prisma.$executeRaw`
+ UPDATE Payout
+ SET
+ amount = ${totalEarningsForPayout ?? 0},
+ periodEnd = COALESCE(${isReusingPendingPayout ? periodEnd : null}, periodEnd),
+ updatedAt = NOW()
+ WHERE id = ${payoutToUse.id}
+ AND status IN (${Prisma.join(MUTABLE_PAYOUT_STATUSES)})
+ `;
+
+ if (updatedPayout === 0) {
+ console.warn(
+ `Payout ${payoutToUse.id} is no longer mutable after claim for partner ${partnerId}. Skipping...`,
+ );
+ return false;
+ }
// Only activity-log commissions we actually claimed (handles partial races).
const claimedCommissions = await prisma.commission.findMany({
diff --git a/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts b/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts
index e835ee7af6..ce60bf989f 100644
--- a/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts
+++ b/apps/web/app/(ee)/api/cron/payouts/reconcile-amounts/route.ts
@@ -7,9 +7,11 @@ import { subMinutes } from "date-fns";
import { logAndRespond } from "../../utils";
export const dynamic = "force-dynamic";
+export const maxDuration = 60;
const BATCH_SIZE = 500;
const LOOKBACK_MINUTES = 2;
+const LOCK_TTL_SECONDS = 60;
// Finds recently updated pending payouts whose amount does not match
// SUM(commission.earnings) and reconciles them (update amount or delete empty payouts).
@@ -18,7 +20,7 @@ const LOOKBACK_MINUTES = 2;
export const GET = withCron(async () => {
const response = await withRedisLock({
key: "lock:payouts:reconcile-amounts",
- ttlSeconds: 60,
+ ttlSeconds: LOCK_TTL_SECONDS,
fn: async () => {
const payouts = await prisma.payout.findMany({
where: {
diff --git a/apps/web/lib/upstash/redis-lock.ts b/apps/web/lib/upstash/redis-lock.ts
index 62bf3f8865..999afd2832 100644
--- a/apps/web/lib/upstash/redis-lock.ts
+++ b/apps/web/lib/upstash/redis-lock.ts
@@ -1,9 +1,18 @@
import { redis } from "./redis";
+const RELEASE_LOCK_SCRIPT = `
+ if redis.call("get", KEYS[1]) == ARGV[1] then
+ return redis.call("del", KEYS[1])
+ else
+ return 0
+ end
+`;
+
/**
* Runs `fn` while holding a Redis NX lock. Returns `null` if the lock
- * could not be acquired (another holder is active). Always releases the
- * lock in `finally` when acquired.
+ * could not be acquired (another holder is active).
+ * Stores a unique token and releases via compare-and-delete so an expired
+ * lock acquired by another runner is never deleted by this run.
*/
export async function withRedisLock<T>({
key,
@@ -14,7 +23,8 @@ export async function withRedisLock<T>({
ttlSeconds: number;
fn: () => Promise<T>;
}): Promise<T | null> {
- const acquired = await redis.set(key, "1", {
+ const token = crypto.randomUUID();
+ const acquired = await redis.set(key, token, {
nx: true,
ex: ttlSeconds,
});
@@ -26,6 +36,7 @@ export async function withRedisLock<T>({
try {
return await fn();
} finally {
- await redis.del(key);
+ // Lua compare-and-delete: only the token owner may release the lock.
+ await redis.eval(RELEASE_LOCK_SCRIPT, [key], [token]);
}
}