mirror of
https://github.com/navidrome/navidrome.git
synced 2025-04-04 13:07:36 +03:00
Fix SIGUSR1 work when ScanInterval=0
This commit is contained in:
parent
a35de2bfd1
commit
8a07bac2a2
3 changed files with 45 additions and 19 deletions
53
cmd/root.go
53
cmd/root.go
|
@ -53,8 +53,16 @@ func runNavidrome() {
|
||||||
db.EnsureLatestVersion()
|
db.EnsureLatestVersion()
|
||||||
|
|
||||||
var g run.Group
|
var g run.Group
|
||||||
|
|
||||||
g.Add(startServer())
|
g.Add(startServer())
|
||||||
g.Add(startScanner())
|
g.Add(startSignaler())
|
||||||
|
|
||||||
|
interval := conf.Server.ScanInterval
|
||||||
|
if interval != 0 {
|
||||||
|
g.Add(startScanner(interval))
|
||||||
|
} else {
|
||||||
|
log.Warn("Periodic scan is DISABLED", "interval", interval)
|
||||||
|
}
|
||||||
|
|
||||||
if err := g.Run(); err != nil {
|
if err := g.Run(); err != nil {
|
||||||
log.Error("Fatal error in Navidrome. Aborting", err)
|
log.Error("Fatal error in Navidrome. Aborting", err)
|
||||||
|
@ -76,20 +84,45 @@ func startServer() (func() error, func(err error)) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func startScanner() (func() error, func(err error)) {
|
var sigChan = make(chan os.Signal, 1)
|
||||||
interval := conf.Server.ScanInterval
|
|
||||||
|
func startSignaler() (func() error, func(err error)) {
|
||||||
|
scanner := GetScanner()
|
||||||
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
|
return func() error {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case sig := <-sigChan:
|
||||||
|
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():
|
||||||
|
break
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}, func(err error) {
|
||||||
|
cancel()
|
||||||
|
if err != nil {
|
||||||
|
log.Error("Shutting down Signaler due to error", err)
|
||||||
|
} else {
|
||||||
|
log.Info("Shutting down Signaler")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func startScanner(interval time.Duration) (func() error, func(err error)) {
|
||||||
log.Info("Starting scanner", "interval", interval.String())
|
log.Info("Starting scanner", "interval", interval.String())
|
||||||
scanner := GetScanner()
|
scanner := GetScanner()
|
||||||
ctx, cancel := context.WithCancel(context.Background())
|
ctx, cancel := context.WithCancel(context.Background())
|
||||||
|
|
||||||
return func() error {
|
return func() error {
|
||||||
if interval != 0 {
|
time.Sleep(2 * time.Second) // Wait 2 seconds before the first scan
|
||||||
time.Sleep(2 * time.Second) // Wait 2 seconds before the first scan
|
scanner.Run(ctx, interval)
|
||||||
scanner.Run(ctx, interval)
|
|
||||||
} else {
|
|
||||||
log.Warn("Periodic scan is DISABLED", "interval", interval)
|
|
||||||
<-ctx.Done()
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}, func(err error) {
|
}, func(err error) {
|
||||||
|
|
|
@ -1,7 +1,7 @@
|
||||||
// +build !windows
|
// +build !windows
|
||||||
// +build !plan9
|
// +build !plan9
|
||||||
|
|
||||||
package scanner
|
package cmd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"os"
|
"os"
|
|
@ -4,7 +4,6 @@ import (
|
||||||
"context"
|
"context"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"os"
|
|
||||||
"strconv"
|
"strconv"
|
||||||
"sync"
|
"sync"
|
||||||
"time"
|
"time"
|
||||||
|
@ -40,10 +39,7 @@ type FolderScanner interface {
|
||||||
Scan(ctx context.Context, lastModifiedSince time.Time, progress chan uint32) error
|
Scan(ctx context.Context, lastModifiedSince time.Time, progress chan uint32) error
|
||||||
}
|
}
|
||||||
|
|
||||||
var (
|
var isScanning utils.AtomicBool
|
||||||
isScanning utils.AtomicBool
|
|
||||||
sigChan = make(chan os.Signal, 1)
|
|
||||||
)
|
|
||||||
|
|
||||||
type scanner struct {
|
type scanner struct {
|
||||||
folders map[string]FolderScanner
|
folders map[string]FolderScanner
|
||||||
|
@ -87,9 +83,6 @@ func (s *scanner) Run(ctx context.Context, interval time.Duration) {
|
||||||
select {
|
select {
|
||||||
case <-ticker.C:
|
case <-ticker.C:
|
||||||
continue
|
continue
|
||||||
case sig := <-sigChan:
|
|
||||||
log.Info(ctx, "Received signal, triggering a new scan", "signal", sig)
|
|
||||||
continue
|
|
||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue