Add global shutdown hook function to modules

This commit is contained in:
Daniel 2021-10-07 14:23:49 +02:00
parent f999cb027f
commit e7ae898ddb
2 changed files with 16 additions and 1 deletions

View file

@ -16,8 +16,9 @@ var (
globalPrepFn func() error
)
// SetGlobalPrepFn sets a global prep function that is run before all modules. This can be used to pre-initialize modules, such as setting the data root or database path.
// SetGlobalPrepFn sets a global prep function that is run before all modules.
// This can be used to pre-initialize modules, such as setting the data root
// or database path.
func SetGlobalPrepFn(fn func() error) {
if globalPrepFn == nil {
globalPrepFn = fn

View file

@ -14,8 +14,17 @@ var (
shutdownFlag = abool.NewBool(false)
shutdownCompleteSignal = make(chan struct{})
globalShutdownFn func()
)
// SetGlobalShutdownFn sets a global shutdown function that is called first when shutting down.
func SetGlobalShutdownFn(fn func()) {
if globalShutdownFn == nil {
globalShutdownFn = fn
}
}
// IsShuttingDown returns whether the global shutdown is in progress.
func IsShuttingDown() bool {
return shutdownFlag.IsSet()
@ -39,6 +48,11 @@ func Shutdown() error {
return errors.New("shutdown already initiated")
}
// Execute global shutdown function.
if globalShutdownFn != nil {
globalShutdownFn()
}
if initialStartCompleted.IsSet() {
log.Warning("modules: starting shutdown...")
} else {