Implement graceful shutdown for `wakunode2` and `chat2` (#490)

* Implement graceful shutdown

* Consistent use of QuitSuccess
This commit is contained in:
Hanno Cornelius 2021-04-15 10:18:14 +02:00 committed by GitHub
parent 17cfc32c4f
commit 900d53f9df
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 34 additions and 11 deletions

View File

@ -69,6 +69,7 @@ This will bypass the random peer selection process and connect to the specified
| `/help` | displays available in-chat commands |
| `/connect` | interactively connect to a new peer |
| `/nick` | change nickname for current chat session |
| `/exit` | exits the current chat session |
## `chat2` message protobuf format

View File

@ -30,6 +30,7 @@ const Help = """
help: Prints this help
connect: dials a remote peer
nick: change nickname for current chat session
exit: exits chat session
"""
const
@ -203,14 +204,11 @@ proc writeAndPrint(c: Chat) {.async.} =
c.nick = await readNick(c.transp)
echo "You are now known as " & c.nick
# elif line.startsWith("/exit"):
# if p.connected and p.conn.closed.not:
# await p.conn.close()
# p.connected = false
#
# await p.switch.stop()
# echo "quitting..."
# quit(0)
elif line.startsWith("/exit"):
await c.node.stop()
echo "quitting..."
quit(QuitSuccess)
else:
# XXX connected state problematic
if c.started:
@ -368,6 +366,7 @@ proc processInput(rfd: AsyncFD, rng: ref BrHmacDrbgContext) {.async.} =
node.subscribe(topic, handler)
await chat.readWriteLoop()
runForever()
#await allFuturesThrowing(libp2pFuts)

View File

@ -417,9 +417,6 @@ proc mountRelay*(node: WakuNode, topics: seq[string] = newSeq[string](), rlnRela
node.wakuRelay = wakuRelay
node.switch.mount(wakuRelay)
# Reonnect to previous relay peers
waitFor node.peerManager.reconnectPeers(WakuRelayCodec)
info "mounting relay"
node.subscribe(defaultTopic, none(TopicHandler))
@ -441,6 +438,9 @@ proc mountRelay*(node: WakuNode, topics: seq[string] = newSeq[string](), rlnRela
waitFor node.wakuRelay.start()
info "relay mounted and started successfully"
# Reconnect to previous relay peers
waitFor node.peerManager.reconnectPeers(WakuRelayCodec)
## Helpers
proc dialPeer*(n: WakuNode, address: string) {.async.} =
@ -496,6 +496,7 @@ proc connectToNodes*(n: WakuNode, nodes: seq[PeerInfo]) {.async.} =
when isMainModule:
import
system/ansi_c,
confutils, json_rpc/rpcserver, metrics,
./config,
./jsonrpc/[admin_api,
@ -646,5 +647,27 @@ when isMainModule:
if conf.metricsServer:
startMetricsServer(conf.metricsServerAddress,
Port(conf.metricsServerPort + conf.portsShift))
# Setup graceful shutdown
# Handle Ctrl-C SIGINT
proc handleCtrlC() {.noconv.} =
when defined(windows):
# workaround for https://github.com/nim-lang/Nim/issues/4057
setupForeignThreadGc()
info "Shutting down after receiving SIGINT"
waitFor node.stop()
quit(QuitSuccess)
setControlCHook(handleCtrlC)
# Handle SIGTERM
when defined(posix):
proc handleSigterm(signal: cint) {.noconv.} =
info "Shutting down after receiving SIGTERM"
waitFor node.stop()
quit(QuitSuccess)
c_signal(SIGTERM, handleSigterm)
runForever()