diff --git a/functions/src/update-feed.ts b/functions/src/update-feed.ts index 59d6ddd8..44a39e61 100644 --- a/functions/src/update-feed.ts +++ b/functions/src/update-feed.ts @@ -26,18 +26,26 @@ import { CATEGORY_LIST } from '../../common/categories' const firestore = admin.firestore() +const BATCH_SIZE = 30 +const MAX_BATCHES = 50 + +const getUserBatches = async () => { + const users = _.shuffle(await getValues(firestore.collection('users'))) + let userBatches: User[][] = [] + for (let i = 0; i < users.length; i += BATCH_SIZE) { + userBatches.push(users.slice(i, i + BATCH_SIZE)) + } + userBatches = userBatches.slice(0, MAX_BATCHES) + + console.log('updating feed batches', userBatches.length, 'of', MAX_BATCHES) + + return userBatches +} + export const updateFeed = functions.pubsub - .schedule('every 60 minutes') + .schedule('every 5 minutes') .onRun(async () => { - const users = await getValues(firestore.collection('users')) - - const batchSize = 100 - let userBatches: User[][] = [] - for (let i = 0; i < users.length; i += batchSize) { - userBatches.push(users.slice(i, i + batchSize)) - } - - console.log('updating feed batch') + const userBatches = await getUserBatches() await Promise.all( userBatches.map((users) => @@ -72,13 +80,7 @@ export const updateFeedBatch = functions.https.onCall( export const updateCategoryFeed = functions.https.onCall( async (data: { category: string }) => { const { category } = data - 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)) - } + const userBatches = await getUserBatches() await Promise.all( userBatches.map(async (users) => {