From a19741d927f82d02b77a25c83d55ab1bbddc0af4 Mon Sep 17 00:00:00 2001 From: James Grugett Date: Sun, 1 May 2022 12:16:29 -0400 Subject: [PATCH] Tweak cloud function batching --- functions/src/index.ts | 2 +- functions/src/scripts/update-feed.ts | 2 +- .../{update-user-feed.ts => update-feed.ts} | 23 +++++++++++-------- functions/src/update-recommendations.ts | 7 +++--- 4 files changed, 19 insertions(+), 15 deletions(-) rename functions/src/{update-user-feed.ts => update-feed.ts} (92%) diff --git a/functions/src/index.ts b/functions/src/index.ts index 51ab5900..3c0dc8f8 100644 --- a/functions/src/index.ts +++ b/functions/src/index.ts @@ -22,7 +22,7 @@ export * from './unsubscribe' export * from './update-contract-metrics' export * from './update-user-metrics' export * from './update-recommendations' -export * from './update-user-feed' +export * from './update-feed' export * from './backup-db' export * from './change-user-info' export * from './market-close-emails' diff --git a/functions/src/scripts/update-feed.ts b/functions/src/scripts/update-feed.ts index 3a83a59a..25a0b14f 100644 --- a/functions/src/scripts/update-feed.ts +++ b/functions/src/scripts/update-feed.ts @@ -9,7 +9,7 @@ import { User } from '../../../common/user' import { batchedWaitAll } from '../../../common/util/promise' import { Contract } from '../../../common/contract' import { updateWordScores } from '../update-recommendations' -import { getFeedContracts, doUserFeedUpdate } from '../update-user-feed' +import { getFeedContracts, doUserFeedUpdate } from '../update-feed' const firestore = admin.firestore() diff --git a/functions/src/update-user-feed.ts b/functions/src/update-feed.ts similarity index 92% rename from functions/src/update-user-feed.ts rename to functions/src/update-feed.ts index cae77ee4..accd48e8 100644 --- a/functions/src/update-user-feed.ts +++ b/functions/src/update-feed.ts @@ -25,22 +25,27 @@ const firestore = admin.firestore() export const updateFeed = functions.pubsub .schedule('every 60 minutes') .onRun(async () => { - const contracts = await getFeedContracts() const users = await getValues(firestore.collection('users')) + const batchSize = 100 + const userBatches: User[][] = [] + for (let i = 0; i < users.length; i += batchSize) { + userBatches.push(users.slice(i, i + batchSize)) + } + await Promise.all( - users.map(async (user, i) => { - await new Promise((resolve) => setTimeout(resolve, 10 * i)) - await callCloudFunction('updateUserFeed', { user, contracts }) - }) + userBatches.map(async (users) => + callCloudFunction('updateFeedBatch', { users }) + ) ) }) -export const updateUserFeed = functions.https.onCall( - async (data: { contracts: Contract[]; user: User }) => { - const { user, contracts } = data +export const updateFeedBatch = functions.https.onCall( + async (data: { users: User[] }) => { + const { users } = data + const contracts = await getFeedContracts() - await doUserFeedUpdate(user, contracts) + await Promise.all(users.map((user) => doUserFeedUpdate(user, contracts))) } ) diff --git a/functions/src/update-recommendations.ts b/functions/src/update-recommendations.ts index 07abc257..4e656dda 100644 --- a/functions/src/update-recommendations.ts +++ b/functions/src/update-recommendations.ts @@ -25,10 +25,9 @@ export const updateRecommendations = functions.pubsub } await Promise.all( - userBatches.map(async (batch, i) => { - await new Promise((resolve) => setTimeout(resolve, 100 * i)) - await callCloudFunction('updateRecommendationsBatch', { users: batch }) - }) + userBatches.map((batch) => + callCloudFunction('updateRecommendationsBatch', { users: batch }) + ) ) })