From 99a435626b2933eabd3e3e52e68ed3567efe35f6 Mon Sep 17 00:00:00 2001 From: Angelo Capossele <angelocapossele@gmail.com> Date: Tue, 2 Jun 2020 11:47:30 +0100 Subject: [PATCH] Add panic if backgroundWorker fails (#444) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit * :scream: Add panic if backgroundWorker fails * 🎨 Use log.Panicf instead of panic --- dapps/valuetransfers/dapp.go | 6 ++++-- dapps/valuetransfers/fpc.go | 12 ++++++++---- plugins/analysis/client/plugin.go | 2 +- .../analysis/dashboard/autopeering_feed.go | 2 +- plugins/analysis/dashboard/fpc_livefeed.go | 2 +- plugins/analysis/dashboard/plugin.go | 2 +- plugins/analysis/dashboard/recorded_events.go | 6 ++++-- plugins/analysis/server/plugin.go | 2 +- plugins/autopeering/plugin.go | 2 +- plugins/bootstrap/plugin.go | 6 ++++-- plugins/dashboard/drng_livefeed.go | 6 ++++-- plugins/dashboard/livefeed.go | 6 ++++-- plugins/dashboard/plugin.go | 2 +- plugins/dashboard/visualizer.go | 6 ++++-- plugins/dashboard/ws.go | 6 ++++-- plugins/database/plugin.go | 2 +- plugins/gossip/plugin.go | 2 +- plugins/messagelayer/plugin.go | 12 ++++++++---- plugins/metrics/plugin.go | 19 ++++++++++++------- plugins/remotelog/plugin.go | 6 ++++-- plugins/sync/plugin.go | 12 ++++++++---- plugins/webapi/plugin.go | 2 +- plugins/webapi/spammer/plugin.go | 10 ++++++++-- 23 files changed, 86 insertions(+), 47 deletions(-) diff --git a/dapps/valuetransfers/dapp.go b/dapps/valuetransfers/dapp.go index 6aaf256b..aa244304 100644 --- a/dapps/valuetransfers/dapp.go +++ b/dapps/valuetransfers/dapp.go @@ -99,10 +99,12 @@ func configure(_ *node.Plugin) { } func run(*node.Plugin) { - _ = daemon.BackgroundWorker("Tangle", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Tangle", func(shutdownSignal <-chan struct{}) { <-shutdownSignal Tangle.Shutdown() - }, shutdown.PriorityTangle) + }, shutdown.PriorityTangle); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } runFPC() } diff --git a/dapps/valuetransfers/fpc.go b/dapps/valuetransfers/fpc.go index d8daf02a..dbbaa8d3 100644 --- a/dapps/valuetransfers/fpc.go +++ b/dapps/valuetransfers/fpc.go @@ -99,7 +99,7 @@ func configureFPC() { } func runFPC() { - daemon.BackgroundWorker("FPCVoterServer", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("FPCVoterServer", func(shutdownSignal <-chan struct{}) { voterServer = votenet.New(Voter(), func(id string) vote.Opinion { branchID, err := branchmanager.BranchIDFromBase58(id) if err != nil { @@ -133,9 +133,11 @@ func runFPC() { <-shutdownSignal voterServer.Shutdown() log.Info("Stopped vote server") - }, shutdown.PriorityFPC) + }, shutdown.PriorityFPC); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } - daemon.BackgroundWorker("FPCRoundsInitiator", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("FPCRoundsInitiator", func(shutdownSignal <-chan struct{}) { log.Infof("Started FPC round initiator") unixTsPRNG := prng.NewUnixTimestampPRNG(roundIntervalSeconds) defer unixTsPRNG.Stop() @@ -151,7 +153,9 @@ func runFPC() { } } log.Infof("Stopped FPC round initiator") - }, shutdown.PriorityFPC) + }, shutdown.PriorityFPC); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } // PeerOpinionGiver implements the OpinionGiver interface based on a peer. diff --git a/plugins/analysis/client/plugin.go b/plugins/analysis/client/plugin.go index e434a462..9e975f73 100644 --- a/plugins/analysis/client/plugin.go +++ b/plugins/analysis/client/plugin.go @@ -61,7 +61,7 @@ func run(_ *node.Plugin) { } } }, shutdown.PriorityAnalysis); err != nil { - log.Panic(err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/analysis/dashboard/autopeering_feed.go b/plugins/analysis/dashboard/autopeering_feed.go index acab58b3..431061f4 100644 --- a/plugins/analysis/dashboard/autopeering_feed.go +++ b/plugins/analysis/dashboard/autopeering_feed.go @@ -119,7 +119,7 @@ func runAutopeeringFeed() { autopeeringWorkerPool.Stop() log.Info("Stopping Analysis-Dashboard[AutopeeringVisualizer] ... done") }, shutdown.PriorityDashboard); err != nil { - panic(err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/analysis/dashboard/fpc_livefeed.go b/plugins/analysis/dashboard/fpc_livefeed.go index d1508f09..2c4e58bb 100644 --- a/plugins/analysis/dashboard/fpc_livefeed.go +++ b/plugins/analysis/dashboard/fpc_livefeed.go @@ -67,7 +67,7 @@ func runFPCLiveFeed() { analysis.Events.FPCHeartbeat.Detach(onFPCHeartbeatReceived) log.Info("Stopping Analysis[FPCUpdater] ... done") }, shutdown.PriorityDashboard); err != nil { - panic(err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/analysis/dashboard/plugin.go b/plugins/analysis/dashboard/plugin.go index 99e1b7ae..0d3817b5 100644 --- a/plugins/analysis/dashboard/plugin.go +++ b/plugins/analysis/dashboard/plugin.go @@ -63,7 +63,7 @@ func run(*node.Plugin) { log.Infof("Starting %s ...", PluginName) if err := daemon.BackgroundWorker(PluginName, worker, shutdown.PriorityAnalysis); err != nil { - log.Errorf("Error starting as daemon: %s", err) + log.Panicf("Error starting as daemon: %s", err) } } diff --git a/plugins/analysis/dashboard/recorded_events.go b/plugins/analysis/dashboard/recorded_events.go index 018f839b..d0174bc7 100644 --- a/plugins/analysis/dashboard/recorded_events.go +++ b/plugins/analysis/dashboard/recorded_events.go @@ -110,7 +110,7 @@ func configureEventsRecording() { // starts record manager that initiates a record cleanup periodically func runEventsRecordManager() { - _ = daemon.BackgroundWorker("Dashboard Analysis Server Record Manager", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Dashboard Analysis Server Record Manager", func(shutdownSignal <-chan struct{}) { ticker := time.NewTicker(cleanUpPeriod) defer ticker.Stop() for { @@ -121,7 +121,9 @@ func runEventsRecordManager() { cleanUp(cleanUpPeriod) } } - }, shutdown.PriorityAnalysis) + }, shutdown.PriorityAnalysis); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } // removes nodes and links we haven't seen for at least 3 times the heartbeat interval. diff --git a/plugins/analysis/server/plugin.go b/plugins/analysis/server/plugin.go index ce938289..d874e300 100644 --- a/plugins/analysis/server/plugin.go +++ b/plugins/analysis/server/plugin.go @@ -69,7 +69,7 @@ func run(_ *node.Plugin) { log.Info("Stopping Server ...") server.Shutdown() }, shutdown.PriorityAnalysis); err != nil { - log.Panic(err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/autopeering/plugin.go b/plugins/autopeering/plugin.go index f9884bb4..74aa171c 100644 --- a/plugins/autopeering/plugin.go +++ b/plugins/autopeering/plugin.go @@ -30,7 +30,7 @@ func configure(*node.Plugin) { func run(*node.Plugin) { if err := daemon.BackgroundWorker(PluginName, start, shutdown.PriorityAutopeering); err != nil { - log.Errorf("Failed to start as daemon: %s", err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/bootstrap/plugin.go b/plugins/bootstrap/plugin.go index d6ce2e31..1ebfd520 100644 --- a/plugins/bootstrap/plugin.go +++ b/plugins/bootstrap/plugin.go @@ -51,7 +51,7 @@ func run(_ *node.Plugin) { issuancePeriod := time.Duration(issuancePeriodSec) * time.Second // issue messages on top of the genesis - _ = daemon.BackgroundWorker("Bootstrapping-Issuer", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Bootstrapping-Issuer", func(shutdownSignal <-chan struct{}) { messageSpammer.Start(initialIssuanceMPS) defer messageSpammer.Shutdown() // don't stop issuing messages if in continuous mode @@ -65,5 +65,7 @@ func run(_ *node.Plugin) { case <-time.After(issuancePeriod): case <-shutdownSignal: } - }, shutdown.PriorityBootstrap) + }, shutdown.PriorityBootstrap); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } diff --git a/plugins/dashboard/drng_livefeed.go b/plugins/dashboard/drng_livefeed.go index 9bd474ab..01d468ef 100644 --- a/plugins/dashboard/drng_livefeed.go +++ b/plugins/dashboard/drng_livefeed.go @@ -40,7 +40,7 @@ func configureDrngLiveFeed() { } func runDrngLiveFeed() { - daemon.BackgroundWorker("Dashboard[DRNGUpdater]", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Dashboard[DRNGUpdater]", func(shutdownSignal <-chan struct{}) { newMsgRateLimiter := time.NewTicker(time.Second / 10) defer newMsgRateLimiter.Stop() @@ -60,5 +60,7 @@ func runDrngLiveFeed() { log.Info("Stopping Dashboard[DRNGUpdater] ...") drng.Instance().Events.Randomness.Detach(notifyNewRandomness) log.Info("Stopping Dashboard[DRNGUpdater] ... done") - }, shutdown.PriorityDashboard) + }, shutdown.PriorityDashboard); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } diff --git a/plugins/dashboard/livefeed.go b/plugins/dashboard/livefeed.go index 86841695..f0bebaa6 100644 --- a/plugins/dashboard/livefeed.go +++ b/plugins/dashboard/livefeed.go @@ -39,7 +39,7 @@ func runLiveFeed() { } }) - daemon.BackgroundWorker("Dashboard[MsgUpdater]", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Dashboard[MsgUpdater]", func(shutdownSignal <-chan struct{}) { messagelayer.Tangle.Events.MessageAttached.Attach(notifyNewMsg) liveFeedWorkerPool.Start() <-shutdownSignal @@ -48,5 +48,7 @@ func runLiveFeed() { newMsgRateLimiter.Stop() liveFeedWorkerPool.Stop() log.Info("Stopping Dashboard[MsgUpdater] ... done") - }, shutdown.PriorityDashboard) + }, shutdown.PriorityDashboard); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } diff --git a/plugins/dashboard/plugin.go b/plugins/dashboard/plugin.go index dbed8637..f0f033aa 100644 --- a/plugins/dashboard/plugin.go +++ b/plugins/dashboard/plugin.go @@ -81,7 +81,7 @@ func run(*node.Plugin) { log.Infof("Starting %s ...", PluginName) if err := daemon.BackgroundWorker(PluginName, worker, shutdown.PriorityAnalysis); err != nil { - log.Errorf("Error starting as daemon: %s", err) + log.Panicf("Error starting as daemon: %s", err) } } diff --git a/plugins/dashboard/visualizer.go b/plugins/dashboard/visualizer.go index 1e65a582..2725f40d 100644 --- a/plugins/dashboard/visualizer.go +++ b/plugins/dashboard/visualizer.go @@ -79,7 +79,7 @@ func runVisualizer() { visualizerWorkerPool.TrySubmit(messageId, false) }) - daemon.BackgroundWorker("Dashboard[Visualizer]", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Dashboard[Visualizer]", func(shutdownSignal <-chan struct{}) { messagelayer.Tangle.Events.MessageAttached.Attach(notifyNewMsg) defer messagelayer.Tangle.Events.MessageAttached.Detach(notifyNewMsg) messagelayer.Tangle.Events.MessageSolid.Attach(notifyNewMsg) @@ -93,5 +93,7 @@ func runVisualizer() { log.Info("Stopping Dashboard[Visualizer] ...") visualizerWorkerPool.Stop() log.Info("Stopping Dashboard[Visualizer] ... done") - }, shutdown.PriorityDashboard) + }, shutdown.PriorityDashboard); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } diff --git a/plugins/dashboard/ws.go b/plugins/dashboard/ws.go index e8137d49..7092edfc 100644 --- a/plugins/dashboard/ws.go +++ b/plugins/dashboard/ws.go @@ -58,7 +58,7 @@ func runWebSocketStreams() { wsSendWorkerPool.TrySubmit(mps) }) - daemon.BackgroundWorker("Dashboard[StatusUpdate]", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Dashboard[StatusUpdate]", func(shutdownSignal <-chan struct{}) { metrics.Events.ReceivedMPSUpdated.Attach(updateStatus) wsSendWorkerPool.Start() <-shutdownSignal @@ -66,7 +66,9 @@ func runWebSocketStreams() { metrics.Events.ReceivedMPSUpdated.Detach(updateStatus) wsSendWorkerPool.Stop() log.Info("Stopping Dashboard[StatusUpdate] ... done") - }, shutdown.PriorityDashboard) + }, shutdown.PriorityDashboard); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } // reigsters and creates a new websocket client. diff --git a/plugins/database/plugin.go b/plugins/database/plugin.go index 99333abf..5035199e 100644 --- a/plugins/database/plugin.go +++ b/plugins/database/plugin.go @@ -79,7 +79,7 @@ func configure(_ *node.Plugin) { func run(_ *node.Plugin) { if err := daemon.BackgroundWorker(PluginName+"[GC]", runGC, shutdown.PriorityBadgerGarbageCollection); err != nil { - log.Errorf("Failed to start as daemon: %s", err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/gossip/plugin.go b/plugins/gossip/plugin.go index 07f4bc6a..e7b953a9 100644 --- a/plugins/gossip/plugin.go +++ b/plugins/gossip/plugin.go @@ -35,7 +35,7 @@ func configure(*node.Plugin) { func run(*node.Plugin) { if err := daemon.BackgroundWorker(PluginName, start, shutdown.PriorityGossip); err != nil { - log.Errorf("Failed to start as daemon: %s", err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/messagelayer/plugin.go b/plugins/messagelayer/plugin.go index e5088a86..5e7cd7d8 100644 --- a/plugins/messagelayer/plugin.go +++ b/plugins/messagelayer/plugin.go @@ -73,15 +73,19 @@ func configure(*node.Plugin) { } func run(*node.Plugin) { - _ = daemon.BackgroundWorker("Tangle[MissingMessagesMonitor]", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Tangle[MissingMessagesMonitor]", func(shutdownSignal <-chan struct{}) { Tangle.MonitorMissingMessages(shutdownSignal) - }, shutdown.PriorityMissingMessagesMonitoring) + }, shutdown.PriorityMissingMessagesMonitoring); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } - _ = daemon.BackgroundWorker("Tangle", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Tangle", func(shutdownSignal <-chan struct{}) { <-shutdownSignal MessageFactory.Shutdown() MessageParser.Shutdown() Tangle.Shutdown() - }, shutdown.PriorityTangle) + }, shutdown.PriorityTangle); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } diff --git a/plugins/metrics/plugin.go b/plugins/metrics/plugin.go index 10d52add..7d62f84c 100644 --- a/plugins/metrics/plugin.go +++ b/plugins/metrics/plugin.go @@ -3,15 +3,15 @@ package metrics import ( "time" - "github.com/iotaledger/hive.go/daemon" - "github.com/iotaledger/hive.go/events" - "github.com/iotaledger/hive.go/node" - "github.com/iotaledger/hive.go/timeutil" - "github.com/iotaledger/goshimmer/packages/binary/messagelayer/message" "github.com/iotaledger/goshimmer/packages/binary/messagelayer/tangle" "github.com/iotaledger/goshimmer/packages/shutdown" "github.com/iotaledger/goshimmer/plugins/messagelayer" + "github.com/iotaledger/hive.go/daemon" + "github.com/iotaledger/hive.go/events" + "github.com/iotaledger/hive.go/logger" + "github.com/iotaledger/hive.go/node" + "github.com/iotaledger/hive.go/timeutil" ) // PluginName is the name of the metrics plugin. @@ -20,7 +20,10 @@ const PluginName = "Metrics" // Plugin is the plugin instance of the metrics plugin. var Plugin = node.NewPlugin(PluginName, node.Enabled, configure, run) +var log *logger.Logger + func configure(_ *node.Plugin) { + log = logger.NewLogger(PluginName) // increase received MPS counter whenever we attached a message messagelayer.Tangle.Events.MessageAttached.Attach(events.NewClosure(func(cachedMessage *message.CachedMessage, cachedMessageMetadata *tangle.CachedMessageMetadata) { cachedMessage.Release() @@ -31,7 +34,9 @@ func configure(_ *node.Plugin) { func run(_ *node.Plugin) { // create a background worker that "measures" the MPS value every second - daemon.BackgroundWorker("Metrics MPS Updater", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Metrics MPS Updater", func(shutdownSignal <-chan struct{}) { timeutil.Ticker(measureReceivedMPS, 1*time.Second, shutdownSignal) - }, shutdown.PriorityMetrics) + }, shutdown.PriorityMetrics); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } diff --git a/plugins/remotelog/plugin.go b/plugins/remotelog/plugin.go index cd895c08..fadc136e 100644 --- a/plugins/remotelog/plugin.go +++ b/plugins/remotelog/plugin.go @@ -95,7 +95,7 @@ func run(plugin *node.Plugin) { workerPool.TrySubmit(level, name, msg) }) - daemon.BackgroundWorker(PluginName, func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker(PluginName, func(shutdownSignal <-chan struct{}) { logger.Events.AnyMsg.Attach(logEvent) workerPool.Start() <-shutdownSignal @@ -103,7 +103,9 @@ func run(plugin *node.Plugin) { logger.Events.AnyMsg.Detach(logEvent) workerPool.Stop() log.Infof("Stopping %s ... done", PluginName) - }, shutdown.PriorityRemoteLog) + }, shutdown.PriorityRemoteLog); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } func sendLogMsg(level logger.Level, name string, msg string) { diff --git a/plugins/sync/plugin.go b/plugins/sync/plugin.go index 29e2b799..83b2e526 100644 --- a/plugins/sync/plugin.go +++ b/plugins/sync/plugin.go @@ -130,7 +130,7 @@ func monitorForDesynchronization() { } }) - daemon.BackgroundWorker("Desync-Monitor", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Desync-Monitor", func(shutdownSignal <-chan struct{}) { gossip.Manager().Events().NeighborRemoved.Attach(monitorPeerCountClosure) defer gossip.Manager().Events().NeighborRemoved.Detach(monitorPeerCountClosure) messagelayer.Tangle.Events.MessageAttached.Attach(monitorMessageInflowClosure) @@ -163,7 +163,9 @@ func monitorForDesynchronization() { return } } - }, shutdown.PrioritySynchronization) + }, shutdown.PrioritySynchronization); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } // starts a background worker and event handlers to check whether the node is synchronized by first collecting @@ -200,7 +202,7 @@ func monitorForSynchronization() { synced <- types.Empty{} }) - daemon.BackgroundWorker("Sync-Monitor", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Sync-Monitor", func(shutdownSignal <-chan struct{}) { messagelayer.Tangle.Events.MessageAttached.Attach(initAnchorPointClosure) defer messagelayer.Tangle.Events.MessageAttached.Detach(initAnchorPointClosure) messagelayer.Tangle.Events.MessageSolid.Attach(checkAnchorPointSolidityClosure) @@ -228,7 +230,9 @@ func monitorForSynchronization() { return } } - }, shutdown.PrioritySynchronization) + }, shutdown.PrioritySynchronization); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } // fills up the anchor points with newly attached messages which then are used to determine whether we are synchronized. diff --git a/plugins/webapi/plugin.go b/plugins/webapi/plugin.go index 2f1a669e..f97f3db9 100644 --- a/plugins/webapi/plugin.go +++ b/plugins/webapi/plugin.go @@ -37,7 +37,7 @@ func configure(*node.Plugin) { func run(*node.Plugin) { log.Infof("Starting %s ...", PluginName) if err := daemon.BackgroundWorker("WebAPI Server", worker, shutdown.PriorityWebAPI); err != nil { - log.Errorf("Error starting as daemon: %s", err) + log.Panicf("Failed to start as daemon: %s", err) } } diff --git a/plugins/webapi/spammer/plugin.go b/plugins/webapi/spammer/plugin.go index e1dda8b4..f4de8c78 100644 --- a/plugins/webapi/spammer/plugin.go +++ b/plugins/webapi/spammer/plugin.go @@ -6,6 +6,7 @@ import ( "github.com/iotaledger/goshimmer/plugins/issuer" "github.com/iotaledger/goshimmer/plugins/webapi" "github.com/iotaledger/hive.go/daemon" + "github.com/iotaledger/hive.go/logger" "github.com/iotaledger/hive.go/node" ) @@ -17,15 +18,20 @@ const PluginName = "Spammer" // Plugin is the plugin instance of the spammer plugin. var Plugin = node.NewPlugin(PluginName, node.Disabled, configure, run) +var log *logger.Logger + func configure(plugin *node.Plugin) { + log = logger.NewLogger(PluginName) messageSpammer = spammer.New(issuer.IssuePayload) webapi.Server.GET("spammer", handleRequest) } func run(*node.Plugin) { - _ = daemon.BackgroundWorker("Tangle", func(shutdownSignal <-chan struct{}) { + if err := daemon.BackgroundWorker("Tangle", func(shutdownSignal <-chan struct{}) { <-shutdownSignal messageSpammer.Shutdown() - }, shutdown.PrioritySpammer) + }, shutdown.PrioritySpammer); err != nil { + log.Panicf("Failed to start as daemon: %s", err) + } } -- GitLab