mirror of https://github.com/waku-org/nwaku.git
Implement graceful shutdown for `wakunode2` and `chat2` (#490)
* Implement graceful shutdown * Consistent use of QuitSuccess
This commit is contained in:
parent
17cfc32c4f
commit
900d53f9df
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
|
||||
|
|
|
@ -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))
|
||||
|
@ -442,6 +439,9 @@ proc mountRelay*(node: WakuNode, topics: seq[string] = newSeq[string](), rlnRela
|
|||
|
||||
info "relay mounted and started successfully"
|
||||
|
||||
# Reconnect to previous relay peers
|
||||
waitFor node.peerManager.reconnectPeers(WakuRelayCodec)
|
||||
|
||||
## Helpers
|
||||
proc dialPeer*(n: WakuNode, address: string) {.async.} =
|
||||
info "dialPeer", address = address
|
||||
|
@ -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,
|
||||
|
@ -647,4 +648,26 @@ when isMainModule:
|
|||
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()
|
||||
|
|
Loading…
Reference in New Issue