Fixed Ctrl-C handling
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"os"
|
||||
"os/signal"
|
||||
"sync"
|
||||
"syscall"
|
||||
|
||||
"source.hodakov.me/hdkv/faketunes/internal/application"
|
||||
@@ -36,6 +37,15 @@ func main() {
|
||||
app.Logger().Fatal(err)
|
||||
}
|
||||
|
||||
// CTRL+C handler.
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(
|
||||
interrupt, syscall.SIGINT, syscall.SIGTERM,
|
||||
)
|
||||
|
||||
var wg sync.WaitGroup
|
||||
app.RegisterGlobalWaitGroup(&wg)
|
||||
|
||||
err = app.StartDomains()
|
||||
if err != nil {
|
||||
app.Logger().Fatal(err)
|
||||
@@ -43,24 +53,17 @@ func main() {
|
||||
|
||||
app.Logger().Info("Started faketunes")
|
||||
|
||||
// CTRL+C handler.
|
||||
interrupt := make(chan os.Signal, 1)
|
||||
signal.Notify(interrupt)
|
||||
|
||||
shutdownDone := make(chan bool, 1)
|
||||
|
||||
go func() {
|
||||
signalThing := <-interrupt
|
||||
if signalThing == syscall.SIGTERM || signalThing == syscall.SIGINT {
|
||||
app.Logger().WithField("signal", signalThing.String()).
|
||||
Info("Got terminating signal, shutting down...")
|
||||
app.Logger().WithField("signal", signalThing.String()).
|
||||
Info("Got terminating signal, shutting down...")
|
||||
|
||||
cancel()
|
||||
|
||||
shutdownDone <- true
|
||||
}
|
||||
cancel()
|
||||
}()
|
||||
|
||||
<-shutdownDone
|
||||
// Wait for all domains to finish their cleanup
|
||||
wg.Wait()
|
||||
|
||||
app.Logger().Info("Faketunes shutdown complete")
|
||||
os.Exit(0)
|
||||
}
|
||||
|
||||
@@ -16,6 +16,7 @@ type App struct {
|
||||
ctx context.Context
|
||||
logger *logrus.Entry
|
||||
config *configuration.Config
|
||||
wg *sync.WaitGroup
|
||||
|
||||
domains map[string]domains.Domain
|
||||
domainsMutex sync.RWMutex
|
||||
@@ -118,3 +119,11 @@ func (a *App) StartDomains() error {
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
func (a *App) RegisterGlobalWaitGroup(wg *sync.WaitGroup) {
|
||||
a.wg = wg
|
||||
}
|
||||
|
||||
func (a *App) GetGlobalWaitGroup() *sync.WaitGroup {
|
||||
return a.wg
|
||||
}
|
||||
|
||||
@@ -6,6 +6,7 @@ var (
|
||||
ErrFilesystem = errors.New("filesystem")
|
||||
ErrConnectDependencies = errors.New("failed to connect dependencies")
|
||||
ErrFailedToPrepareDirectories = errors.New("failed to prepare directories")
|
||||
ErrFailedToGetWaitGroup = errors.New("failed to get global waitgroup")
|
||||
ErrNoSource = errors.New("source does not exist")
|
||||
ErrFailedToCleanupDestination = errors.New("failed to clean up destination directory")
|
||||
ErrFailedToCreateDestinationDirectory = errors.New("failed to create destination directory")
|
||||
|
||||
@@ -58,9 +58,14 @@ func (f *FS) Start() error {
|
||||
return fmt.Errorf("%w: %w (%w)", ErrFilesystem, ErrFailedToPrepareDirectories, err)
|
||||
}
|
||||
|
||||
go func() {
|
||||
wg := f.app.GetGlobalWaitGroup()
|
||||
if wg == nil {
|
||||
return fmt.Errorf("%w: %w (%s)", ErrFilesystem, ErrFailedToGetWaitGroup, "got nil waitgroup")
|
||||
}
|
||||
|
||||
wg.Go(func() {
|
||||
f.mount()
|
||||
}()
|
||||
})
|
||||
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -43,10 +43,6 @@ func (f *FS) mount() {
|
||||
}
|
||||
defer server.Unmount()
|
||||
|
||||
select {
|
||||
case <-f.app.Context().Done():
|
||||
return
|
||||
default:
|
||||
server.Wait()
|
||||
}
|
||||
<-f.app.Context().Done()
|
||||
f.app.Logger().Debug("Application context cancelled, unmounting FUSE server...")
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user