mirror of
https://github.com/seaweedfs/seaweedfs.git
synced 2024-01-19 02:48:24 +00:00
Merge pull request #747 from a1exwang/master
[bugfix] Fix interrupt hook overwritten bug
This commit is contained in:
commit
88ad8d3a89
|
@ -14,7 +14,6 @@ func SetupProfiling(cpuProfile, memProfile string) {
|
||||||
glog.Fatal(err)
|
glog.Fatal(err)
|
||||||
}
|
}
|
||||||
pprof.StartCPUProfile(f)
|
pprof.StartCPUProfile(f)
|
||||||
defer pprof.StopCPUProfile()
|
|
||||||
OnInterrupt(func() {
|
OnInterrupt(func() {
|
||||||
pprof.StopCPUProfile()
|
pprof.StopCPUProfile()
|
||||||
})
|
})
|
||||||
|
|
|
@ -5,13 +5,16 @@ package util
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
||||||
"os/signal"
|
"os/signal"
|
||||||
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
)
|
)
|
||||||
|
|
||||||
func OnInterrupt(fn func()) {
|
var signalChan chan os.Signal
|
||||||
// deal with control+c,etc
|
var hooks = make([]func(), 0)
|
||||||
signalChan := make(chan os.Signal, 1)
|
var hookLock sync.Mutex
|
||||||
// controlling terminal close, daemon not exit
|
|
||||||
|
func init() {
|
||||||
|
signalChan = make(chan os.Signal, 1)
|
||||||
signal.Ignore(syscall.SIGHUP)
|
signal.Ignore(syscall.SIGHUP)
|
||||||
signal.Notify(signalChan,
|
signal.Notify(signalChan,
|
||||||
os.Interrupt,
|
os.Interrupt,
|
||||||
|
@ -24,8 +27,20 @@ func OnInterrupt(fn func()) {
|
||||||
)
|
)
|
||||||
go func() {
|
go func() {
|
||||||
for _ = range signalChan {
|
for _ = range signalChan {
|
||||||
fn()
|
for _, hook := range hooks {
|
||||||
|
hook()
|
||||||
|
}
|
||||||
os.Exit(0)
|
os.Exit(0)
|
||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func OnInterrupt(fn func()) {
|
||||||
|
// prevent reentry
|
||||||
|
hookLock.Lock()
|
||||||
|
defer hookLock.Unlock()
|
||||||
|
|
||||||
|
// deal with control+c,etc
|
||||||
|
// controlling terminal close, daemon not exit
|
||||||
|
hooks = append(hooks, fn)
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue