mirror of
https://github.com/safing/portbase
synced 2025-04-17 07:59:09 +00:00
Add global shutdown hook function to modules
This commit is contained in:
parent
f999cb027f
commit
e7ae898ddb
2 changed files with 16 additions and 1 deletions
|
@ -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
|
||||
|
|
|
@ -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 {
|
||||
|
|
Loading…
Add table
Reference in a new issue