mirror of
https://github.com/wailsapp/wails.git
synced 2025-05-02 22:31:06 +08:00
Better signal handling (#1488)
* Better signal handling * Remove waitgroup
This commit is contained in:
parent
c22f59adb9
commit
0531645377
@ -35,6 +35,8 @@ func (a *App) Run() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func (a *App) Shutdown() {}
|
||||||
|
|
||||||
// CreateApp creates the app!
|
// CreateApp creates the app!
|
||||||
func CreateApp(appoptions *options.App) (*App, error) {
|
func CreateApp(appoptions *options.App) (*App, error) {
|
||||||
// Set up logger
|
// Set up logger
|
||||||
|
@ -44,11 +44,16 @@ type App struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) Run() error {
|
func (a *App) Shutdown() {
|
||||||
err := a.frontend.Run(a.ctx)
|
|
||||||
if a.shutdownCallback != nil {
|
if a.shutdownCallback != nil {
|
||||||
a.shutdownCallback(a.ctx)
|
a.shutdownCallback(a.ctx)
|
||||||
}
|
}
|
||||||
|
a.frontend.Quit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) Run() error {
|
||||||
|
err := a.frontend.Run(a.ctx)
|
||||||
|
a.Shutdown()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -34,11 +34,16 @@ type App struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
func (a *App) Run() error {
|
func (a *App) Shutdown() {
|
||||||
err := a.frontend.Run(a.ctx)
|
|
||||||
if a.shutdownCallback != nil {
|
if a.shutdownCallback != nil {
|
||||||
a.shutdownCallback(a.ctx)
|
a.shutdownCallback(a.ctx)
|
||||||
}
|
}
|
||||||
|
a.frontend.Quit()
|
||||||
|
}
|
||||||
|
|
||||||
|
func (a *App) Run() error {
|
||||||
|
err := a.frontend.Run(a.ctx)
|
||||||
|
a.Shutdown()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -1,73 +1,39 @@
|
|||||||
package signal
|
package signal
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"context"
|
|
||||||
"os"
|
"os"
|
||||||
gosignal "os/signal"
|
gosignal "os/signal"
|
||||||
"sync"
|
"sync"
|
||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/wailsapp/wails/v2/internal/logger"
|
|
||||||
"github.com/wailsapp/wails/v2/internal/servicebus"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
// Manager manages signals such as CTRL-C
|
var signalChannel = make(chan os.Signal, 2)
|
||||||
type Manager struct {
|
|
||||||
// Service Bus
|
|
||||||
bus *servicebus.ServiceBus
|
|
||||||
|
|
||||||
// logger
|
var callbacks []func()
|
||||||
logger logger.CustomLogger
|
var lock sync.Mutex
|
||||||
|
|
||||||
// signalChannel
|
func OnShutdown(callback func()) {
|
||||||
signalchannel chan os.Signal
|
lock.Lock()
|
||||||
|
defer lock.Unlock()
|
||||||
// ctx
|
callbacks = append(callbacks, callback)
|
||||||
ctx context.Context
|
|
||||||
cancel context.CancelFunc
|
|
||||||
|
|
||||||
// Parent waitgroup
|
|
||||||
wg *sync.WaitGroup
|
|
||||||
}
|
|
||||||
|
|
||||||
// NewManager creates a new signal manager
|
|
||||||
func NewManager(ctx context.Context, cancel context.CancelFunc, bus *servicebus.ServiceBus, logger *logger.Logger) (*Manager, error) {
|
|
||||||
|
|
||||||
result := &Manager{
|
|
||||||
bus: bus,
|
|
||||||
logger: logger.CustomLogger("Signal Manager"),
|
|
||||||
signalchannel: make(chan os.Signal, 2),
|
|
||||||
ctx: ctx,
|
|
||||||
cancel: cancel,
|
|
||||||
wg: ctx.Value("waitgroup").(*sync.WaitGroup),
|
|
||||||
}
|
|
||||||
|
|
||||||
return result, nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// Start the Signal Manager
|
// Start the Signal Manager
|
||||||
func (m *Manager) Start() {
|
func Start() {
|
||||||
|
|
||||||
// Hook into interrupts
|
// Hook into interrupts
|
||||||
gosignal.Notify(m.signalchannel, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
|
gosignal.Notify(signalChannel, os.Interrupt, syscall.SIGTERM, syscall.SIGINT)
|
||||||
|
|
||||||
m.wg.Add(1)
|
|
||||||
|
|
||||||
// Spin off signal listener and wait for either a cancellation
|
// Spin off signal listener and wait for either a cancellation
|
||||||
// or signal
|
// or signal
|
||||||
go func() {
|
go func() {
|
||||||
select {
|
select {
|
||||||
case <-m.signalchannel:
|
case <-signalChannel:
|
||||||
println()
|
println("")
|
||||||
m.logger.Trace("Ctrl+C detected. Shutting down...")
|
println("Ctrl+C detected. Shutting down...")
|
||||||
m.bus.Publish("quit", "ctrl-c pressed")
|
for _, callback := range callbacks {
|
||||||
|
callback()
|
||||||
// Start shutdown of Wails
|
}
|
||||||
m.cancel()
|
|
||||||
|
|
||||||
case <-m.ctx.Done():
|
|
||||||
}
|
}
|
||||||
m.logger.Trace("Shutdown")
|
|
||||||
m.wg.Done()
|
|
||||||
}()
|
}()
|
||||||
}
|
}
|
||||||
|
@ -4,6 +4,7 @@ package wails
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
app "github.com/wailsapp/wails/v2/internal/appng"
|
app "github.com/wailsapp/wails/v2/internal/appng"
|
||||||
|
"github.com/wailsapp/wails/v2/internal/signal"
|
||||||
"github.com/wailsapp/wails/v2/pkg/options"
|
"github.com/wailsapp/wails/v2/pkg/options"
|
||||||
)
|
)
|
||||||
|
|
||||||
@ -30,5 +31,11 @@ func Run(options *options.App) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
signal.OnShutdown(func() {
|
||||||
|
mainapp.Shutdown()
|
||||||
|
})
|
||||||
|
|
||||||
|
signal.Start()
|
||||||
|
|
||||||
return mainapp.Run()
|
return mainapp.Run()
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user