Refactor health checks to use OpenCode liveness endpoint

This commit is contained in:
Chris Scott committed 2026-07-30 23:30:15 -04:00
1 parent 5c381ddf6c
commit adc1290836
12 files changed
+62 -18

No files matched your search

-1
View File
@@ -59,7 +59,6 @@ WORKSPACE_PATH=./workspace
# ============================================
PROCESS_START_WAIT_MS=2000
PROCESS_VERIFY_WAIT_MS=1000
HEALTH_CHECK_INTERVAL_MS=5000
HEALTH_CHECK_TIMEOUT_MS=30000
# ============================================
+4 -4
View File
@@ -70,9 +70,9 @@ export function createHealthRoutes(db: Database, openCodeSupervisor?: OpenCodeSu
const opencodeManagerVersion = await opencodeManagerVersionPromise
const dbCheck = db.prepare('SELECT 1').get()
const lifecycle = openCodeSupervisor
? await openCodeSupervisor.checkNow('api_probe')
? openCodeSupervisor.getStatus()
: null
const opencodeHealthy = lifecycle?.healthy ?? await opencodeServerManager.checkHealth()
const opencodeHealthy = await opencodeServerManager.checkHealth()
const startupError = lifecycle?.lastError ?? opencodeServerManager.getLastStartupError()
const status = lifecycle?.state === 'recovering'
@@ -117,9 +117,9 @@ export function createHealthRoutes(db: Database, openCodeSupervisor?: OpenCodeSu
app.get('/processes', async (c) => {
try {
const lifecycle = openCodeSupervisor
? await openCodeSupervisor.checkNow('api_probe')
? openCodeSupervisor.getStatus()
: null
const opencodeHealthy = lifecycle?.healthy ?? await opencodeServerManager.checkHealth()
const opencodeHealthy = await opencodeServerManager.checkHealth()
return c.json({
opencode: {
@@ -31,7 +31,6 @@ import { mkdirSafe } from '../utils/fs-safe'
const MIN_OPENCODE_VERSION = '1.0.137'
const MAX_STDERR_SIZE = 10240
const HEALTH_CHECK_TIMEOUT_MS = 3000
const PLUGIN_INSTALL_TIMEOUT_MS = 120000
const PROCESS_EXIT_GRACE_MS = 2000
const PROCESS_EXIT_POLL_MS = 50
@@ -734,8 +733,8 @@ class OpenCodeServerManager {
try {
const response = await this.openCodeClient.forward({
method: 'GET',
path: '/doc',
signal: AbortSignal.timeout(HEALTH_CHECK_TIMEOUT_MS),
path: '/global/health',
signal: AbortSignal.timeout(ENV.TIMEOUTS.HEALTH_CHECK_TIMEOUT_MS),
})
return response.ok
} catch {
@@ -30,7 +30,6 @@ export type OpenCodeRecoveryAction = (typeof OPENCODE_RECOVERY_ACTIONS)[number]
export type OpenCodeOperationReason =
| 'backend_startup'
| 'health_poll'
| 'api_probe'
| 'settings_restart'
| 'settings_reload'
| 'manual'
+34
View File
@@ -24,6 +24,7 @@ vi.mock('bun:sqlite', () => ({
import { opencodeServerManager } from '../../src/services/opencode-single-server'
import { createHealthRoutes } from '../../src/routes/health'
import type { OpenCodeSupervisor } from '../../src/services/opencode-supervisor'
describe('Health Routes', () => {
let healthApp: ReturnType<typeof createHealthRoutes>
@@ -113,5 +114,38 @@ describe('Health Routes', () => {
expect(json.status).toBe('unhealthy')
expect(json.error).toBe('Database error')
})
it('reads the supervisor status without triggering recovery', async () => {
mockDb.prepare().get.mockReturnValue({ 1: 1 })
;(opencodeServerManager.checkHealth as ReturnType<typeof vi.fn>).mockResolvedValue(true)
const supervisor = {
getStatus: vi.fn(() => ({
state: 'healthy',
healthy: true,
port: 5551,
version: '1.0.0',
minVersion: '1.0.137',
versionSupported: true,
lastError: null,
activeRecoveryAction: null,
attemptedRecoveryActions: [],
nextRecoveryAction: null,
failureCount: 0,
watching: true,
updatedAt: new Date().toISOString(),
})),
checkNow: vi.fn(),
} as unknown as OpenCodeSupervisor
healthApp = createHealthRoutes(mockDb, supervisor)
const res = await healthApp.fetch(new Request('http://localhost/'))
const processesRes = await healthApp.fetch(new Request('http://localhost/processes'))
expect(res.status).toBe(200)
expect(processesRes.status).toBe(200)
expect(supervisor.getStatus).toHaveBeenCalledTimes(2)
expect(supervisor.checkNow).not.toHaveBeenCalled()
expect(opencodeServerManager.checkHealth).toHaveBeenCalledTimes(2)
})
})
})
@@ -35,6 +35,7 @@ vi.mock('@opencode-manager/shared/config/env', () => ({
AUTH: { TRUSTED_ORIGINS: 'http://localhost:5173', SECRET: 'test-secret-for-encryption-key-32c' },
WORKSPACE: { BASE_PATH: '/test/workspace', REPOS_DIR: 'repos', CONFIG_DIR: 'config', AUTH_FILE: 'auth.json' },
OPENCODE: { PORT: 5551, HOST: '127.0.0.1', SERVER_PASSWORD: '', SERVER_USERNAME: 'opencode', PUBLIC_URL: '' },
TIMEOUTS: { HEALTH_CHECK_TIMEOUT_MS: 50 },
DATABASE: { PATH: ':memory:' },
FILE_LIMITS: {
MAX_SIZE_BYTES: 1024 * 1024,
@@ -394,6 +395,23 @@ describe('OpenCodeServerManager - reloadConfig', () => {
})
describe('OpenCodeServerManager - checkHealth', () => {
it('uses the OpenCode liveness endpoint', async () => {
const { opencodeServerManager } = await import('../../src/services/opencode-single-server')
const { createStubOpenCodeClient } = await import('../helpers/stub-opencode-client')
const forward = vi.fn(async () => new Response(JSON.stringify({ healthy: true }), { status: 200 }))
const timeout = vi.spyOn(AbortSignal, 'timeout')
opencodeServerManager.setOpenCodeClient(createStubOpenCodeClient({ forward }))
await opencodeServerManager.checkHealth()
expect(forward).toHaveBeenCalledWith(expect.objectContaining({
method: 'GET',
path: '/global/health',
signal: expect.any(AbortSignal),
}))
expect(timeout).toHaveBeenCalledWith(ENV.TIMEOUTS.HEALTH_CHECK_TIMEOUT_MS)
})
it('returns false when the upstream times out and aborts the upstream fetch', async () => {
const { opencodeServerManager } = await import('../../src/services/opencode-single-server')
const { createStubOpenCodeClient } = await import('../helpers/stub-opencode-client')
@@ -132,7 +132,7 @@ describe('OpenCodeSupervisor', () => {
manager.checkHealth.mockResolvedValueOnce(false)
const status = await supervisor.checkNow('api_probe')
const status = await supervisor.checkNow('manual')
expect(status.state).toBe('unhealthy')
expect(status.failureCount).toBe(1)
@@ -152,7 +152,7 @@ describe('OpenCodeSupervisor', () => {
.mockResolvedValueOnce(false)
.mockResolvedValueOnce(true)
const status = await supervisor.checkNow('api_probe')
const status = await supervisor.checkNow('manual')
expect(status.healthy).toBe(true)
expect(ensureDirectoryExists).toHaveBeenCalled()
@@ -167,7 +167,7 @@ describe('OpenCodeSupervisor', () => {
manager.isOperationInProgress.mockReturnValue(true)
await supervisor.checkNow('api_probe')
await supervisor.checkNow('manual')
expect(manager.checkHealth).not.toHaveBeenCalled()
})
-1
View File
@@ -24,7 +24,6 @@ services:
- WORKSPACE_PATH=/workspace
- PROCESS_START_WAIT_MS=2000
- PROCESS_VERIFY_WAIT_MS=1000
- HEALTH_CHECK_INTERVAL_MS=5000
- HEALTH_CHECK_TIMEOUT_MS=30000
- MAX_FILE_SIZE_MB=50
- MAX_UPLOAD_SIZE_MB=50
-1
View File
@@ -56,7 +56,6 @@ services:
- WORKSPACE_PATH=/workspace
- PROCESS_START_WAIT_MS=2000
- PROCESS_VERIFY_WAIT_MS=1000
- HEALTH_CHECK_INTERVAL_MS=5000
- HEALTH_CHECK_TIMEOUT_MS=30000
- MAX_FILE_SIZE_MB=50
- MAX_UPLOAD_SIZE_MB=50
+1 -2
View File
@@ -114,8 +114,7 @@ When configured, users can enable push notifications in Settings → Notificatio
|----------|-------------|---------|
| `PROCESS_START_WAIT_MS` | Wait time for OpenCode process to start | `2000` |
| `PROCESS_VERIFY_WAIT_MS` | Wait time for process health verification | `1000` |
| `HEALTH_CHECK_INTERVAL_MS` | Health check polling interval | `5000` |
| `HEALTH_CHECK_TIMEOUT_MS` | Health check timeout | `30000` |
| `HEALTH_CHECK_TIMEOUT_MS` | OpenCode liveness probe timeout | `30000` |
## File Limits
-1
View File
@@ -34,7 +34,6 @@ export const DEFAULTS = {
TIMEOUTS: {
PROCESS_START_WAIT_MS: 2000,
PROCESS_VERIFY_WAIT_MS: 1000,
HEALTH_CHECK_INTERVAL_MS: 5000,
HEALTH_CHECK_TIMEOUT_MS: 30000,
},
-1
View File
@@ -90,7 +90,6 @@ export const ENV = {
TIMEOUTS: {
PROCESS_START_WAIT_MS: getEnvNumber('PROCESS_START_WAIT_MS', DEFAULTS.TIMEOUTS.PROCESS_START_WAIT_MS),
PROCESS_VERIFY_WAIT_MS: getEnvNumber('PROCESS_VERIFY_WAIT_MS', DEFAULTS.TIMEOUTS.PROCESS_VERIFY_WAIT_MS),
HEALTH_CHECK_INTERVAL_MS: getEnvNumber('HEALTH_CHECK_INTERVAL_MS', DEFAULTS.TIMEOUTS.HEALTH_CHECK_INTERVAL_MS),
HEALTH_CHECK_TIMEOUT_MS: getEnvNumber('HEALTH_CHECK_TIMEOUT_MS', DEFAULTS.TIMEOUTS.HEALTH_CHECK_TIMEOUT_MS),
},