-
Notifications
You must be signed in to change notification settings - Fork 167
/
signals.go
91 lines (74 loc) · 1.85 KB
/
signals.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
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
// +build !windows
package cmd
import (
"io/ioutil"
"os"
"os/signal"
"runtime/pprof"
"syscall"
"time"
"github.com/micro/go-micro/broker"
"go.uber.org/zap"
"github.com/pydio/cells/common/log"
)
func handleSignals() {
c := make(chan os.Signal, 1)
// SIGUSR1 does not compile on windows. Use direct value syscall.Signal instead
signal.Notify(c, syscall.SIGINT, syscall.SIGHUP, syscall.SIGUSR1, syscall.SIGTERM)
go func() {
for sig := range c {
switch sig {
case syscall.SIGINT:
log.Info("Disconnecting broker")
// Disconnecting the broker so that we are not flooded with messages
broker.Disconnect()
log.Info("Stopping all services")
// Stop all services
for _, service := range allServices {
service.Stop()
}
log.Info("Exiting")
<-time.After(2 * time.Second)
os.Exit(0)
case syscall.SIGUSR1:
pprof.Lookup("goroutine").WriteTo(os.Stdout, 1)
if !profiling {
f, err := ioutil.TempFile("/tmp", "pydio-cpu-profile-")
if err != nil {
log.Fatal("Cannot create cpu profile", zap.Error(err))
}
pprof.StartCPUProfile(f)
profile = f
profiling = true
fheap, err := ioutil.TempFile("/tmp", "pydio-cpu-heap-")
if err != nil {
log.Fatal("Cannot create heap profile", zap.Error(err))
}
pprof.WriteHeapProfile(fheap)
} else {
pprof.StopCPUProfile()
if err := profile.Close(); err != nil {
log.Fatal("Cannot close cpu profile", zap.Error(err))
}
profiling = false
}
case syscall.SIGHUP:
// Stop all services
for _, service := range allServices {
if service.Name() == "nats" {
continue
}
service.Stop()
}
initServices()
// Start all services
for _, service := range allServices {
if service.Name() == "nats" {
continue
}
service.Start()
}
}
}
}()
}