mirror of
https://github.com/wu736139669/hapi.git
synced 2026-08-05 06:24:37 +00:00
fix: add retry logic to daemon machine registration for transient connection errors
Add exponential backoff retry mechanism to handle ECONNREFUSED errors when the server isn't ready yet during daemon startup. This includes: - New errorUtils module with error classification helpers - withRetry function in time.ts supporting configurable exponential backoff - Machine registration retry with sensible defaults (60 attempts, 1-30s delays) - Error refactoring to consolidate extractErrorInfo utility close #35
This commit is contained in:
+20
-6
@@ -12,6 +12,8 @@ import { getEnvironmentInfo } from '@/ui/doctor';
|
||||
import { spawnHappyCLI } from '@/utils/spawnHappyCLI';
|
||||
import { writeDaemonState, DaemonLocallyPersistedState, readDaemonState, acquireDaemonLock, releaseDaemonLock } from '@/persistence';
|
||||
import { isProcessAlive, isWindows, killProcess, killProcessByChildProcess } from '@/utils/process';
|
||||
import { withRetry } from '@/utils/time';
|
||||
import { isRetryableConnectionError } from '@/utils/errorUtils';
|
||||
|
||||
import { cleanupDaemonState, getInstalledCliMtimeMs, isDaemonRunningCurrentlyInstalledHappyVersion, stopDaemon } from './controlClient';
|
||||
import { startDaemonControlServer } from './controlServer';
|
||||
@@ -522,12 +524,24 @@ export async function startDaemon(): Promise<void> {
|
||||
// Create API client
|
||||
const api = await ApiClient.create();
|
||||
|
||||
// Get or create machine
|
||||
const machine = await api.getOrCreateMachine({
|
||||
machineId,
|
||||
metadata: buildMachineMetadata(),
|
||||
daemonState: initialDaemonState
|
||||
});
|
||||
// Get or create machine (with retry for transient connection errors)
|
||||
const machine = await withRetry(
|
||||
() => api.getOrCreateMachine({
|
||||
machineId,
|
||||
metadata: buildMachineMetadata(),
|
||||
daemonState: initialDaemonState
|
||||
}),
|
||||
{
|
||||
maxAttempts: 60,
|
||||
minDelay: 1000,
|
||||
maxDelay: 30000,
|
||||
shouldRetry: isRetryableConnectionError,
|
||||
onRetry: (error, attempt, nextDelayMs) => {
|
||||
const errorMsg = error instanceof Error ? error.message : String(error)
|
||||
logger.debug(`[DAEMON RUN] Failed to register machine (attempt ${attempt}), retrying in ${nextDelayMs}ms: ${errorMsg}`)
|
||||
}
|
||||
}
|
||||
);
|
||||
logger.debug(`[DAEMON RUN] Machine registered: ${machine.id}`);
|
||||
|
||||
// Create realtime machine session
|
||||
|
||||
Reference in New Issue
Block a user