diff --git a/modules/start.go b/modules/start.go index 2245d6f..8ee35a0 100644 --- a/modules/start.go +++ b/modules/start.go @@ -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 diff --git a/modules/stop.go b/modules/stop.go index cbfefad..0ad30cd 100644 --- a/modules/stop.go +++ b/modules/stop.go @@ -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 {