From 1d2b4b04b1495c68d57947e73f46b33251bbcdaf Mon Sep 17 00:00:00 2001 From: Buck Perley Date: Thu, 21 Oct 2021 13:55:15 -0500 Subject: [PATCH] split up sends into groups of 50 --- lib/utils.ts | 3 +++ pages/api/fees.ts | 46 ++++++++++++++++++++++++++-------------------- 2 files changed, 29 insertions(+), 20 deletions(-) diff --git a/lib/utils.ts b/lib/utils.ts index 1a39f96..b7f615e 100644 --- a/lib/utils.ts +++ b/lib/utils.ts @@ -27,3 +27,6 @@ export const getProfileUrl = (id: string): string => { return new URL(`profile/${id}`, base).href } + +export const delay = (time: number): Promise => + new Promise((resolve) => setTimeout(resolve, time)) diff --git a/pages/api/fees.ts b/pages/api/fees.ts index eb553e3..68512e8 100644 --- a/pages/api/fees.ts +++ b/pages/api/fees.ts @@ -2,7 +2,7 @@ import { NextApiHandler } from "next" import axios from "axios" import prisma from "@/lib/prisma" import AWS from "aws-sdk" -import { AlertType, getProfileUrl, getFeeAlertType } from "@/lib/utils" +import { AlertType, getProfileUrl, getFeeAlertType, delay } from "@/lib/utils" const SESConfig = { accessKeyId: process.env.AWS_SES_ACCESS_KEY_ID, @@ -50,26 +50,32 @@ const sendEmail = async ( destinations.push(destination) } - try { - const params = { - Source: "notifications@txfees.watch", - Template: type, - Destinations: destinations, - DefaultTemplateData: JSON.stringify({ - HIGH_FEE: process.env.HIGH_FEE, - LOW_FEE: process.env.HIGH_FEE, - date, - hourFee, - minimumFee, - profileUrl: "https://txfees.watch", - }), + while (destinations.length) { + // max send per hour + const destinationList = destinations.splice(0, 49) + try { + const params = { + Source: "notifications@txfees.watch", + Template: type, + Destinations: destinationList, + DefaultTemplateData: JSON.stringify({ + HIGH_FEE: process.env.HIGH_FEE, + LOW_FEE: process.env.HIGH_FEE, + date, + hourFee, + minimumFee, + profileUrl: "https://txfees.watch", + }), + } + console.log(`Sending ${destinationList.length} ${type} email alerts`) + const client = new AWS.SES(SESConfig) + const resp = await client.sendBulkTemplatedEmail(params).promise() + console.log("email response: ", resp) + // delay before next send + await delay(250) + } catch (e) { + console.error(`Problem sending emails: ${e.message}`) } - console.log(`Sending ${data.length} ${type} email alerts`) - const client = new AWS.SES(SESConfig) - const resp = await client.sendBulkTemplatedEmail(params).promise() - console.log("email response: ", resp) - } catch (e) { - console.error(`Problem sending emails: ${e.message}`) } }