github.com/decred/politeia@v1.4.0/politeiawww/cmd/politeiavoter/signal.go (about) 1 // Copyright (c) 2013-2016 The btcsuite developers 2 // Copyright (c) 2015-2016 The Decred developers 3 // Use of this source code is governed by an ISC 4 // license that can be found in the LICENSE file. 5 6 package main 7 8 import ( 9 "context" 10 "os" 11 "os/signal" 12 ) 13 14 // shutdownRequestChannel is used to initiate shutdown from one of the 15 // subsystems using the same code paths as when an interrupt signal is received. 16 var shutdownRequestChannel = make(chan struct{}) 17 18 // interruptSignals defines the default signals to catch in order to do a proper 19 // shutdown. This may be modified during init depending on the platform. 20 var interruptSignals = []os.Signal{os.Interrupt} 21 22 // shutdownListener listens for OS Signals such as SIGINT (Ctrl+C) and shutdown 23 // requests from shutdownRequestChannel. It returns a context that is canceled 24 // when either signal is received. 25 func shutdownListener() context.Context { 26 ctx, cancel := context.WithCancel(context.Background()) 27 go func() { 28 interruptChannel := make(chan os.Signal, 1) 29 signal.Notify(interruptChannel, interruptSignals...) 30 31 // Listen for initial shutdown signal and cancel the returned context. 32 select { 33 case sig := <-interruptChannel: 34 log.Infof("Received signal (%s). Shutting down...", sig) 35 36 case <-shutdownRequestChannel: 37 log.Infof("Shutdown requested. Shutting down...") 38 } 39 cancel() 40 41 // Listen for repeated signals and display a message so the user 42 // knows the shutdown is in progress and the process is not 43 // hung. 44 for { 45 select { 46 case sig := <-interruptChannel: 47 log.Infof("Received signal (%s). Already "+ 48 "shutting down...", sig) 49 50 case <-shutdownRequestChannel: 51 log.Info("Shutdown requested. Already " + 52 "shutting down...") 53 } 54 } 55 }() 56 57 return ctx 58 }