Skip to content

Commit 2cf2c69

Browse files
author
pmmiranda
committed
Added graceful shutdown
1 parent e0ae399 commit 2cf2c69

File tree

3 files changed

+32
-24
lines changed

3 files changed

+32
-24
lines changed

nimbus/consensus/consensus_layer.nim

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,9 @@ import
2222
logScope:
2323
topics = "Consensus layer"
2424

25+
proc shutdownConsensus*() =
26+
bnStatus = BeaconNodeStatus.Stopping
27+
2528
proc makeConfig*(
2629
cmdCommandList: seq[string], ConfType: type
2730
): Result[ConfType, string] =

nimbus/execution/execution_layer.nim

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -20,6 +20,11 @@ import
2020
logScope:
2121
topics = "Execution layer"
2222

23+
var nimbusHandler = NimbusNode()
24+
25+
proc shutdownExecution*() =
26+
nimbusHandler.state = NimbusState.Stopping
27+
2328
## Execution Layer handler
2429
proc executionLayerHandler*(channel: ptr Channel[pointer]) =
2530
var p: pointer
@@ -38,7 +43,7 @@ proc executionLayerHandler*(channel: ptr Channel[pointer]) =
3843

3944
try:
4045
{.gcsafe.}:
41-
var nimbusHandler = NimbusNode(state: NimbusState.Starting, ctx: newEthContext())
46+
nimbusHandler = NimbusNode(state: NimbusState.Starting, ctx: newEthContext())
4247
let conf = makeConfig(parametersList)
4348
nimbusHandler.run(conf)
4449
except [CatchableError, OSError, IOError, CancelledError, MetricsError]:

nimbus/nimbus.nim

Lines changed: 23 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -81,16 +81,11 @@ proc startService(nimbus: var Nimbus, service: var NimbusService) =
8181
## Gracefully exits all services
8282
proc monitorServices(nimbus: Nimbus) =
8383
for service in nimbus.serviceList:
84-
if service.serviceHandler.running():
85-
joinThread(service.serviceHandler)
86-
info "Exited service ", service = service.name
84+
joinThread(service.serviceHandler)
85+
info "Exited service ", service = service.name
8786

8887
notice "Exited all services"
8988

90-
# ------------------------------------------------------------------------------
91-
# Public
92-
# ------------------------------------------------------------------------------
93-
9489
# aux function to prepare arguments and options for eth1 and eth2
9590
func addArg(
9691
paramTable: var NimbusConfigTable, cmdKind: CmdLineKind, key: string, arg: string
@@ -110,6 +105,22 @@ func addArg(
110105

111106
paramTable[newKey] = newArg
112107

108+
proc controlCHandler() {.noconv.} =
109+
when defined(windows):
110+
# workaround for https://github.com/nim-lang/Nim/issues/4057
111+
try:
112+
setupForeignThreadGc()
113+
except NimbusServiceError as exc:
114+
raiseAssert exc.msg # shouldn't happen
115+
116+
notice "\tCtrl+C pressed. Shutting down services ..."
117+
shutdownExecution()
118+
shutdownConsensus()
119+
120+
# ------------------------------------------------------------------------------
121+
# Public
122+
# ------------------------------------------------------------------------------
123+
113124
# Setup services
114125
proc setup*(nimbus: var Nimbus) =
115126
let
@@ -158,6 +169,10 @@ proc run*(nimbus: var Nimbus) =
158169
fatal "error starting service:", msg = e.msg
159170
quit QuitFailure
160171

172+
# handling Ctrl+C signal
173+
# note: do not move. Both execution and consensus clients create these handlers.
174+
setControlCHook(controlCHandler)
175+
161176
## wait for shutdown
162177
nimbus.monitorServices()
163178

@@ -167,22 +182,7 @@ when isMainModule:
167182

168183
setupFileLimits()
169184

170-
var nimbus: Nimbus = Nimbus.new
171-
172-
## Graceful shutdown by handling of Ctrl+C signal
173-
proc controlCHandler() {.noconv.} =
174-
when defined(windows):
175-
# workaround for https://github.com/nim-lang/Nim/issues/4057
176-
try:
177-
setupForeignThreadGc()
178-
except NimbusServiceError as exc:
179-
raiseAssert exc.msg # shouldn't happen
180-
181-
notice "\tCtrl+C pressed. Shutting down services ..."
182-
quit 0
183-
184-
setControlCHook(controlCHandler)
185-
185+
var nimbus = Nimbus()
186186
nimbus.setup()
187187
nimbus.run()
188188

0 commit comments

Comments
 (0)