Skip to content

Commit 8b37692

Browse files
authored
fix: retry runtime file reads -- on slow machines the delay causes errors (#1254)
1 parent 53f6aaa commit 8b37692

File tree

2 files changed

+33
-3
lines changed

2 files changed

+33
-3
lines changed

genkit-tools/common/src/manager/manager.ts

+10-3
Original file line numberDiff line numberDiff line change
@@ -27,7 +27,7 @@ import {
2727
import * as apis from '../types/apis';
2828
import { TraceData } from '../types/trace';
2929
import { logger } from '../utils/logger';
30-
import { checkServerHealth, findRuntimesDir } from '../utils/utils';
30+
import { checkServerHealth, findRuntimesDir, retriable } from '../utils/utils';
3131
import {
3232
GenkitToolsError,
3333
RuntimeEvent,
@@ -308,8 +308,15 @@ export class RuntimeManager {
308308
*/
309309
private async handleNewRuntime(filePath: string) {
310310
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+
313320
if (isValidRuntimeInfo(runtimeInfo)) {
314321
const fileName = path.basename(filePath);
315322
if (await checkServerHealth(runtimeInfo.reflectionServerUrl)) {

genkit-tools/common/src/utils/utils.ts

+23
Original file line numberDiff line numberDiff line change
@@ -148,3 +148,26 @@ export async function waitUntilUnresponsive(
148148
}
149149
return false;
150150
}
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+
}

0 commit comments

Comments
 (0)