2021-09-23 19:57:05 +08:00
|
|
|
//go:build linux || darwin
|
2020-07-26 17:09:05 +08:00
|
|
|
|
|
|
|
package proc
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
"os/signal"
|
|
|
|
"sync"
|
|
|
|
"syscall"
|
|
|
|
"time"
|
|
|
|
|
2022-01-04 15:51:32 +08:00
|
|
|
"github.com/zeromicro/go-zero/core/logx"
|
|
|
|
"github.com/zeromicro/go-zero/core/threading"
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
2025-01-01 19:48:53 +08:00
|
|
|
type ProcConf struct {
|
|
|
|
WrapUpTime time.Duration `json:",default=1s"`
|
|
|
|
WaitTime time.Duration `json:",default=5.5s"`
|
|
|
|
}
|
2020-07-26 17:09:05 +08:00
|
|
|
|
|
|
|
var (
|
2025-01-01 19:48:53 +08:00
|
|
|
wrapUpListeners = new(listenerManager)
|
|
|
|
shutdownListeners = new(listenerManager)
|
|
|
|
wrapUpTime = time.Second
|
|
|
|
// why we use 5500 milliseconds is because most of our queue are blocking mode with 5 seconds
|
|
|
|
delayTimeBeforeForceQuit = 5500 * time.Millisecond
|
2020-07-26 17:09:05 +08:00
|
|
|
)
|
|
|
|
|
2021-02-22 10:07:39 +08:00
|
|
|
// AddShutdownListener adds fn as a shutdown listener.
|
|
|
|
// The returned func can be used to wait for fn getting called.
|
2020-07-26 17:09:05 +08:00
|
|
|
func AddShutdownListener(fn func()) (waitForCalled func()) {
|
|
|
|
return shutdownListeners.addListener(fn)
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:07:39 +08:00
|
|
|
// AddWrapUpListener adds fn as a wrap up listener.
|
|
|
|
// The returned func can be used to wait for fn getting called.
|
2020-07-26 17:09:05 +08:00
|
|
|
func AddWrapUpListener(fn func()) (waitForCalled func()) {
|
|
|
|
return wrapUpListeners.addListener(fn)
|
|
|
|
}
|
|
|
|
|
2021-02-22 10:07:39 +08:00
|
|
|
// SetTimeToForceQuit sets the waiting time before force quitting.
|
2020-09-14 21:13:19 +08:00
|
|
|
func SetTimeToForceQuit(duration time.Duration) {
|
2020-07-26 17:09:05 +08:00
|
|
|
delayTimeBeforeForceQuit = duration
|
|
|
|
}
|
|
|
|
|
2025-01-01 19:48:53 +08:00
|
|
|
func Setup(conf ProcConf) {
|
|
|
|
wrapUpTime = conf.WrapUpTime
|
|
|
|
delayTimeBeforeForceQuit = conf.WaitTime
|
|
|
|
}
|
|
|
|
|
2023-01-24 13:43:13 +08:00
|
|
|
// Shutdown calls the registered shutdown listeners, only for test purpose.
|
|
|
|
func Shutdown() {
|
|
|
|
shutdownListeners.notifyListeners()
|
|
|
|
}
|
|
|
|
|
|
|
|
// WrapUp wraps up the process, only for test purpose.
|
|
|
|
func WrapUp() {
|
|
|
|
wrapUpListeners.notifyListeners()
|
|
|
|
}
|
|
|
|
|
2023-10-15 23:24:17 +08:00
|
|
|
func gracefulStop(signals chan os.Signal, sig syscall.Signal) {
|
2020-07-26 17:09:05 +08:00
|
|
|
signal.Stop(signals)
|
|
|
|
|
2023-10-15 23:24:17 +08:00
|
|
|
logx.Infof("Got signal %d, shutting down...", sig)
|
2021-12-02 22:41:57 +08:00
|
|
|
go wrapUpListeners.notifyListeners()
|
2020-07-26 17:09:05 +08:00
|
|
|
|
|
|
|
time.Sleep(wrapUpTime)
|
2021-12-02 22:41:57 +08:00
|
|
|
go shutdownListeners.notifyListeners()
|
2020-07-26 17:09:05 +08:00
|
|
|
|
|
|
|
time.Sleep(delayTimeBeforeForceQuit - wrapUpTime)
|
|
|
|
logx.Infof("Still alive after %v, going to force kill the process...", delayTimeBeforeForceQuit)
|
2023-10-15 23:24:17 +08:00
|
|
|
_ = syscall.Kill(syscall.Getpid(), sig)
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
type listenerManager struct {
|
|
|
|
lock sync.Mutex
|
|
|
|
waitGroup sync.WaitGroup
|
|
|
|
listeners []func()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *listenerManager) addListener(fn func()) (waitForCalled func()) {
|
|
|
|
lm.waitGroup.Add(1)
|
|
|
|
|
|
|
|
lm.lock.Lock()
|
|
|
|
lm.listeners = append(lm.listeners, func() {
|
|
|
|
defer lm.waitGroup.Done()
|
|
|
|
fn()
|
|
|
|
})
|
|
|
|
lm.lock.Unlock()
|
|
|
|
|
2025-01-01 15:06:50 +08:00
|
|
|
// we can return lm.waitGroup.Wait directly,
|
|
|
|
// but we want to make the returned func more readable.
|
|
|
|
// creating an extra closure would be negligible in practice.
|
2020-07-26 17:09:05 +08:00
|
|
|
return func() {
|
|
|
|
lm.waitGroup.Wait()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (lm *listenerManager) notifyListeners() {
|
|
|
|
lm.lock.Lock()
|
|
|
|
defer lm.lock.Unlock()
|
|
|
|
|
2021-12-02 22:41:57 +08:00
|
|
|
group := threading.NewRoutineGroup()
|
2020-07-26 17:09:05 +08:00
|
|
|
for _, listener := range lm.listeners {
|
2021-12-02 22:41:57 +08:00
|
|
|
group.RunSafe(listener)
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|
2021-12-02 22:41:57 +08:00
|
|
|
group.Wait()
|
2023-07-29 16:51:43 +08:00
|
|
|
|
|
|
|
lm.listeners = nil
|
2020-07-26 17:09:05 +08:00
|
|
|
}
|