chore: refactor signal sigterm and sigint (#3632)

master
Kevin Wan 1 year ago committed by GitHub
parent 95b7a3d3ce
commit 88f60d7736
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23

@ -52,10 +52,10 @@ func WrapUp() {
wrapUpListeners.notifyListeners() wrapUpListeners.notifyListeners()
} }
func gracefulStop(signals chan os.Signal) { func gracefulStop(signals chan os.Signal, sig syscall.Signal) {
signal.Stop(signals) signal.Stop(signals)
logx.Info("Got signal SIGTERM, shutting down...") logx.Infof("Got signal %d, shutting down...", sig)
go wrapUpListeners.notifyListeners() go wrapUpListeners.notifyListeners()
time.Sleep(wrapUpTime) time.Sleep(wrapUpTime)
@ -63,7 +63,7 @@ func gracefulStop(signals chan os.Signal) {
time.Sleep(delayTimeBeforeForceQuit - wrapUpTime) time.Sleep(delayTimeBeforeForceQuit - wrapUpTime)
logx.Infof("Still alive after %v, going to force kill the process...", delayTimeBeforeForceQuit) logx.Infof("Still alive after %v, going to force kill the process...", delayTimeBeforeForceQuit)
syscall.Kill(syscall.Getpid(), syscall.SIGTERM) _ = syscall.Kill(syscall.Getpid(), sig)
} }
type listenerManager struct { type listenerManager struct {

@ -34,14 +34,12 @@ func init() {
profiler.Stop() profiler.Stop()
profiler = nil profiler = nil
} }
case syscall.SIGTERM, syscall.SIGINT: case syscall.SIGTERM:
select { stopOnSignal()
case <-done: gracefulStop(signals, syscall.SIGTERM)
// already closed case syscall.SIGINT:
default: stopOnSignal()
close(done) gracefulStop(signals, syscall.SIGINT)
}
gracefulStop(signals)
default: default:
logx.Error("Got unregistered signal:", v) logx.Error("Got unregistered signal:", v)
} }
@ -53,3 +51,12 @@ func init() {
func Done() <-chan struct{} { func Done() <-chan struct{} {
return done return done
} }
func stopOnSignal() {
select {
case <-done:
// already closed
default:
close(done)
}
}

Loading…
Cancel
Save