Skip to content

Commit d46615c

Browse files
committed
add retry for config loading
1 parent 63d3123 commit d46615c

File tree

1 file changed

+32
-3
lines changed

1 file changed

+32
-3
lines changed

packages/shared/src/utils.ts

Lines changed: 32 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -27,9 +27,38 @@ export const loadConfig = async (configPath: string): Promise<SourcebotConfig> =
2727
}
2828
return response.text();
2929
} else {
30-
return readFile(configPath, {
31-
encoding: 'utf-8',
32-
});
30+
// Retry logic for handling race conditions with mounted volumes
31+
const maxAttempts = 5;
32+
const retryDelayMs = 2000;
33+
let lastError: Error | null = null;
34+
35+
for (let attempt = 1; attempt <= maxAttempts; attempt++) {
36+
try {
37+
return await readFile(configPath, {
38+
encoding: 'utf-8',
39+
});
40+
} catch (error) {
41+
lastError = error as Error;
42+
43+
// Only retry on ENOENT errors (file not found)
44+
if ((error as NodeJS.ErrnoException)?.code !== 'ENOENT') {
45+
throw error; // Throw immediately for non-ENOENT errors
46+
}
47+
48+
// Log warning before retry (except on the last attempt)
49+
if (attempt < maxAttempts) {
50+
console.warn(`Config file not found, retrying in 2s... (Attempt ${attempt}/${maxAttempts})`);
51+
await new Promise(resolve => setTimeout(resolve, retryDelayMs));
52+
}
53+
}
54+
}
55+
56+
// If we've exhausted all retries, throw the last ENOENT error
57+
if (lastError) {
58+
throw lastError;
59+
}
60+
61+
throw new Error('Failed to load config after all retry attempts');
3362
}
3463
})();
3564

0 commit comments

Comments
 (0)