File tree 2 files changed +33
-3
lines changed
2 files changed +33
-3
lines changed Original file line number Diff line number Diff line change @@ -27,7 +27,7 @@ import {
27
27
import * as apis from '../types/apis' ;
28
28
import { TraceData } from '../types/trace' ;
29
29
import { logger } from '../utils/logger' ;
30
- import { checkServerHealth , findRuntimesDir } from '../utils/utils' ;
30
+ import { checkServerHealth , findRuntimesDir , retriable } from '../utils/utils' ;
31
31
import {
32
32
GenkitToolsError ,
33
33
RuntimeEvent ,
@@ -308,8 +308,15 @@ export class RuntimeManager {
308
308
*/
309
309
private async handleNewRuntime ( filePath : string ) {
310
310
try {
311
- const content = await fs . readFile ( filePath , 'utf-8' ) ;
312
- const runtimeInfo = JSON . parse ( content ) as RuntimeInfo ;
311
+ const { content, runtimeInfo } = await retriable (
312
+ async ( ) => {
313
+ const content = await fs . readFile ( filePath , 'utf-8' ) ;
314
+ const runtimeInfo = JSON . parse ( content ) as RuntimeInfo ;
315
+ return { content, runtimeInfo } ;
316
+ } ,
317
+ { maxRetries : 10 , delayMs : 500 }
318
+ ) ;
319
+
313
320
if ( isValidRuntimeInfo ( runtimeInfo ) ) {
314
321
const fileName = path . basename ( filePath ) ;
315
322
if ( await checkServerHealth ( runtimeInfo . reflectionServerUrl ) ) {
Original file line number Diff line number Diff line change @@ -148,3 +148,26 @@ export async function waitUntilUnresponsive(
148
148
}
149
149
return false ;
150
150
}
151
+
152
+ export async function retriable < T > (
153
+ fn : ( ) => Promise < T > ,
154
+ opts : { maxRetries ?: number ; delayMs ?: number }
155
+ ) : Promise < T > {
156
+ const maxRetries = opts . maxRetries ?? 3 ;
157
+ const delayMs = opts . delayMs ?? 0 ;
158
+
159
+ let attempt = 0 ;
160
+ while ( true ) {
161
+ try {
162
+ return await fn ( ) ;
163
+ } catch ( e ) {
164
+ if ( attempt >= maxRetries - 1 ) {
165
+ throw e ;
166
+ }
167
+ if ( delayMs > 0 ) {
168
+ await new Promise ( ( r ) => setTimeout ( r , delayMs ) ) ;
169
+ }
170
+ }
171
+ attempt ++ ;
172
+ }
173
+ }
You can’t perform that action at this time.
0 commit comments