-
Notifications
You must be signed in to change notification settings - Fork 115
/
plugin.go
72 lines (58 loc) · 1.85 KB
/
plugin.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
package gracefulshutdown
import (
"os"
"os/signal"
"runtime/pprof"
"sort"
"strings"
"syscall"
"time"
"github.com/iotaledger/hive.go/daemon"
"github.com/iotaledger/hive.go/node"
)
// PluginName is the name of the graceful shutdown plugin.
const PluginName = "GracefulShutdown"
var (
// Plugin is the plugin instance of the graceful shutdown plugin.
Plugin *node.Plugin
gracefulStop chan os.Signal
)
func init() {
Plugin = node.NewPlugin(PluginName, nil, node.Enabled, configure)
}
func configure(plugin *node.Plugin) {
gracefulStop = make(chan os.Signal)
signal.Notify(gracefulStop, syscall.SIGTERM)
signal.Notify(gracefulStop, syscall.SIGINT)
go func() {
<-gracefulStop
plugin.LogWarnf("Received shutdown request - waiting (max %v) to finish processing ...", Parameters.WaitToKillTime)
go func() {
ticker := time.NewTicker(1 * time.Second)
defer ticker.Stop()
start := time.Now()
for x := range ticker.C {
timeSinceStart := x.Sub(start)
if timeSinceStart <= Parameters.WaitToKillTime {
processList := ""
runningBackgroundWorkers := daemon.GetRunningBackgroundWorkers()
if len(runningBackgroundWorkers) >= 1 {
sort.Strings(runningBackgroundWorkers)
processList = "(" + strings.Join(runningBackgroundWorkers, ", ") + ") "
}
plugin.LogWarnf("Received shutdown request - waiting (max %v) to finish processing %s...", (Parameters.WaitToKillTime - timeSinceStart).Truncate(time.Second), processList)
} else {
plugin.LogError("Background processes did not terminate in time! Forcing shutdown ...")
pprof.Lookup("goroutine").WriteTo(os.Stdout, 2)
os.Exit(1)
}
}
}()
daemon.Shutdown()
}()
}
// ShutdownWithError prints out an error message and shuts down the default daemon instance.
func ShutdownWithError(err error) {
Plugin.LogError(err)
gracefulStop <- syscall.SIGINT
}