parent
0beeefd760
commit
7907d67797
40
codex.nim
40
codex.nim
|
@ -19,6 +19,7 @@ import ./codex/utils/keyutils
|
||||||
export codex, conf, libp2p, chronos, chronicles
|
export codex, conf, libp2p, chronos, chronicles
|
||||||
|
|
||||||
when isMainModule:
|
when isMainModule:
|
||||||
|
import std/sequtils
|
||||||
import std/os
|
import std/os
|
||||||
import pkg/confutils/defs
|
import pkg/confutils/defs
|
||||||
import ./codex/utils/fileutils
|
import ./codex/utils/fileutils
|
||||||
|
@ -29,6 +30,12 @@ when isMainModule:
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
import system/ansi_c
|
import system/ansi_c
|
||||||
|
|
||||||
|
type
|
||||||
|
CodexStatus {.pure.} = enum
|
||||||
|
Stopped,
|
||||||
|
Stopping,
|
||||||
|
Running
|
||||||
|
|
||||||
let config = CodexConf.load(
|
let config = CodexConf.load(
|
||||||
version = codexFullVersion
|
version = codexFullVersion
|
||||||
)
|
)
|
||||||
|
@ -59,6 +66,10 @@ when isMainModule:
|
||||||
|
|
||||||
trace "Repo dir initialized", dir = config.dataDir / "repo"
|
trace "Repo dir initialized", dir = config.dataDir / "repo"
|
||||||
|
|
||||||
|
var
|
||||||
|
state: CodexStatus
|
||||||
|
pendingFuts: seq[Future[void]]
|
||||||
|
|
||||||
let
|
let
|
||||||
keyPath =
|
keyPath =
|
||||||
if isAbsolute(string config.netPrivKeyFile):
|
if isAbsolute(string config.netPrivKeyFile):
|
||||||
|
@ -77,7 +88,11 @@ when isMainModule:
|
||||||
setupForeignThreadGc()
|
setupForeignThreadGc()
|
||||||
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"
|
||||||
waitFor server.stop()
|
|
||||||
|
pendingFuts.add(server.stop())
|
||||||
|
state = CodexStatus.Stopping
|
||||||
|
|
||||||
|
notice "Stopping Codex"
|
||||||
|
|
||||||
try:
|
try:
|
||||||
setControlCHook(controlCHandler)
|
setControlCHook(controlCHandler)
|
||||||
|
@ -88,12 +103,29 @@ when isMainModule:
|
||||||
when defined(posix):
|
when defined(posix):
|
||||||
proc SIGTERMHandler(signal: cint) {.noconv.} =
|
proc SIGTERMHandler(signal: cint) {.noconv.} =
|
||||||
notice "Shutting down after having received SIGTERM"
|
notice "Shutting down after having received SIGTERM"
|
||||||
waitFor server.stop()
|
|
||||||
notice "Stopped Codex"
|
pendingFuts.add(server.stop())
|
||||||
|
state = CodexStatus.Stopping
|
||||||
|
|
||||||
|
notice "Stopping Codex"
|
||||||
|
|
||||||
c_signal(ansi_c.SIGTERM, SIGTERMHandler)
|
c_signal(ansi_c.SIGTERM, SIGTERMHandler)
|
||||||
|
|
||||||
waitFor server.start()
|
pendingFuts.add(server.start())
|
||||||
|
|
||||||
|
state = CodexStatus.Running
|
||||||
|
while state == CodexStatus.Running:
|
||||||
|
# poll chronos
|
||||||
|
chronos.poll()
|
||||||
|
|
||||||
|
# wait fot futures to finish
|
||||||
|
let res = waitFor allFinished(pendingFuts)
|
||||||
|
state = CodexStatus.Stopped
|
||||||
|
|
||||||
|
if res.anyIt( it.failed ):
|
||||||
|
error "Codex didn't shutdown correctly"
|
||||||
|
quit QuitFailure
|
||||||
|
|
||||||
notice "Exited codex"
|
notice "Exited codex"
|
||||||
|
|
||||||
of StartUpCommand.initNode:
|
of StartUpCommand.initNode:
|
||||||
|
|
Loading…
Reference in New Issue