mirror of
https://github.com/safing/portmaster
synced 2025-09-15 17:29:42 +00:00
Always trigger event on shutdown
This commit is contained in:
parent
77d5b083eb
commit
4c9abbabd3
2 changed files with 34 additions and 31 deletions
31
core/api.go
31
core/api.go
|
@ -1,9 +1,7 @@
|
||||||
package core
|
package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/safing/portbase/api"
|
"github.com/safing/portbase/api"
|
||||||
"github.com/safing/portbase/log"
|
"github.com/safing/portbase/log"
|
||||||
|
@ -13,16 +11,6 @@ import (
|
||||||
"github.com/safing/portmaster/updates"
|
"github.com/safing/portmaster/updates"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
|
||||||
eventShutdown = "shutdown"
|
|
||||||
eventRestart = "restart"
|
|
||||||
)
|
|
||||||
|
|
||||||
func registerEvents() {
|
|
||||||
module.RegisterEvent(eventShutdown, true)
|
|
||||||
module.RegisterEvent(eventRestart, true)
|
|
||||||
}
|
|
||||||
|
|
||||||
func registerAPIEndpoints() error {
|
func registerAPIEndpoints() error {
|
||||||
if err := api.RegisterEndpoint(api.Endpoint{
|
if err := api.RegisterEndpoint(api.Endpoint{
|
||||||
Path: "core/shutdown",
|
Path: "core/shutdown",
|
||||||
|
@ -70,16 +58,8 @@ func registerAPIEndpoints() error {
|
||||||
func shutdown(_ *api.Request) (msg string, err error) {
|
func shutdown(_ *api.Request) (msg string, err error) {
|
||||||
log.Warning("core: user requested shutdown via action")
|
log.Warning("core: user requested shutdown via action")
|
||||||
|
|
||||||
module.StartWorker("shutdown", func(context.Context) error {
|
|
||||||
// Notify everyone of the shutdown.
|
|
||||||
module.TriggerEvent(eventShutdown, nil)
|
|
||||||
// Wait a bit for the event to propagate.
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
// Do not run in worker, as this would block itself here.
|
// Do not run in worker, as this would block itself here.
|
||||||
go modules.Shutdown() //nolint:errcheck
|
go modules.Shutdown() //nolint:errcheck
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return "shutdown initiated", nil
|
return "shutdown initiated", nil
|
||||||
}
|
}
|
||||||
|
@ -88,15 +68,10 @@ func shutdown(_ *api.Request) (msg string, err error) {
|
||||||
func restart(_ *api.Request) (msg string, err error) {
|
func restart(_ *api.Request) (msg string, err error) {
|
||||||
log.Info("core: user requested restart via action")
|
log.Info("core: user requested restart via action")
|
||||||
|
|
||||||
module.StartWorker("restart", func(context.Context) error {
|
// Trigger restart event instead of shutdown event.
|
||||||
// Notify everyone of the shutdown.
|
restarting.Set()
|
||||||
module.TriggerEvent(eventRestart, nil)
|
// Let the updates module handle restarting.
|
||||||
// Wait a bit for the event to propagate.
|
|
||||||
time.Sleep(1 * time.Second)
|
|
||||||
|
|
||||||
updates.RestartNow()
|
updates.RestartNow()
|
||||||
return nil
|
|
||||||
})
|
|
||||||
|
|
||||||
return "restart initiated", nil
|
return "restart initiated", nil
|
||||||
}
|
}
|
||||||
|
|
28
core/core.go
28
core/core.go
|
@ -2,9 +2,11 @@ package core
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"time"
|
||||||
|
|
||||||
"github.com/safing/portbase/modules"
|
"github.com/safing/portbase/modules"
|
||||||
"github.com/safing/portbase/modules/subsystems"
|
"github.com/safing/portbase/modules/subsystems"
|
||||||
|
"github.com/tevino/abool"
|
||||||
|
|
||||||
// module dependencies
|
// module dependencies
|
||||||
_ "github.com/safing/portmaster/netenv"
|
_ "github.com/safing/portmaster/netenv"
|
||||||
|
@ -13,8 +15,15 @@ import (
|
||||||
_ "github.com/safing/portmaster/updates"
|
_ "github.com/safing/portmaster/updates"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
const (
|
||||||
|
eventShutdown = "shutdown"
|
||||||
|
eventRestart = "restart"
|
||||||
|
)
|
||||||
|
|
||||||
var (
|
var (
|
||||||
module *modules.Module
|
module *modules.Module
|
||||||
|
|
||||||
|
restarting = abool.New()
|
||||||
)
|
)
|
||||||
|
|
||||||
func init() {
|
func init() {
|
||||||
|
@ -27,6 +36,8 @@ func init() {
|
||||||
"config:core/",
|
"config:core/",
|
||||||
nil,
|
nil,
|
||||||
)
|
)
|
||||||
|
|
||||||
|
modules.SetGlobalShutdownFn(shutdownHook)
|
||||||
}
|
}
|
||||||
|
|
||||||
func prep() error {
|
func prep() error {
|
||||||
|
@ -54,3 +65,20 @@ func start() error {
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func registerEvents() {
|
||||||
|
module.RegisterEvent(eventShutdown, true)
|
||||||
|
module.RegisterEvent(eventRestart, true)
|
||||||
|
}
|
||||||
|
|
||||||
|
func shutdownHook() {
|
||||||
|
// Notify everyone of the restart/shutdown.
|
||||||
|
if restarting.IsNotSet() {
|
||||||
|
module.TriggerEvent(eventShutdown, nil)
|
||||||
|
} else {
|
||||||
|
module.TriggerEvent(eventRestart, nil)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Wait a bit for the event to propagate.
|
||||||
|
time.Sleep(1 * time.Second)
|
||||||
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue