graceful exit on SIGTERM (#2178)

Much easier than convincing all users to change the default signal in
their service definition file to SIGINT.
This commit is contained in:
Ștefan Talpalaru 2020-12-14 17:45:14 +01:00 committed by GitHub
parent 14c5d6db1e
commit 9daf6be73c
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
1 changed files with 13 additions and 0 deletions

View File

@ -9,6 +9,7 @@ import
# Standard library
std/[os, tables, strutils, strformat, sequtils, times, math,
terminal, osproc, random],
system/ansi_c,
# Nimble packages
stew/[objects, byteutils, endians2, io2], stew/shims/macros,
@ -829,6 +830,12 @@ proc run*(node: BeaconNode) =
notice "Shutting down after having received SIGINT"
bnStatus = BeaconNodeStatus.Stopping
setControlCHook(controlCHandler)
# equivalent SIGTERM handler
when defined(posix):
proc SIGTERMHandler(signal: cint) {.noconv.} =
notice "Shutting down after having received SIGTERM"
bnStatus = BeaconNodeStatus.Stopping
c_signal(SIGTERM, SIGTERMHandler)
# main event loop
while bnStatus == BeaconNodeStatus.Running:
@ -1212,6 +1219,12 @@ programMain:
notice "Shutting down after having received SIGINT"
quit 0
setControlCHook(exitImmediatelyOnCtrlC)
# equivalent SIGTERM handler
when defined(posix):
proc exitImmediatelyOnSIGTERM(signal: cint) {.noconv.} =
notice "Shutting down after having received SIGTERM"
quit 0
c_signal(SIGTERM, exitImmediatelyOnSIGTERM)
if config.eth2Network.isSome:
let metadata = getMetadataForNetwork(config.eth2Network.get)