Skip to content

Commit

Permalink
split up sends into groups of 50
Browse files Browse the repository at this point in the history
  • Loading branch information
bucko13 committed Oct 22, 2021
1 parent 7f81458 commit 1d2b4b0
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 20 deletions.
3 changes: 3 additions & 0 deletions lib/utils.ts
Original file line number Diff line number Diff line change
Expand Up @@ -27,3 +27,6 @@ export const getProfileUrl = (id: string): string => {

return new URL(`profile/${id}`, base).href
}

export const delay = (time: number): Promise<void> =>
new Promise((resolve) => setTimeout(resolve, time))
46 changes: 26 additions & 20 deletions pages/api/fees.ts
Original file line number Diff line number Diff line change
Expand Up @@ -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,
Expand Down Expand Up @@ -50,26 +50,32 @@ const sendEmail = async (
destinations.push(destination)
}

try {
const params = {
Source: "[email protected]",
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: "[email protected]",
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}`)
}
}

Expand Down

1 comment on commit 1d2b4b0

@vercel
Copy link

@vercel vercel bot commented on 1d2b4b0 Oct 22, 2021

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please sign in to comment.