Tweak cloud function batching
This commit is contained in:
parent
65a8042414
commit
a19741d927
|
@ -22,7 +22,7 @@ export * from './unsubscribe'
|
||||||
export * from './update-contract-metrics'
|
export * from './update-contract-metrics'
|
||||||
export * from './update-user-metrics'
|
export * from './update-user-metrics'
|
||||||
export * from './update-recommendations'
|
export * from './update-recommendations'
|
||||||
export * from './update-user-feed'
|
export * from './update-feed'
|
||||||
export * from './backup-db'
|
export * from './backup-db'
|
||||||
export * from './change-user-info'
|
export * from './change-user-info'
|
||||||
export * from './market-close-emails'
|
export * from './market-close-emails'
|
||||||
|
|
|
@ -9,7 +9,7 @@ import { User } from '../../../common/user'
|
||||||
import { batchedWaitAll } from '../../../common/util/promise'
|
import { batchedWaitAll } from '../../../common/util/promise'
|
||||||
import { Contract } from '../../../common/contract'
|
import { Contract } from '../../../common/contract'
|
||||||
import { updateWordScores } from '../update-recommendations'
|
import { updateWordScores } from '../update-recommendations'
|
||||||
import { getFeedContracts, doUserFeedUpdate } from '../update-user-feed'
|
import { getFeedContracts, doUserFeedUpdate } from '../update-feed'
|
||||||
|
|
||||||
const firestore = admin.firestore()
|
const firestore = admin.firestore()
|
||||||
|
|
||||||
|
|
|
@ -25,22 +25,27 @@ const firestore = admin.firestore()
|
||||||
export const updateFeed = functions.pubsub
|
export const updateFeed = functions.pubsub
|
||||||
.schedule('every 60 minutes')
|
.schedule('every 60 minutes')
|
||||||
.onRun(async () => {
|
.onRun(async () => {
|
||||||
const contracts = await getFeedContracts()
|
|
||||||
const users = await getValues<User>(firestore.collection('users'))
|
const users = await getValues<User>(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(
|
await Promise.all(
|
||||||
users.map(async (user, i) => {
|
userBatches.map(async (users) =>
|
||||||
await new Promise((resolve) => setTimeout(resolve, 10 * i))
|
callCloudFunction('updateFeedBatch', { users })
|
||||||
await callCloudFunction('updateUserFeed', { user, contracts })
|
)
|
||||||
})
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
export const updateUserFeed = functions.https.onCall(
|
export const updateFeedBatch = functions.https.onCall(
|
||||||
async (data: { contracts: Contract[]; user: User }) => {
|
async (data: { users: User[] }) => {
|
||||||
const { user, contracts } = data
|
const { users } = data
|
||||||
|
const contracts = await getFeedContracts()
|
||||||
|
|
||||||
await doUserFeedUpdate(user, contracts)
|
await Promise.all(users.map((user) => doUserFeedUpdate(user, contracts)))
|
||||||
}
|
}
|
||||||
)
|
)
|
||||||
|
|
|
@ -25,10 +25,9 @@ export const updateRecommendations = functions.pubsub
|
||||||
}
|
}
|
||||||
|
|
||||||
await Promise.all(
|
await Promise.all(
|
||||||
userBatches.map(async (batch, i) => {
|
userBatches.map((batch) =>
|
||||||
await new Promise((resolve) => setTimeout(resolve, 100 * i))
|
callCloudFunction('updateRecommendationsBatch', { users: batch })
|
||||||
await callCloudFunction('updateRecommendationsBatch', { users: batch })
|
)
|
||||||
})
|
|
||||||
)
|
)
|
||||||
})
|
})
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue
Block a user