Skip to content

Open-source status page, incident response and uptime monitoring

chore: parallelize follow-up email sending with Promise.all (#1239)

Ajinkya Anil Kahane0d3ba6c88e20
diff --git a/apps/workflows/package.json b/apps/workflows/package.json
index c70d2daf2..1679ec5c7 100644
--- a/apps/workflows/package.json
+++ b/apps/workflows/package.json
@@ -27,6 +27,7 @@
},
"devDependencies": {
"@openstatus/tsconfig": "workspace:*",
- "@types/bun": "latest"
+ "@types/bun": "latest",
+ "typescript": "5.6.2"
}
}
diff --git a/apps/workflows/src/cron/emails.ts b/apps/workflows/src/cron/emails.ts
index fb45f06f2..3ca3b507b 100644
--- a/apps/workflows/src/cron/emails.ts
+++ b/apps/workflows/src/cron/emails.ts
@@ -14,16 +14,33 @@ export async function sendFollowUpEmails() {
date2.setDate(date2.getDate() - 2);
const users = await db
- .select()
+ .select({
+ email: user.email,
+ })
.from(user)
.where(and(gte(user.createdAt, date1), lte(user.createdAt, date2)))
.all();
console.log(`Found ${users.length} users to send follow ups.`);
- for (const user of users) {
- if (user.email) {
- await email.sendFollowUp({ to: user.email });
+ // Filter valid emails
+ const validEmails = users
+ .map((u) => u.email)
+ .filter((email) => email !== null)
+ // I don't know why but I can't have both filter at the same time
+ .filter((email) => email.trim() !== "");
+
+ // Chunk emails into batches of 80
+ const batchSize = 80;
+ for (let i = 0; i < validEmails.length; i += batchSize) {
+ const batch = validEmails.slice(i, i + batchSize);
+ console.log(`Sending batch with ${batch.length} emails...`);
+ try {
+ await email.sendFollowUpBatched({ to: batch });
+ } catch {
+ //Stop email send when rate limit error is faced in order to avoid wasteful API calls
+ console.error("Rate limit exceeded. Stopping further sends.");
+ break;
}
}
}
diff --git a/packages/emails/src/client.tsx b/packages/emails/src/client.tsx
index 0363e8cc9..5679e4655 100644
--- a/packages/emails/src/client.tsx
+++ b/packages/emails/src/client.tsx
@@ -1,7 +1,7 @@
/** @jsxImportSource react */
import { render } from "@react-email/render";
-import { Resend } from "resend";
+import { ErrorResponse, Resend } from "resend";
import FollowUpEmail from "../emails/followup";
import MonitorAlertEmail from "../emails/monitor-alert";
import type { MonitorAlertProps } from "../emails/monitor-alert";
@@ -42,6 +42,34 @@ export class EmailClient {
}
}
+ public async sendFollowUpBatched(req: { to: string[] }) {
+ if (process.env.NODE_ENV === "development") return;
+
+ const html = await render(<FollowUpEmail />);
+ const result = await this.client.batch.send(
+ req.to.map((subscriber) => ({
+ from: "Thibault Le Ouay Ducasse <[email protected]>",
+ subject: "How's it going with OpenStatus?",
+ to: subscriber,
+ html,
+ })),
+ );
+
+ if (result.error) {
+ // We only throw the error if we are rate limited
+ if (result.error?.name === "rate_limit_exceeded") {
+ throw result.error;
+ }
+ // Otherwise let's log the error and continue
+ console.error(
+ `Error sending follow up email to ${req.to}: ${result.error}`,
+ );
+ return;
+ }
+
+ console.log(`Sent follow up emails to ${req.to}`);
+ }
+
public async sendStatusReportUpdate(
req: StatusReportProps & { to: string[] },
) {
@@ -78,8 +106,12 @@ export class EmailClient {
try {
const html = await render(<TeamInvitationEmail {...req} />);
const result = await this.client.emails.send({
- from: `${req.workspaceName ?? "OpenStatus"} <[email protected]>`,
- subject: `You've been invited to join ${req.workspaceName ?? "OpenStatus"}`,
+ from: `${
+ req.workspaceName ?? "OpenStatus"
+ subject: `You've been invited to join ${
+ req.workspaceName ?? "OpenStatus"
+ }`,
to: req.to,
html,
});
diff --git a/pnpm-lock.yaml b/pnpm-lock.yaml
index c29ad8f55..e8d6a2cf1 100644
--- a/pnpm-lock.yaml
+++ b/pnpm-lock.yaml
@@ -529,6 +529,9 @@ importers:
'@types/bun':
specifier: latest
version: 1.2.5
+ typescript:
+ specifier: 5.6.2
+ version: 5.6.2
packages/analytics:
dependencies: