@@ -10,7 +10,7 @@ import type { Namespace, Repository } from "@acme/extract-schema";
1010import { GitHubSourceControl , GitlabSourceControl } from "@acme/source-control" ;
1111import type { Pagination } from "@acme/source-control" ;
1212import { Config } from "sst/node/config" ;
13- import { extractMemberPageBatchMessage } from "./messages" ;
13+ import { extractMemberPageMessage } from "./messages" ;
1414
1515import { QueueHandler } from "./create-message" ;
1616
@@ -61,7 +61,7 @@ const extractMembersPage = async ({ namespace, repository, sourceControl, userId
6161 context . integrations . sourceControl = await initSourceControl ( userId , sourceControl ) ;
6262 } catch ( error ) {
6363 console . error ( error ) ;
64- return ;
64+ throw error ;
6565 }
6666
6767 const { paginationInfo : resultPaginationInfo } = await getMembers ( {
@@ -76,11 +76,7 @@ const extractMembersPage = async ({ namespace, repository, sourceControl, userId
7676 return resultPaginationInfo ;
7777} ;
7878
79- const range = ( a : number , b : number ) => Array . apply ( 0 , { length : b - a + 1 } as number [ ] ) . map ( ( _ , index ) => index + a ) ;
80- const chunks = < T > ( array : Array < T > , size : number ) => Array . apply ( 0 , { length : Math . ceil ( array . length / size ) } as unknown [ ] ) . map ( ( _ , index ) => array . slice ( index * size , ( index + 1 ) * size ) ) ;
81-
8279export const eventHandler = EventHandler ( extractRepositoryEvent , async ( ev ) => {
83-
8480 const pagination = await extractMembersPage ( {
8581 namespace : ev . properties . namespace ,
8682 repository : ev . properties . repository ,
@@ -89,32 +85,32 @@ export const eventHandler = EventHandler(extractRepositoryEvent, async (ev) => {
8985 paginationInfo : { page : 1 , perPage : 2 , totalPages : 1000 } ,
9086 } ) ;
9187
92- if ( ! pagination ) return ;
93-
94- const remainingMemberPages = range ( 2 , pagination . totalPages )
95- . map ( page => ( {
96- page,
97- perPage : pagination . perPage ,
98- totalPages : pagination . totalPages
99- } satisfies Pagination ) ) ;
100-
101- const batchedPages = chunks ( remainingMemberPages , 10 ) ;
102-
103- await Promise . all ( batchedPages . map ( batch => extractMemberPageBatchMessage . send (
104- batch . map ( page => ( {
88+ const arrayOfExtractMemberPageMessageContent : { repository : Repository , namespace : Namespace | null , pagination : Pagination } [ ] = [ ] ;
89+ for ( let i = 2 ; i <= pagination . totalPages ; i ++ ) {
90+ arrayOfExtractMemberPageMessageContent . push ( {
10591 namespace : ev . properties . namespace ,
10692 repository : ev . properties . repository ,
107- pagination : page
108- } ) ) , {
93+ pagination : {
94+ page : i ,
95+ perPage : pagination . perPage ,
96+ totalPages : pagination . totalPages
97+ }
98+ } )
99+ }
100+
101+ if ( arrayOfExtractMemberPageMessageContent . length === 0 ) return console . log ( "No more pages left, no need to enqueue" ) ;
102+
103+ await extractMemberPageMessage . sendAll ( arrayOfExtractMemberPageMessageContent , {
109104 version : 1 ,
110105 caller : 'extract-member' ,
111106 sourceControl : ev . metadata . sourceControl ,
112107 userId : ev . metadata . userId ,
113108 timestamp : new Date ( ) . getTime ( ) ,
114- } ) ) ) ;
109+ } )
110+
115111} ) ;
116112
117- export const queueHandler = QueueHandler ( extractMemberPageBatchMessage , async ( message ) => {
113+ export const queueHandler = QueueHandler ( extractMemberPageMessage , async ( message ) => {
118114 await extractMembersPage ( {
119115 namespace : message . content . namespace ,
120116 paginationInfo : message . content . pagination ,
0 commit comments