Wait and poll until proxy starts for max 60s

This commit is contained in:
Sander Declerck 2026-01-13 10:02:48 +01:00
parent 6006760b67
commit 0411a579ae
No known key found for this signature in database

View file

@ -25,8 +25,6 @@ export function getRamaPath() {
const executableDir = dirname(process.execPath); const executableDir = dirname(process.execPath);
const ramaPath = join(executableDir, "safechain-proxy"); const ramaPath = join(executableDir, "safechain-proxy");
ui.writeWarning(ramaPath);
if (existsSync(ramaPath)) { if (existsSync(ramaPath)) {
return ramaPath; return ramaPath;
} }
@ -46,7 +44,7 @@ export function createRamaProxy(ramaPath) {
return { return {
startServer: async () => { startServer: async () => {
ramaInstance = await startRama(ramaPath, tempDir); ramaInstance = await startRama(ramaPath, tempDir);
ui.writeInformation( ui.writeVerbose(
`Proxy started at address "${ramaInstance.proxyAddress}"` `Proxy started at address "${ramaInstance.proxyAddress}"`
); );
}, },
@ -73,6 +71,7 @@ export function createRamaProxy(ramaPath) {
* @returns {Promise<RamaProxyInstance>} * @returns {Promise<RamaProxyInstance>}
*/ */
async function startRama(ramaPath, dataFolder) { async function startRama(ramaPath, dataFolder) {
const startTime = Date.now();
const args = ["--secrets", "memory", "--data", dataFolder]; const args = ["--secrets", "memory", "--data", dataFolder];
const process = const process =
getLoggingLevel() === LOGGING_VERBOSE getLoggingLevel() === LOGGING_VERBOSE
@ -81,13 +80,22 @@ async function startRama(ramaPath, dataFolder) {
}) })
: spawn(ramaPath, args); : spawn(ramaPath, args);
// wait some time to allow the proxy process to start // wait for the proxy process to start (poll for proxy.addr.txt file)
await new Promise((resolve) => setTimeout(resolve, 5000)); const proxyAddrPath = join(dataFolder, "proxy.addr.txt");
const maxWaitTime = 60000; // 60 seconds
const pollInterval = 500; // 500 ms
const proxyAddress = await readFilePromise( while (!existsSync(proxyAddrPath)) {
join(dataFolder, "proxy.addr.txt"), if (Date.now() - startTime > maxWaitTime) {
"utf-8" throw new Error("Timeout waiting for proxy to start");
); }
await new Promise((resolve) => setTimeout(resolve, pollInterval));
}
const elapsedTime = Date.now() - startTime;
ui.writeVerbose(`Proxy started in ${elapsedTime}ms`);
const proxyAddress = await readFilePromise(proxyAddrPath, "utf-8");
const metaAddress = await readFilePromise( const metaAddress = await readFilePromise(
join(dataFolder, "meta.addr.txt"), join(dataFolder, "meta.addr.txt"),
"utf-8" "utf-8"