Skip to content

Commit

Permalink
Use signal.NotifyContext
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed Apr 28, 2024
1 parent 31fcab0 commit 3d9fff3
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 79 deletions.
37 changes: 36 additions & 1 deletion cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,9 @@ import (
"errors"
"fmt"
"os"
"os/signal"
"strings"
"syscall"
"time"

"github.com/go-chi/chi/v5/middleware"
Expand Down Expand Up @@ -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))
Expand Down Expand Up @@ -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
Expand Down
27 changes: 0 additions & 27 deletions cmd/signaler_nonunix.go

This file was deleted.

51 changes: 0 additions & 51 deletions cmd/signaler_unix.go

This file was deleted.

0 comments on commit 3d9fff3

Please sign in to comment.