Add OnShutdown hook

This will allow plugins to clean up resources that need to be
properly shutdown before Geth terminates.
This commit is contained in:
Austin Roberts 2022-07-29 15:34:42 -05:00
parent 66a0d514c4
commit b45c9e8e03
2 changed files with 19 additions and 0 deletions

View File

@ -357,6 +357,7 @@ func geth(ctx *cli.Context) error {
return err return err
} }
defer stack.Close() defer stack.Close()
defer pluginsOnShutdown()
stack.RegisterAPIs(pluginGetAPIs(stack, wrapperBackend)) stack.RegisterAPIs(pluginGetAPIs(stack, wrapperBackend))
//end PluGeth code injection //end PluGeth code injection
startNode(ctx, stack, backend, false) startNode(ctx, stack, backend, false)

View File

@ -84,3 +84,21 @@ func pluginsInitializeNode(stack *node.Node, backend restricted.Backend) {
} }
InitializeNode(plugins.DefaultPluginLoader, stack, backend) InitializeNode(plugins.DefaultPluginLoader, stack, backend)
} }
func OnShutdown(pl *plugins.PluginLoader) {
fnList := pl.Lookup("OnShutdown", func(item interface{}) bool {
_, ok := item.(func())
return ok
})
for _, fni := range fnList {
fni.(func())()
}
}
func pluginsOnShutdown() {
if plugins.DefaultPluginLoader == nil {
log.Warn("Attempting OnShutdown, but default PluginLoader has not been initialized")
return
}
OnShutdown(plugins.DefaultPluginLoader)
}