Fix process hanging after restart

This commit is contained in:
Eric 2025-03-06 17:27:00 +11:00
parent dff1ca961f
commit 919f46b2e1
No known key found for this signature in database
2 changed files with 28 additions and 8 deletions

View File

@ -7,6 +7,7 @@ import pkg/ethers
import pkg/libp2p
import std/os
import std/strutils
import std/times
import codex/conf
import ./codexclient
import ./nodeprocess
@ -78,6 +79,9 @@ proc apiUrl*(node: CodexProcess): string {.raises: [CodexProcessError].} =
let config = node.config
return "http://" & config.apiBindAddress & ":" & $config.apiPort & "/api/codex/v1"
proc logFile*(node: CodexProcess): ?string {.raises: [CodexProcessError].} =
node.config.logFile
proc client*(node: CodexProcess): CodexClient {.raises: [CodexProcessError].} =
if client =? node.client:
return client
@ -85,6 +89,25 @@ proc client*(node: CodexProcess): CodexClient {.raises: [CodexProcessError].} =
node.client = some client
return client
proc updateLogFile(node: CodexProcess, newLogFile: string) =
for arg in node.arguments.mitems:
if arg.startsWith("--log-file="):
arg = "--log-file=" & newLogFile
break
method restart*(node: CodexProcess) {.async.} =
trace "restarting codex"
await node.stop()
if logFile =? node.logFile:
# chronicles truncates the existing log file on start, so changed the log
# file cli param to create a new one
node.updateLogFile(
logFile & "_restartedAt_" & now().format("yyyy-MM-dd'_'HH-mm-ss") & ".log"
)
await node.start()
await node.waitUntilStarted()
trace "codex process restarted"
method stop*(node: CodexProcess) {.async: (raises: []).} =
logScope:
nodeName = node.name

View File

@ -137,13 +137,10 @@ method stop*(
error = e.msg
writeStackTrace()
finally:
proc closeProcessStreams() {.async: (raises: []).} =
trace "closing node process' streams"
await node.process.closeWait()
node.process = nil
trace "node process' streams closed"
asyncSpawn closeProcessStreams()
trace "closing node process' streams"
await node.process.closeWait()
node.process = nil
trace "node process' streams closed"
proc waitUntilOutput*(
node: NodeProcess, output: string
@ -184,7 +181,7 @@ proc waitUntilStarted*(
raise
newException(NodeProcessError, "node did not output '" & node.startedOutput & "'")
proc restart*(node: NodeProcess) {.async.} =
method restart*(node: NodeProcess) {.base, async.} =
await node.stop()
await node.start()
await node.waitUntilStarted()