abort Codex when startup sequence fails (#738)

This commit is contained in:
Giuliano Mega 2024-03-11 09:31:17 -03:00 committed by GitHub
parent 6ee1162989
commit 74679e9e2e
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 24 additions and 22 deletions

View File

@ -80,7 +80,7 @@ when isMainModule:
var var
state: CodexStatus state: CodexStatus
pendingFuts: seq[Future[void]] shutdown: Future[void]
let let
keyPath = keyPath =
@ -93,6 +93,12 @@ when isMainModule:
server = CodexServer.new(config, privateKey) server = CodexServer.new(config, privateKey)
## Ctrl+C handling ## Ctrl+C handling
proc doShutdown() =
shutdown = server.stop()
state = CodexStatus.Stopping
notice "Stopping Codex"
proc controlCHandler() {.noconv.} = proc controlCHandler() {.noconv.} =
when defined(windows): when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057 # workaround for https://github.com/nim-lang/Nim/issues/4057
@ -101,10 +107,7 @@ when isMainModule:
except Exception as exc: raiseAssert exc.msg # shouldn't happen except Exception as exc: raiseAssert exc.msg # shouldn't happen
notice "Shutting down after having received SIGINT" notice "Shutting down after having received SIGINT"
pendingFuts.add(server.stop()) doShutdown()
state = CodexStatus.Stopping
notice "Stopping Codex"
try: try:
setControlCHook(controlCHandler) setControlCHook(controlCHandler)
@ -116,26 +119,31 @@ when isMainModule:
proc SIGTERMHandler(signal: cint) {.noconv.} = proc SIGTERMHandler(signal: cint) {.noconv.} =
notice "Shutting down after having received SIGTERM" notice "Shutting down after having received SIGTERM"
pendingFuts.add(server.stop()) doShutdown()
state = CodexStatus.Stopping
notice "Stopping Codex"
c_signal(ansi_c.SIGTERM, SIGTERMHandler) c_signal(ansi_c.SIGTERM, SIGTERMHandler)
pendingFuts.add(server.start()) try:
waitFor server.start()
except CatchableError as error:
error "Codex failed to start", error = error.msg
# XXX ideally we'd like to issue a stop instead of quitting cold turkey,
# but this would mean we'd have to fix the implementation of all
# services so they won't crash if we attempt to stop them before they
# had a chance to start (currently you'll get a SISGSEV if you try to).
quit QuitFailure
state = CodexStatus.Running state = CodexStatus.Running
while state == CodexStatus.Running: while state == CodexStatus.Running:
# poll chronos # poll chronos
chronos.poll() chronos.poll()
# wait fot futures to finish try:
let res = waitFor allFinished(pendingFuts) # signal handlers guarantee that the shutdown Future will
state = CodexStatus.Stopped # be assigned before state switches to Stopping
waitFor shutdown
if res.anyIt( it.failed ): except CatchableError as error:
error "Codex didn't shutdown correctly" error "Codex didn't shutdown correctly", error = error.msg
quit QuitFailure quit QuitFailure
notice "Exited codex" notice "Exited codex"

View File

@ -46,7 +46,6 @@ logScope:
type type
CodexServer* = ref object CodexServer* = ref object
runHandle: Future[void]
config: CodexConf config: CodexConf
restServer: RestServerRef restServer: RestServerRef
codexNode: CodexNodeRef codexNode: CodexNodeRef
@ -183,9 +182,6 @@ proc start*(s: CodexServer) {.async.} =
await s.codexNode.start() await s.codexNode.start()
s.restServer.start() s.restServer.start()
s.runHandle = newFuture[void]("codex.runHandle")
await s.runHandle
proc stop*(s: CodexServer) {.async.} = proc stop*(s: CodexServer) {.async.} =
notice "Stopping codex node" notice "Stopping codex node"
@ -196,8 +192,6 @@ proc stop*(s: CodexServer) {.async.} =
s.repoStore.stop(), s.repoStore.stop(),
s.maintenance.stop()) s.maintenance.stop())
s.runHandle.complete()
proc new*( proc new*(
T: type CodexServer, T: type CodexServer,
config: CodexConf, config: CodexConf,