Skip to content

Commit 2691ce8

Browse files
[dev] [Marfuen] mariano/batch (#1432)
* chore: remove training video backfill action and related documentation - Deleted the trigger-training-video-backfill action and its associated README documentation as they are no longer needed. This change streamlines the codebase and eliminates outdated references to backfill functionality. * feat: enhance training video backfill process with batch processing - Updated the backfillTrainingVideosForAllOrgs function to process organizations in batches of 500, improving efficiency and handling of large datasets. - Added logging for batch processing to track the number of organizations processed and any errors encountered during triggering. - Adjusted return values to reflect the total number of organizations processed and the number of batches created. --------- Co-authored-by: Mariano Fuentes <marfuen98@gmail.com>
1 parent 98c6503 commit 2691ce8

File tree

1 file changed

+32
-6
lines changed

1 file changed

+32
-6
lines changed

apps/app/src/jobs/tasks/onboarding/backfill-training-videos-for-all-orgs.ts

Lines changed: 32 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -41,24 +41,50 @@ export const backfillTrainingVideosForAllOrgs = task({
4141
);
4242

4343
// Create batch items for processing
44-
const batchItems = organizations.map((organization) => ({
44+
const allBatchItems = organizations.map((organization) => ({
4545
payload: {
4646
organizationId: organization.id,
4747
},
4848
}));
4949

50-
logger.info(`Triggering batch job for ${batchItems.length} organizations`);
50+
// Split into chunks of 500 (Trigger.dev batch size limit)
51+
const BATCH_SIZE = 500;
52+
const batches: (typeof allBatchItems)[] = [];
5153

52-
// Trigger the batch job to process all organizations
53-
await backfillTrainingVideosForOrg.batchTrigger(batchItems);
54+
for (let i = 0; i < allBatchItems.length; i += BATCH_SIZE) {
55+
batches.push(allBatchItems.slice(i, i + BATCH_SIZE));
56+
}
57+
58+
logger.info(
59+
`Splitting ${allBatchItems.length} organizations into ${batches.length} batches of max ${BATCH_SIZE} each`,
60+
);
61+
62+
// Process each batch
63+
let totalTriggered = 0;
64+
for (let i = 0; i < batches.length; i++) {
65+
const batch = batches[i];
66+
logger.info(
67+
`Triggering batch ${i + 1}/${batches.length} with ${batch.length} organizations`,
68+
);
69+
70+
try {
71+
await backfillTrainingVideosForOrg.batchTrigger(batch);
72+
totalTriggered += batch.length;
73+
logger.info(`Successfully triggered batch ${i + 1}/${batches.length}`);
74+
} catch (error) {
75+
logger.error(`Failed to trigger batch ${i + 1}/${batches.length}: ${error}`);
76+
throw error;
77+
}
78+
}
5479

5580
logger.info(
56-
`Successfully triggered training video backfill jobs for ${organizations.length} organizations`,
81+
`Successfully triggered training video backfill jobs for ${totalTriggered} organizations across ${batches.length} batches`,
5782
);
5883

5984
return {
6085
success: true,
61-
organizationsProcessed: organizations.length,
86+
organizationsProcessed: totalTriggered,
87+
totalBatches: batches.length,
6288
totalMembers,
6389
};
6490
} catch (error) {

0 commit comments

Comments
 (0)