From 88f60d77363e9c718d3bd10603f73d8c69b3ed0e Mon Sep 17 00:00:00 2001 From: Kevin Wan Date: Sun, 15 Oct 2023 23:24:17 +0800 Subject: [PATCH] chore: refactor signal sigterm and sigint (#3632) --- core/proc/shutdown.go | 6 +++--- core/proc/signals.go | 23 +++++++++++++++-------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/core/proc/shutdown.go b/core/proc/shutdown.go index ece547d5..763742a2 100644 --- a/core/proc/shutdown.go +++ b/core/proc/shutdown.go @@ -52,10 +52,10 @@ func WrapUp() { wrapUpListeners.notifyListeners() } -func gracefulStop(signals chan os.Signal) { +func gracefulStop(signals chan os.Signal, sig syscall.Signal) { signal.Stop(signals) - logx.Info("Got signal SIGTERM, shutting down...") + logx.Infof("Got signal %d, shutting down...", sig) go wrapUpListeners.notifyListeners() time.Sleep(wrapUpTime) @@ -63,7 +63,7 @@ func gracefulStop(signals chan os.Signal) { time.Sleep(delayTimeBeforeForceQuit - wrapUpTime) 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 { diff --git a/core/proc/signals.go b/core/proc/signals.go index a6d667d2..f4a57b08 100644 --- a/core/proc/signals.go +++ b/core/proc/signals.go @@ -34,14 +34,12 @@ func init() { profiler.Stop() profiler = nil } - case syscall.SIGTERM, syscall.SIGINT: - select { - case <-done: - // already closed - default: - close(done) - } - gracefulStop(signals) + case syscall.SIGTERM: + stopOnSignal() + gracefulStop(signals, syscall.SIGTERM) + case syscall.SIGINT: + stopOnSignal() + gracefulStop(signals, syscall.SIGINT) default: logx.Error("Got unregistered signal:", v) } @@ -53,3 +51,12 @@ func init() { func Done() <-chan struct{} { return done } + +func stopOnSignal() { + select { + case <-done: + // already closed + default: + close(done) + } +}