Fixed Ctrl-C handling

This commit is contained in:
2026-02-12 03:18:33 +03:00
parent 1b44637606
commit 4f597b2478
5 changed files with 36 additions and 22 deletions

View File

@@ -4,6 +4,7 @@ import (
"context" "context"
"os" "os"
"os/signal" "os/signal"
"sync"
"syscall" "syscall"
"source.hodakov.me/hdkv/faketunes/internal/application" "source.hodakov.me/hdkv/faketunes/internal/application"
@@ -36,6 +37,15 @@ func main() {
app.Logger().Fatal(err) 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() err = app.StartDomains()
if err != nil { if err != nil {
app.Logger().Fatal(err) app.Logger().Fatal(err)
@@ -43,24 +53,17 @@ func main() {
app.Logger().Info("Started faketunes") app.Logger().Info("Started faketunes")
// CTRL+C handler.
interrupt := make(chan os.Signal, 1)
signal.Notify(interrupt)
shutdownDone := make(chan bool, 1)
go func() { go func() {
signalThing := <-interrupt signalThing := <-interrupt
if signalThing == syscall.SIGTERM || signalThing == syscall.SIGINT {
app.Logger().WithField("signal", signalThing.String()). app.Logger().WithField("signal", signalThing.String()).
Info("Got terminating signal, shutting down...") Info("Got terminating signal, shutting down...")
cancel() cancel()
shutdownDone <- true
}
}() }()
<-shutdownDone // Wait for all domains to finish their cleanup
wg.Wait()
app.Logger().Info("Faketunes shutdown complete")
os.Exit(0) os.Exit(0)
} }

View File

@@ -16,6 +16,7 @@ type App struct {
ctx context.Context ctx context.Context
logger *logrus.Entry logger *logrus.Entry
config *configuration.Config config *configuration.Config
wg *sync.WaitGroup
domains map[string]domains.Domain domains map[string]domains.Domain
domainsMutex sync.RWMutex domainsMutex sync.RWMutex
@@ -118,3 +119,11 @@ func (a *App) StartDomains() error {
return nil return nil
} }
func (a *App) RegisterGlobalWaitGroup(wg *sync.WaitGroup) {
a.wg = wg
}
func (a *App) GetGlobalWaitGroup() *sync.WaitGroup {
return a.wg
}

View File

@@ -6,6 +6,7 @@ var (
ErrFilesystem = errors.New("filesystem") ErrFilesystem = errors.New("filesystem")
ErrConnectDependencies = errors.New("failed to connect dependencies") ErrConnectDependencies = errors.New("failed to connect dependencies")
ErrFailedToPrepareDirectories = errors.New("failed to prepare directories") ErrFailedToPrepareDirectories = errors.New("failed to prepare directories")
ErrFailedToGetWaitGroup = errors.New("failed to get global waitgroup")
ErrNoSource = errors.New("source does not exist") ErrNoSource = errors.New("source does not exist")
ErrFailedToCleanupDestination = errors.New("failed to clean up destination directory") ErrFailedToCleanupDestination = errors.New("failed to clean up destination directory")
ErrFailedToCreateDestinationDirectory = errors.New("failed to create destination directory") ErrFailedToCreateDestinationDirectory = errors.New("failed to create destination directory")

View File

@@ -58,9 +58,14 @@ func (f *FS) Start() error {
return fmt.Errorf("%w: %w (%w)", ErrFilesystem, ErrFailedToPrepareDirectories, err) 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() f.mount()
}() })
return nil return nil
} }

View File

@@ -43,10 +43,6 @@ func (f *FS) mount() {
} }
defer server.Unmount() defer server.Unmount()
select { <-f.app.Context().Done()
case <-f.app.Context().Done(): f.app.Logger().Debug("Application context cancelled, unmounting FUSE server...")
return
default:
server.Wait()
}
} }