From 3d9fff36f777cf9c5f14c0a742a6d9b521604f6d Mon Sep 17 00:00:00 2001 From: Deluan Date: Sun, 28 Apr 2024 17:44:11 -0400 Subject: [PATCH] Use signal.NotifyContext --- cmd/root.go | 37 +++++++++++++++++++++++++++++- cmd/signaler_nonunix.go | 27 ---------------------- cmd/signaler_unix.go | 51 ----------------------------------------- 3 files changed, 36 insertions(+), 79 deletions(-) delete mode 100644 cmd/signaler_nonunix.go delete mode 100644 cmd/signaler_unix.go diff --git a/cmd/root.go b/cmd/root.go index f8383034d..88ad695a3 100644 --- a/cmd/root.go +++ b/cmd/root.go @@ -5,7 +5,9 @@ import ( "errors" "fmt" "os" + "os/signal" "strings" + "syscall" "time" "github.com/go-chi/chi/v5/middleware" @@ -70,7 +72,15 @@ func runNavidrome() { log.Info("Navidrome stopped, bye.") }() - g, ctx := errgroup.WithContext(context.Background()) + ctx, cancel := signal.NotifyContext(context.Background(), + os.Interrupt, + syscall.SIGHUP, + syscall.SIGTERM, + syscall.SIGABRT, + ) + defer cancel() + + g, ctx := errgroup.WithContext(ctx) g.Go(startServer(ctx)) g.Go(startSignaler(ctx)) g.Go(startScheduler(ctx)) @@ -112,6 +122,31 @@ func startServer(ctx context.Context) func() error { } } +func startSignaler(ctx context.Context) func() error { + log.Info("Starting signaler") + scanner := GetScanner() + + return func() error { + var sigChan = make(chan os.Signal, 1) + signal.Notify(sigChan, syscall.SIGUSR1) + + for { + select { + case sig := <-sigChan: + log.Info("Received signal, triggering a new scan", "signal", sig) + start := time.Now() + err := scanner.RescanAll(ctx, false) + if err != nil { + log.Error("Error scanning", err) + } + log.Info("Triggered scan complete", "elapsed", time.Since(start).Round(100*time.Millisecond)) + case <-ctx.Done(): + return nil + } + } + } +} + func schedulePeriodicScan(ctx context.Context) func() error { return func() error { schedule := conf.Server.ScanSchedule diff --git a/cmd/signaler_nonunix.go b/cmd/signaler_nonunix.go deleted file mode 100644 index aad970382..000000000 --- a/cmd/signaler_nonunix.go +++ /dev/null @@ -1,27 +0,0 @@ -//go:build windows || plan9 - -package cmd - -import ( - "context" - "os" - "os/signal" - - "github.com/navidrome/navidrome/log" -) - -func startSignaler(ctx context.Context) func() error { - log.Info(ctx, "Starting signaler") - - return func() error { - var sigChan = make(chan os.Signal, 1) - signal.Notify(sigChan, os.Interrupt) - select { - case sig := <-sigChan: - log.Info(ctx, "Received termination signal", "signal", sig) - return interrupted - case <-ctx.Done(): - return nil - } - } -} diff --git a/cmd/signaler_unix.go b/cmd/signaler_unix.go deleted file mode 100644 index 5ed5b1457..000000000 --- a/cmd/signaler_unix.go +++ /dev/null @@ -1,51 +0,0 @@ -//go:build !windows && !plan9 - -package cmd - -import ( - "context" - "os" - "os/signal" - "syscall" - "time" - - "github.com/navidrome/navidrome/log" -) - -const triggerScanSignal = syscall.SIGUSR1 - -func startSignaler(ctx context.Context) func() error { - log.Info(ctx, "Starting signaler") - scanner := GetScanner() - - return func() error { - var sigChan = make(chan os.Signal, 1) - signal.Notify( - sigChan, - os.Interrupt, - triggerScanSignal, - syscall.SIGHUP, - syscall.SIGTERM, - syscall.SIGABRT, - ) - - for { - select { - case sig := <-sigChan: - if sig != triggerScanSignal { - log.Info(ctx, "Received termination signal", "signal", sig) - return interrupted - } - log.Info(ctx, "Received signal, triggering a new scan", "signal", sig) - start := time.Now() - err := scanner.RescanAll(ctx, false) - if err != nil { - log.Error(ctx, "Error scanning", err) - } - log.Info(ctx, "Triggered scan complete", "elapsed", time.Since(start).Round(100*time.Millisecond)) - case <-ctx.Done(): - return nil - } - } - } -}