mirror of
https://github.com/safing/portbase
synced 2025-09-04 11:40:23 +00:00
Merge pull request #139 from safing/feature/shutdown-hook-fn
Add global shutdown hook function to modules
This commit is contained in:
commit
9a76cf153d
2 changed files with 16 additions and 1 deletions
|
@ -16,8 +16,9 @@ var (
|
||||||
globalPrepFn func() error
|
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.
|
// 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) {
|
func SetGlobalPrepFn(fn func() error) {
|
||||||
if globalPrepFn == nil {
|
if globalPrepFn == nil {
|
||||||
globalPrepFn = fn
|
globalPrepFn = fn
|
||||||
|
|
|
@ -14,8 +14,17 @@ var (
|
||||||
shutdownFlag = abool.NewBool(false)
|
shutdownFlag = abool.NewBool(false)
|
||||||
|
|
||||||
shutdownCompleteSignal = make(chan struct{})
|
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.
|
// IsShuttingDown returns whether the global shutdown is in progress.
|
||||||
func IsShuttingDown() bool {
|
func IsShuttingDown() bool {
|
||||||
return shutdownFlag.IsSet()
|
return shutdownFlag.IsSet()
|
||||||
|
@ -39,6 +48,11 @@ func Shutdown() error {
|
||||||
return errors.New("shutdown already initiated")
|
return errors.New("shutdown already initiated")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Execute global shutdown function.
|
||||||
|
if globalShutdownFn != nil {
|
||||||
|
globalShutdownFn()
|
||||||
|
}
|
||||||
|
|
||||||
if initialStartCompleted.IsSet() {
|
if initialStartCompleted.IsSet() {
|
||||||
log.Warning("modules: starting shutdown...")
|
log.Warning("modules: starting shutdown...")
|
||||||
} else {
|
} else {
|
||||||
|
|
Loading…
Add table
Reference in a new issue