fix: cron scheduleEmailRemider time out (#12108)

Co-authored-by: CarinaWolli <wollencarina@gmail.com>
pull/12095/head
Carina Wollendorfer 2023-10-27 07:53:53 -04:00 committed by GitHub
parent b9cef10ef2
commit 08d65c85de
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 84 additions and 55 deletions

View File

@ -26,6 +26,7 @@ const sendgridAPIKey = process.env.SENDGRID_API_KEY as string;
const senderEmail = process.env.SENDGRID_EMAIL as string;
sgMail.setApiKey(sendgridAPIKey);
client.setApiKey(sendgridAPIKey);
type Booking = Prisma.BookingGetPayload<{
include: {
@ -106,6 +107,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const pageSize = 90;
let pageNumber = 0;
const deletePromises = [];
//delete batch_ids with already past scheduled date from scheduled_sends
while (true) {
@ -128,19 +130,25 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
break;
}
for (const reminder of remindersToDelete) {
try {
await client.request({
deletePromises.push(
remindersToDelete.map((reminder) =>
client.request({
url: `/v3/user/scheduled_sends/${reminder.referenceId}`,
method: "DELETE",
});
} catch (error) {
console.log(`Error deleting batch id from scheduled_sends: ${error}`);
}
}
})
)
);
pageNumber++;
}
Promise.allSettled(deletePromises).then((results) => {
results.forEach((result) => {
if (result.status === "rejected") {
console.log(`Error deleting batch id from scheduled_sends: ${result.reason}`);
}
});
});
await prisma.workflowReminder.deleteMany({
where: {
method: WorkflowMethods.EMAIL,
@ -153,6 +161,9 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
//cancel reminders for cancelled/rescheduled bookings that are scheduled within the next hour
pageNumber = 0;
const allPromisesCancelReminders = [];
while (true) {
const remindersToCancel = await prisma.workflowReminder.findMany({
where: {
@ -175,8 +186,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}
for (const reminder of remindersToCancel) {
try {
await client.request({
const cancelPromise = client.request({
url: "/v3/user/scheduled_sends",
method: "POST",
body: {
@ -185,7 +195,7 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
},
});
await prisma.workflowReminder.update({
const updatePromise = prisma.workflowReminder.update({
where: {
id: reminder.id,
},
@ -193,14 +203,22 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
scheduled: false, // to know which reminder already got cancelled (to avoid error from cancelling the same reminders again)
},
});
} catch (error) {
console.log(`Error cancelling scheduled Emails: ${error}`);
}
allPromisesCancelReminders.push(cancelPromise, updatePromise);
}
pageNumber++;
}
Promise.allSettled(allPromisesCancelReminders).then((results) => {
results.forEach((result) => {
if (result.status === "rejected") {
console.log(`Error cancelling scheduled_sends: ${result.reason}`);
}
});
});
pageNumber = 0;
const sendEmailPromises = [];
while (true) {
//find all unscheduled Email reminders
@ -390,7 +408,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
const batchId = batchIdResponse[1].batch_id;
if (reminder.workflowStep.action !== WorkflowActions.EMAIL_ADDRESS) {
await sgMail.send({
sendEmailPromises.push(
sgMail.send({
to: sendTo,
from: {
email: senderEmail,
@ -417,7 +436,8 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
},
]
: undefined,
});
})
);
}
await prisma.workflowReminder.update({
@ -436,6 +456,15 @@ async function handler(req: NextApiRequest, res: NextApiResponse) {
}
pageNumber++;
}
Promise.allSettled(sendEmailPromises).then((results) => {
results.forEach((result) => {
if (result.status === "rejected") {
console.log("Email sending failed", result.reason);
}
});
});
res.status(200).json({ message: "Emails scheduled" });
}