Skip to content
Snippets Groups Projects
Unverified Commit e9e1b662 authored by Angelo Capossele's avatar Angelo Capossele Committed by GitHub
Browse files

Make WaitToKillTimeInSeconds a parameter (#570)

* :white_check_mark: Make WaitToKillTimeInSeconds a parameter

* :wrench: Set default ParaWaitToKill to 60 seconds

* :bug: Fix bug
parent e789dd80
Branches
Tags
No related merge requests found
...@@ -29,6 +29,7 @@ var PLUGINS = node.Plugins( ...@@ -29,6 +29,7 @@ var PLUGINS = node.Plugins(
config.Plugin(), config.Plugin(),
logger.Plugin(), logger.Plugin(),
cli.Plugin(), cli.Plugin(),
gracefulshutdown.Plugin(),
portcheck.Plugin(), portcheck.Plugin(),
profiling.Plugin(), profiling.Plugin(),
database.Plugin(), database.Plugin(),
...@@ -39,7 +40,6 @@ var PLUGINS = node.Plugins( ...@@ -39,7 +40,6 @@ var PLUGINS = node.Plugins(
issuer.Plugin(), issuer.Plugin(),
bootstrap.Plugin(), bootstrap.Plugin(),
sync.Plugin(), sync.Plugin(),
gracefulshutdown.Plugin(),
metrics.Plugin(), metrics.Plugin(),
drng.Plugin(), drng.Plugin(),
faucet.App(), faucet.App(),
......
package gracefulshutdown
import (
flag "github.com/spf13/pflag"
)
const (
// CfgWaitToKillTimeInSeconds the maximum amount of time to wait for background processes to terminate.
CfgWaitToKillTimeInSeconds = "gracefulshutdown.waitToKillTime"
)
func init() {
flag.Int(CfgWaitToKillTimeInSeconds, 60, "the maximum amount of time to wait for background processes to terminate, in seconds")
}
...@@ -9,6 +9,7 @@ import ( ...@@ -9,6 +9,7 @@ import (
"syscall" "syscall"
"time" "time"
"github.com/iotaledger/goshimmer/plugins/config"
"github.com/iotaledger/hive.go/daemon" "github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/logger"
"github.com/iotaledger/hive.go/node" "github.com/iotaledger/hive.go/node"
...@@ -17,59 +18,60 @@ import ( ...@@ -17,59 +18,60 @@ import (
// PluginName is the name of the graceful shutdown plugin. // PluginName is the name of the graceful shutdown plugin.
const PluginName = "Graceful Shutdown" const PluginName = "Graceful Shutdown"
// WaitToKillTimeInSeconds is the maximum amount of time to wait for background processes to terminate.
// After that the process is killed.
const WaitToKillTimeInSeconds = 60
var ( var (
// plugin is the plugin instance of the graceful shutdown plugin. // plugin is the plugin instance of the graceful shutdown plugin.
plugin *node.Plugin plugin *node.Plugin
once sync.Once once sync.Once
log *logger.Logger log *logger.Logger
gracefulStop chan os.Signal gracefulStop chan os.Signal
waitToKillTimeInSeconds int
) )
// Plugin gets the plugin instance. func configure(*node.Plugin) {
func Plugin() *node.Plugin { waitToKillTimeInSeconds = config.Node().GetInt(CfgWaitToKillTimeInSeconds)
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Enabled, func(plugin *node.Plugin) { log = logger.NewLogger(PluginName)
log = logger.NewLogger(PluginName) gracefulStop = make(chan os.Signal)
gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM) signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT) signal.Notify(gracefulStop, syscall.SIGINT)
go func() { go func() {
<-gracefulStop <-gracefulStop
log.Warnf("Received shutdown request - waiting (max %d) to finish processing ...", WaitToKillTimeInSeconds) log.Warnf("Received shutdown request - waiting (max %d) to finish processing ...", waitToKillTimeInSeconds)
go func() { go func() {
ticker := time.NewTicker(1 * time.Second) ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop() defer ticker.Stop()
start := time.Now() start := time.Now()
for x := range ticker.C { for x := range ticker.C {
secondsSinceStart := x.Sub(start).Seconds() secondsSinceStart := x.Sub(start).Seconds()
if secondsSinceStart <= WaitToKillTimeInSeconds { if secondsSinceStart <= float64(waitToKillTimeInSeconds) {
processList := "" processList := ""
runningBackgroundWorkers := daemon.GetRunningBackgroundWorkers() runningBackgroundWorkers := daemon.GetRunningBackgroundWorkers()
if len(runningBackgroundWorkers) >= 1 { if len(runningBackgroundWorkers) >= 1 {
sort.Strings(runningBackgroundWorkers) sort.Strings(runningBackgroundWorkers)
processList = "(" + strings.Join(runningBackgroundWorkers, ", ") + ") " processList = "(" + strings.Join(runningBackgroundWorkers, ", ") + ") "
}
log.Warnf("Received shutdown request - waiting (max %d seconds) to finish processing %s...", WaitToKillTimeInSeconds-int(secondsSinceStart), processList)
} else {
log.Error("Background processes did not terminate in time! Forcing shutdown ...")
os.Exit(1)
}
} }
}() log.Warnf("Received shutdown request - waiting (max %d seconds) to finish processing %s...", waitToKillTimeInSeconds-int(secondsSinceStart), processList)
} else {
log.Error("Background processes did not terminate in time! Forcing shutdown ...")
os.Exit(1)
}
}
}()
daemon.Shutdown() daemon.Shutdown()
}() }()
}) }
// Plugin gets the plugin instance.
func Plugin() *node.Plugin {
once.Do(func() {
plugin = node.NewPlugin(PluginName, node.Enabled, configure)
}) })
return plugin return plugin
} }
......
...@@ -86,6 +86,7 @@ func (d *DockerContainer) CreateGoShimmerPeer(config GoShimmerConfig) error { ...@@ -86,6 +86,7 @@ func (d *DockerContainer) CreateGoShimmerPeer(config GoShimmerConfig) error {
fmt.Sprintf("--autopeering.outboundUpdateIntervalMs=%d", ParaOutboundUpdateIntervalMs), fmt.Sprintf("--autopeering.outboundUpdateIntervalMs=%d", ParaOutboundUpdateIntervalMs),
fmt.Sprintf("--node.disablePlugins=%s", config.DisabledPlugins), fmt.Sprintf("--node.disablePlugins=%s", config.DisabledPlugins),
fmt.Sprintf("--pow.difficulty=%d", ParaPoWDifficulty), fmt.Sprintf("--pow.difficulty=%d", ParaPoWDifficulty),
fmt.Sprintf("--gracefulshutdown.waitToKillTime=%d", ParaWaitToKill),
fmt.Sprintf("--node.enablePlugins=%s", func() string { fmt.Sprintf("--node.enablePlugins=%s", func() string {
var plugins []string var plugins []string
if config.Bootstrap { if config.Bootstrap {
......
...@@ -35,6 +35,8 @@ var ( ...@@ -35,6 +35,8 @@ var (
ParaFaucetTokensPerRequest int64 = 1337 ParaFaucetTokensPerRequest int64 = 1337
// ParaPoWDifficulty defines the PoW difficulty. // ParaPoWDifficulty defines the PoW difficulty.
ParaPoWDifficulty = 2 ParaPoWDifficulty = 2
// ParaWaitToKill defines the time to wait before killing the node.
ParaWaitToKill = 60
) )
var ( var (
......
...@@ -27,13 +27,16 @@ func TestConsensusFiftyFiftyOpinionSplit(t *testing.T) { ...@@ -27,13 +27,16 @@ func TestConsensusFiftyFiftyOpinionSplit(t *testing.T) {
// override avg. network delay to accustom integration test slowness // override avg. network delay to accustom integration test slowness
backupFCoBAvgNetworkDelay := framework.ParaFCoBAverageNetworkDelay backupFCoBAvgNetworkDelay := framework.ParaFCoBAverageNetworkDelay
backupBootstrapOnEveryNode := framework.ParaBootstrapOnEveryNode backupBootstrapOnEveryNode := framework.ParaBootstrapOnEveryNode
backupParaWaitToKill := framework.ParaWaitToKill
framework.ParaFCoBAverageNetworkDelay = 90 framework.ParaFCoBAverageNetworkDelay = 90
framework.ParaBootstrapOnEveryNode = true framework.ParaBootstrapOnEveryNode = true
framework.ParaWaitToKill = 2 * framework.ParaFCoBAverageNetworkDelay
// reset framework paras // reset framework paras
defer func() { defer func() {
framework.ParaFCoBAverageNetworkDelay = backupFCoBAvgNetworkDelay framework.ParaFCoBAverageNetworkDelay = backupFCoBAvgNetworkDelay
framework.ParaBootstrapOnEveryNode = backupBootstrapOnEveryNode framework.ParaBootstrapOnEveryNode = backupBootstrapOnEveryNode
framework.ParaWaitToKill = backupParaWaitToKill
}() }()
// create two partitions with their own peers // create two partitions with their own peers
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment