mirror of
https://github.com/chriswritescode-dev/opencode-manager.git
synced 2026-12-05 20:34:18 +01:00
Add config rollback and startup error recovery
This commit is contained in:
1 parent
120d087910
commit
876b444599
10 files changed
+428
-69
No files matched your search
@@ -10,6 +10,7 @@ const OPENCODE_SERVER_PORT = ENV.OPENCODE.PORT
|
||||
const OPENCODE_SERVER_DIRECTORY = getWorkspacePath()
|
||||
const OPENCODE_CONFIG_PATH = getOpenCodeConfigFilePath()
|
||||
const MIN_OPENCODE_VERSION = '1.0.137'
|
||||
const MAX_STDERR_SIZE = 10240
|
||||
|
||||
function compareVersions(v1: string, v2: string): number {
|
||||
const parts1 = v1.split('.').map(Number)
|
||||
@@ -31,6 +32,7 @@ class OpenCodeServerManager {
|
||||
private isHealthy: boolean = false
|
||||
private db: Database | null = null
|
||||
private version: string | null = null
|
||||
private lastStartupError: string | null = null
|
||||
|
||||
private constructor() {}
|
||||
|
||||
@@ -105,13 +107,15 @@ class OpenCodeServerManager {
|
||||
|
||||
const gitEnv = gitToken ? createGitHubGitEnv(gitToken) : createNoPromptGitEnv()
|
||||
|
||||
let stderrOutput = ''
|
||||
|
||||
this.serverProcess = spawn(
|
||||
'opencode',
|
||||
'opencode',
|
||||
['serve', '--port', OPENCODE_SERVER_PORT.toString(), '--hostname', '127.0.0.1'],
|
||||
{
|
||||
cwd: OPENCODE_SERVER_DIRECTORY,
|
||||
detached: !isDevelopment,
|
||||
stdio: isDevelopment ? 'inherit' : 'ignore',
|
||||
stdio: isDevelopment ? 'inherit' : ['ignore', 'pipe', 'pipe'],
|
||||
env: {
|
||||
...process.env,
|
||||
...gitEnv,
|
||||
@@ -122,12 +126,32 @@ class OpenCodeServerManager {
|
||||
}
|
||||
)
|
||||
|
||||
if (!isDevelopment && this.serverProcess.stderr) {
|
||||
this.serverProcess.stderr.on('data', (data) => {
|
||||
stderrOutput += data.toString()
|
||||
if (stderrOutput.length > MAX_STDERR_SIZE) {
|
||||
stderrOutput = stderrOutput.slice(-MAX_STDERR_SIZE)
|
||||
}
|
||||
})
|
||||
}
|
||||
|
||||
this.serverProcess.on('exit', (code, signal) => {
|
||||
if (code !== null && code !== 0) {
|
||||
this.lastStartupError = `Server exited with code ${code}${stderrOutput ? `: ${stderrOutput.slice(-500)}` : ''}`
|
||||
logger.error('OpenCode server process exited:', this.lastStartupError)
|
||||
} else if (signal) {
|
||||
this.lastStartupError = `Server terminated by signal ${signal}`
|
||||
logger.error('OpenCode server process terminated:', this.lastStartupError)
|
||||
}
|
||||
})
|
||||
|
||||
this.serverPid = this.serverProcess.pid ?? null
|
||||
|
||||
logger.info(`OpenCode server started with PID ${this.serverPid}`)
|
||||
|
||||
const healthy = await this.waitForHealth(30000)
|
||||
if (!healthy) {
|
||||
this.lastStartupError = `Server failed to become healthy after 30s${stderrOutput ? `. Last error: ${stderrOutput.slice(-500)}` : ''}`
|
||||
throw new Error('OpenCode server failed to become healthy')
|
||||
}
|
||||
|
||||
@@ -191,6 +215,14 @@ class OpenCodeServerManager {
|
||||
return compareVersions(this.version, MIN_OPENCODE_VERSION) >= 0
|
||||
}
|
||||
|
||||
getLastStartupError(): string | null {
|
||||
return this.lastStartupError
|
||||
}
|
||||
|
||||
clearStartupError(): void {
|
||||
this.lastStartupError = null
|
||||
}
|
||||
|
||||
async checkHealth(): Promise<boolean> {
|
||||
try {
|
||||
const response = await fetch(`http://127.0.0.1:${OPENCODE_SERVER_PORT}/doc`, {
|
||||
|
||||
Reference in new issue
Block a user