Add control db interface for triggering hooks, add shutdown/restart hooks

This commit is contained in:
Daniel
2020-04-24 10:02:07 +02:00
parent 350555f843
commit d2d69139b9
4 changed files with 156 additions and 1 deletions

46
core/events.go Normal file
View File

@@ -0,0 +1,46 @@
package core
import (
"context"
"github.com/safing/portbase/log"
"github.com/safing/portbase/modules"
)
const (
eventShutdown = "shutdown"
eventRestart = "restart"
restartCode = 23
)
func registerEvents() {
module.RegisterEvent(eventShutdown)
module.RegisterEvent(eventRestart)
}
func registerEventHooks() error {
err := module.RegisterEventHook("core", eventShutdown, "execute shutdown", shutdown)
if err != nil {
return err
}
err = module.RegisterEventHook("core", eventRestart, "execute shutdown", restart)
if err != nil {
return err
}
return nil
}
func shutdown(ctx context.Context, _ interface{}) error {
log.Warning("core: user requested shutdown")
go modules.Shutdown() //nolint:errcheck
return nil
}
func restart(ctx context.Context, data interface{}) error {
log.Info("core: user requested restart")
modules.SetExitStatusCode(restartCode)
go modules.Shutdown() //nolint:errcheck
return nil
}