block sending emails on limits (#216)

This commit is contained in:
KM Koushik
2025-09-08 18:08:57 +10:00
committed by GitHub
parent 5b3022c27b
commit 55d8c7e998
21 changed files with 844 additions and 132 deletions

View File

@@ -10,6 +10,8 @@ import { DEFAULT_QUEUE_OPTIONS } from "../queue/queue-constants";
import { Prisma } from "@prisma/client";
import { logger } from "../logger/log";
import { createWorkerHandler, TeamJob } from "../queue/bullmq-context";
import { LimitService } from "./limit-service";
// Notifications about limits are handled inside LimitService.
type QueueEmailJob = TeamJob<{
emailId: string;
@@ -366,6 +368,29 @@ async function executeEmail(job: QueueEmailJob) {
}
try {
// Check limits right before sending (cloud-only)
const limitCheck = await LimitService.checkEmailLimit(email.teamId);
logger.info({ limitCheck }, `[EmailQueueService]: Limit check`);
if (limitCheck.isLimitReached) {
await db.emailEvent.create({
data: {
emailId: email.id,
status: "FAILED",
data: {
error: "Email sending limit reached",
reason: limitCheck.reason,
limit: limitCheck.limit,
},
teamId: email.teamId,
},
});
await db.email.update({
where: { id: email.id },
data: { latestStatus: "FAILED" },
});
return;
}
const messageId = await sendRawEmail({
to: email.to,
from: email.from,