@@ -65,13 +65,107 @@ async function startProcessJob(dirName: string): Promise<boolean> {
6565 }
6666}
6767
68+ /**
69+ * This function retries the given function until it returns true or the maximum number of retries is reached.
70+ *
71+ * @param maxRetries The maximum number of retries to be made before giving up. If set to -1, it will retry indefinitely
72+ * @param retryInterval The time interval between each retry in milliseconds
73+ * @param fn The function to be retried
74+ * @returns true/false (boolean) representing the success or failure respectively
75+ */
76+ async function keepRetrying (
77+ maxRetries : number ,
78+ retryInterval : number ,
79+ fn : ( ) => Promise < any >
80+ ) : Promise < boolean > {
81+ if ( maxRetries < 0 ) maxRetries = Number . MAX_SAFE_INTEGER
82+
83+ let retries = 0
84+ while ( retries < maxRetries ) {
85+ if ( await fn ( ) ) return true
86+
87+ await new Promise ( ( resolve ) => setTimeout ( resolve , retryInterval ) )
88+ retries ++
89+ }
90+
91+ return false
92+ }
93+
94+ /**
95+ * This function sends the success response to the client
96+ *
97+ * @param successURL The URL where the success response is to be sent
98+ */
99+ async function sendSuccessResponse ( successURL : string , startTime : number ) {
100+ const success = await keepRetrying ( 100 , 1000 , async ( ) => {
101+ await fetch ( successURL , {
102+ method : "POST" ,
103+ headers : {
104+ "Content-Type" : "application/json" ,
105+ } ,
106+ body : JSON . stringify ( {
107+ status : 200 ,
108+ message : "INGESTION_COMPLETED" ,
109+ timeTaken : `${ ( Date . now ( ) - startTime ) / 1000 } s` ,
110+ } ) ,
111+ } )
112+ } )
113+ if ( ! success ) {
114+ console . error ( "Failed to send success response" )
115+ }
116+ }
117+
118+ /**
119+ * This function sends the failure response to the client
120+ *
121+ * @param failureURL The URL where the failure response is to be sent
122+ */
123+ async function sendFailureResponse (
124+ failureURL : string ,
125+ reason : string ,
126+ startTime : number
127+ ) {
128+ const success = await keepRetrying ( 100 , 1000 , async ( ) => {
129+ await fetch ( failureURL , {
130+ method : "POST" ,
131+ headers : {
132+ "Content-Type" : "application/json" ,
133+ } ,
134+ body : JSON . stringify ( {
135+ status : 400 ,
136+ message : "INGESTION_FAILED" ,
137+ reason : reason ,
138+ timeTaken : `${ ( Date . now ( ) - startTime ) / 1000 } s` ,
139+ } ) ,
140+ } )
141+ } )
142+ if ( ! success ) {
143+ console . error ( "Failed to send failure response" )
144+ }
145+ }
146+
68147/**
69148 *
70149 * @param _ request from the server to ingest code.
71150 * @param res response tells that ingestion is sucessfull or not
72151 *
73152 */
74- export async function ingestRoute ( _ : Request , res : Response ) {
153+ export async function ingestRoute ( req : Request , res : Response ) {
154+ const { successURL, failureURL } = req . body
155+ if ( ! successURL || ! failureURL ) {
156+ return res . status ( 400 ) . send ( {
157+ status : 400 ,
158+ message : "BAD_REQUEST" ,
159+ reason : "URLS_MISSING" ,
160+ } )
161+ }
162+
163+ // Send the response back to the client so that request is not kept waiting & timeout eventually
164+ res . status ( 200 ) . send ( {
165+ status : 200 ,
166+ message : "INGESTION INITIATED" ,
167+ } )
168+
75169 const sessionID = nanoid . customAlphabet (
76170 "1234567890abcdefghijklmnopqrstuvwxyz"
77171 ) ( 10 )
@@ -80,34 +174,19 @@ export async function ingestRoute(_: Request, res: Response) {
80174 {
81175 let success = false
82176
177+ /* Step 1: Fetch codebase to local storage */
83178 success = fetchCodebaseFromRemote (
84179 sessionID ,
85180 "https://github.com/RocketChat/Rocket.Chat"
86181 )
87- if ( ! success ) {
88- console . error ( "Error while fetching code." )
89- return res . status ( 500 ) . send ( {
90- status : 500 ,
91- message : "FETCH_FAIL" ,
92- timeTaken : `0s` ,
93- } )
94- }
182+ if ( ! success )
183+ return await sendFailureResponse ( failureURL , "FETCH_FAIL" , startTime )
95184
185+ /* Step 2: Start the process job */
96186 success = await startProcessJob ( sessionID )
97- if ( ! success ) {
98- console . error ( "Error in processing code." )
99- return res . status ( 500 ) . send ( {
100- status : 500 ,
101- message : "PROCESS_JOB_FAIL" ,
102- timeTaken : `0s` ,
103- } )
104- }
187+ if ( ! success )
188+ return await sendFailureResponse ( failureURL , "PROCESS_FAIL" , startTime )
105189 }
106- const endTime = Date . now ( )
107190
108- res . status ( 200 ) . send ( {
109- status : 200 ,
110- message : "INGESTION SUCCESSFUL" ,
111- timeTaken : `${ ( endTime - startTime ) / 1000 } s` ,
112- } )
191+ await sendSuccessResponse ( successURL , startTime )
113192}
0 commit comments