-
Notifications
You must be signed in to change notification settings - Fork 189
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
add email queue to handle email sending failures
- Loading branch information
1 parent
8e3e845
commit 1c22c8a
Showing
14 changed files
with
193 additions
and
193 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
import { transporter } from "~/emails/transporter.server"; | ||
import { QueueNames, scheduler } from "~/utils/scheduler.server"; | ||
import type { EmailPayloadType } from "./types"; | ||
import { SMTP_FROM } from "../utils/env"; | ||
import { ShelfError } from "../utils/error"; | ||
|
||
export const registerEmailWorkers = async () => { | ||
await scheduler.work<EmailPayloadType>( | ||
QueueNames.emailQueue, | ||
{ newJobCheckIntervalSeconds: 60 * 3, teamSize: 2 }, | ||
async (job) => { | ||
await triggerEmail(job.data); | ||
} | ||
); | ||
}; | ||
|
||
export const triggerEmail = async ({ | ||
to, | ||
subject, | ||
text, | ||
html, | ||
from, | ||
replyTo, | ||
}: EmailPayloadType) => { | ||
try { | ||
// send mail with defined transport object | ||
await transporter.sendMail({ | ||
from: from || SMTP_FROM || `"Shelf" <[email protected]>`, // sender address | ||
replyTo: replyTo || "[email protected]", // reply to | ||
to, // list of receivers | ||
subject, // Subject line | ||
text, // plain text body | ||
html: html || "", // html body | ||
}); | ||
} catch (cause) { | ||
throw new ShelfError({ | ||
cause, | ||
message: "Unable to send email", | ||
additionalData: { to, subject, from }, | ||
label: "Email", | ||
}); | ||
} | ||
|
||
// verify connection configuration | ||
// transporter.verify(function (error) { | ||
// if (error) { | ||
// // eslint-disable-next-line no-console | ||
// console.log(error); | ||
// } else { | ||
// // eslint-disable-next-line no-console | ||
// console.log("Server is ready to take our messages"); | ||
// } | ||
// }); | ||
|
||
// Message sent: <[email protected]> | ||
|
||
// Preview only available when sending through an Ethereal account | ||
// console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); | ||
}; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,104 +1,39 @@ | ||
import type { Attachment } from "nodemailer/lib/mailer"; | ||
import { transporter } from "~/emails/transporter.server"; | ||
import { SMTP_FROM } from "../utils/env"; | ||
import { ShelfError } from "../utils/error"; | ||
|
||
export const sendEmail = async ({ | ||
to, | ||
subject, | ||
text, | ||
html, | ||
attachments, | ||
from, | ||
replyTo, | ||
}: { | ||
/** Email address of recipient */ | ||
to: string; | ||
|
||
/** Subject of email */ | ||
subject: string; | ||
|
||
/** Text content of email */ | ||
text: string; | ||
|
||
/** HTML content of email */ | ||
html?: string; | ||
|
||
attachments?: Attachment[]; | ||
|
||
/** Override the default sender */ | ||
from?: string; | ||
import { Logger } from "~/utils/logger"; | ||
import { QueueNames, scheduler } from "~/utils/scheduler.server"; | ||
import { triggerEmail } from "./email.worker.server"; | ||
import type { EmailPayloadType } from "./types"; | ||
|
||
export const sendEmail = (payload: EmailPayloadType) => { | ||
// attempt to send email, push to the queue if it fails | ||
triggerEmail(payload).catch((err) => { | ||
Logger.warn({ | ||
err, | ||
details: { | ||
to: payload.to, | ||
subject: payload.subject, | ||
from: payload.from, | ||
}, | ||
message: "email sending failed, pushing to the queue", | ||
}); | ||
void addToQueue(payload); | ||
}); | ||
}; | ||
|
||
/** Override the default reply to email address */ | ||
replyTo?: string; | ||
}) => { | ||
const addToQueue = async (payload: EmailPayloadType) => { | ||
try { | ||
// send mail with defined transport object | ||
await transporter.sendMail({ | ||
from: from || SMTP_FROM || `"Shelf" <[email protected]>`, // sender address | ||
replyTo: replyTo || "[email protected]", // reply to | ||
to, // list of receivers | ||
subject, // Subject line | ||
text, // plain text body | ||
html: html || "", // html body | ||
attachments: [...(attachments || [])], | ||
await scheduler.send(QueueNames.emailQueue, payload, { | ||
retryLimit: 5, | ||
retryDelay: 5, | ||
}); | ||
} catch (cause) { | ||
throw new ShelfError({ | ||
cause, | ||
message: "Unable to send email", | ||
additionalData: { to, subject, from }, | ||
label: "Email", | ||
} catch (err) { | ||
Logger.warn({ | ||
err, | ||
details: { | ||
to: payload.to, | ||
subject: payload.subject, | ||
from: payload.from, | ||
}, | ||
message: "Failed to push email payload to queue", | ||
}); | ||
} | ||
|
||
// verify connection configuration | ||
// transporter.verify(function (error) { | ||
// if (error) { | ||
// // eslint-disable-next-line no-console | ||
// console.log(error); | ||
// } else { | ||
// // eslint-disable-next-line no-console | ||
// console.log("Server is ready to take our messages"); | ||
// } | ||
// }); | ||
|
||
// Message sent: <[email protected]> | ||
|
||
// Preview only available when sending through an Ethereal account | ||
// console.log("Preview URL: %s", nodemailer.getTestMessageUrl(info)); | ||
}; | ||
|
||
/** Utility function to add delay between operations */ | ||
async function delay(ms: number): Promise<void> { | ||
return new Promise((resolve) => setTimeout(resolve, ms)); | ||
} | ||
|
||
/** Process emails in batches with rate limiting | ||
* @param emails - Array of email configurations to send | ||
* @param batchSize - Number of emails to process per batch (default: 2) | ||
* @param delayMs - Milliseconds to wait between batches (default: 1000ms) | ||
*/ | ||
export async function sendEmailsWithRateLimit( | ||
emails: Array<{ | ||
to: string; | ||
subject: string; | ||
text: string; | ||
html: string; | ||
}>, | ||
batchSize = 2, | ||
delayMs = 1100 | ||
): Promise<void> { | ||
for (let i = 0; i < emails.length; i += batchSize) { | ||
// Process emails in batches of specified size | ||
const batch = emails.slice(i, i + batchSize); | ||
|
||
// Send emails in current batch concurrently | ||
await Promise.all(batch.map((email) => sendEmail(email))); | ||
|
||
// If there are more emails to process, add delay before next batch | ||
if (i + batchSize < emails.length) { | ||
await delay(delayMs); | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.