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.

Improve bounty notification batching mechanism (#3091)

Steven Tey362dad64f954
diff --git a/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts b/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
index 166ab07481..c29d3fab2d 100644
--- a/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
+++ b/apps/web/app/(ee)/api/cron/bounties/notify-partners/route.ts
@@ -15,10 +15,18 @@ export const dynamic = "force-dynamic";
const schema = z.object({
bountyId: z.string(),
- page: z.number().optional().default(0),
+ startingAfter: z.string().optional(),
+ batchNumber: z
+ .number()
+ .optional()
+ .default(1)
+ .describe("Keep track of the batches sent."),
});
-const MAX_PAGE_SIZE = 100;
+const EMAIL_BATCH_SIZE = 100; // Batch size
+const BATCH_DELAY_SECONDS = 2; // Delay between batches
+const EXTENDED_DELAY_SECONDS = 30; // Extended delay after 25 batches
+const EXTENDED_DELAY_INTERVAL = 25; // Number of batches after which to extend the delay
// POST /api/cron/bounties/notify-partners
// Send emails to eligible partners about new bounty that is published
@@ -31,7 +39,9 @@ export async function POST(req: Request) {
rawBody,
});
- const { bountyId, page } = schema.parse(JSON.parse(rawBody));
+ let { bountyId, startingAfter, batchNumber } = schema.parse(
+ JSON.parse(rawBody),
+ );
// Find bounty
const bounty = await prisma.bounty.findUnique({
@@ -40,7 +50,11 @@ export async function POST(req: Request) {
},
include: {
groups: true,
- program: true,
+ program: {
+ include: {
+ emailDomains: true,
+ },
+ },
},
});
@@ -96,11 +110,16 @@ export async function POST(req: Request) {
},
},
},
+ take: EMAIL_BATCH_SIZE,
+ skip: startingAfter ? 1 : 0,
+ ...(startingAfter && {
+ cursor: {
+ id: startingAfter,
+ },
+ }),
orderBy: {
- createdAt: "asc",
+ id: "asc",
},
- skip: page * MAX_PAGE_SIZE,
- take: MAX_PAGE_SIZE,
});
if (programEnrollments.length === 0) {
@@ -112,9 +131,17 @@ export async function POST(req: Request) {
console.log(
`Sending emails to ${programEnrollments.length} partners: ${programEnrollments.map(({ partner }) => partner.email).join(", ")}`,
);
+
+ const verifiedEmailDomain = bounty.program.emailDomains.find(
+ ({ status }) => status === "verified",
+ )?.slug;
+
const { data } = await sendBatchEmail(
programEnrollments.map(({ partner }) => ({
variant: "notifications",
+ from: verifiedEmailDomain
+ ? `${bounty.program.name} <bounties@${verifiedEmailDomain}>`
+ : undefined,
to: partner.email!, // coerce the type here because we've already filtered out partners with no email in the prisma query
subject: `New bounty available for ${bounty.program.name}`,
replyTo: bounty.program.supportEmail || "noreply",
@@ -134,7 +161,7 @@ export async function POST(req: Request) {
tags: [{ name: "type", value: "notification-email" }],
})),
{
- idempotencyKey: `bounty-notify/${bountyId}-page-${page}`,
+ idempotencyKey: `bounty-notify/${bountyId}-${startingAfter || "initial"}`,
},
);
@@ -152,17 +179,30 @@ export async function POST(req: Request) {
});
}
- if (programEnrollments.length === MAX_PAGE_SIZE) {
- const res = await qstash.publishJSON({
+ if (programEnrollments.length === EMAIL_BATCH_SIZE) {
+ startingAfter = programEnrollments[programEnrollments.length - 1].id;
+
+ // Add BATCH_DELAY_SECONDS pause between each batch, and a longer EXTENDED_DELAY_SECONDS cooldown after every EXTENDED_DELAY_INTERVAL batches.
+ let delay = 0;
+ if (batchNumber > 0 && batchNumber % EXTENDED_DELAY_INTERVAL === 0) {
+ delay = EXTENDED_DELAY_SECONDS;
+ } else {
+ delay = BATCH_DELAY_SECONDS;
+ }
+
+ await qstash.publishJSON({
url: `${APP_DOMAIN_WITH_NGROK}/api/cron/bounties/notify-partners`,
+ method: "POST",
+ delay,
body: {
bountyId,
- page: page + 1,
+ startingAfter,
+ batchNumber: batchNumber + 1,
},
});
return logAndRespond(
- `Enqueued next page (${page + 1}) for bounty ${bountyId}. ${JSON.stringify(res, null, 2)}`,
+ `Enqueued next batch (${startingAfter}) for bounty ${bountyId} to run after ${delay} seconds.`,
);
}
diff --git a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx
index a2fbd0b19f..37729741c1 100644
--- a/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx
+++ b/apps/web/app/app.dub.co/(dashboard)/[slug]/(ee)/program/campaigns/[campaignId]/transactional-campaign-logic.tsx
@@ -148,7 +148,13 @@ function ValueInput({
return (
<InlineBadgePopover
text={
- value ? (isCurrency ? currencyFormatter(value) : value) : "amount"
+ value
+ ? isCurrency
+ ? currencyFormatter(value, {
+ trailingZeroDisplay: "stripIfInteger",
+ })
+ : value
+ : "amount"
}
invalid={!value}
>
diff --git a/apps/web/tests/partners/resource.ts b/apps/web/tests/partners/resource.ts
index 8aa7a2e660..d1c53379e3 100644
--- a/apps/web/tests/partners/resource.ts
+++ b/apps/web/tests/partners/resource.ts
@@ -29,6 +29,7 @@ export const normalizedPartnerDateFields = z.object({
createdAt: z.string(),
bannedAt: z.string().nullish(),
payoutsEnabledAt: z.string().nullish(),
+ trustedAt: z.string().nullish(),
websiteVerifiedAt: z.string().nullish(),
youtubeVerifiedAt: z.string().nullish(),
twitterVerifiedAt: z.string().nullish(),
diff --git a/apps/web/ui/partners/bounties/bounty-logic.tsx b/apps/web/ui/partners/bounties/bounty-logic.tsx
index ff8b269cfd..c8b1b83569 100644
--- a/apps/web/ui/partners/bounties/bounty-logic.tsx
+++ b/apps/web/ui/partners/bounties/bounty-logic.tsx
@@ -88,7 +88,10 @@ export function BountyLogic({ className }: { className?: string }) {
text={
value
? isCurrencyAttribute(attribute)
- ? currencyFormatter(value)
+ ? // value is in dollars, so we need to convert to cents
+ currencyFormatter(value * 100, {
+ trailingZeroDisplay: "stripIfInteger",
+ })
: value
: "amount"
}