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-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'
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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()
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
| 
						 | 
				
			
			@ -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<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(
 | 
			
		||||
      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)))
 | 
			
		||||
  }
 | 
			
		||||
)
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			@ -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 })
 | 
			
		||||
      )
 | 
			
		||||
    )
 | 
			
		||||
  })
 | 
			
		||||
 | 
			
		||||
| 
						 | 
				
			
			
 | 
			
		|||
		Loading…
	
		Reference in New Issue
	
	Block a user