Skip to content
Snippets Groups Projects
Select Git revision
  • 9696c8997cb785c5f0d8f1a98d7da74b56a83e7c
  • without_tipselection default
  • develop protected
  • fix/grafana-local-dashboard
  • wasp
  • fix/dashboard-explorer-freeze
  • master
  • feat/timerqueue
  • test/sync_debug_and_650
  • feat/sync_revamp_inv
  • wip/sync
  • tool/db-recovery
  • portcheck/fix
  • fix/synchronization
  • feat/new-dashboard-analysis
  • feat/refactored-analysis-dashboard
  • feat/new-analysis-dashboard
  • test/demo-prometheus-fpc
  • prometheus_metrics
  • wip/analysis-server
  • merge/fpc-test-value-transfer
  • v0.2.2
  • v0.2.1
  • v0.2.0
  • v0.1.3
  • v0.1.2
  • v0.1.1
  • v0.1.0
28 results

plugin.go

Blame
  • user avatar
    Acha Bill authored and GitHub committed
    * refactor plugins
    
    * fix fmt
    
    * fix compile err
    
    * fix errors
    
    * expose singletons via sync.once
    
    * re-disable remotelog and webauth plugins
    
    * re-enable autopeering plugin
    
    * Apply suggestions from code review
    
    * Apply suggestions from code review
    
    * add descriptive comments for getters
    
    Co-authored-by: default avatarLuca Moser <moser.luca@gmail.com>
    68fdcf49
    History
    plugin.go 2.12 KiB
    package gracefulshutdown
    
    import (
    	"os"
    	"os/signal"
    	"sort"
    	"strings"
    	"sync"
    	"syscall"
    	"time"
    
    	"github.com/iotaledger/hive.go/daemon"
    	"github.com/iotaledger/hive.go/logger"
    	"github.com/iotaledger/hive.go/node"
    )
    
    // PluginName is the name of the graceful shutdown plugin.
    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 (
    	// plugin is the plugin instance of the graceful shutdown plugin.
     	plugin *node.Plugin
    	once sync.Once
    	log *logger.Logger
    	gracefulStop chan os.Signal
    )
    
    // Plugin gets the plugin instance.
    func Plugin() *node.Plugin {
    	once.Do(func() {
    		plugin = node.NewPlugin(PluginName, node.Enabled, func(plugin *node.Plugin) {
    			log = logger.NewLogger(PluginName)
    			gracefulStop = make(chan os.Signal)
    
    			signal.Notify(gracefulStop, syscall.SIGTERM)
    			signal.Notify(gracefulStop, syscall.SIGINT)
    
    			go func() {
    				<-gracefulStop
    
    				log.Warnf("Received shutdown request - waiting (max %d) to finish processing ...", WaitToKillTimeInSeconds)
    
    				go func() {
    					start := time.Now()
    					for x := range time.Tick(1 * time.Second) {
    						secondsSinceStart := x.Sub(start).Seconds()
    
    						if secondsSinceStart <= WaitToKillTimeInSeconds {
    							processList := ""
    							runningBackgroundWorkers := daemon.GetRunningBackgroundWorkers()
    							if len(runningBackgroundWorkers) >= 1 {
    								sort.Strings(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)
    						}
    					}
    				}()
    
    				daemon.Shutdown()
    			}()
    		})
    	})
    	return plugin
    }
    
    
    // ShutdownWithError prints out an error message and shuts down the default daemon instance.
    func ShutdownWithError(err error) {
    	log.Error(err)
    	gracefulStop <- syscall.SIGINT
    }