Skip to content

Commit

Permalink
Clean up runNavidrome function
Browse files Browse the repository at this point in the history
  • Loading branch information
deluan committed May 9, 2024
1 parent c4b05da commit 885cd34
Show file tree
Hide file tree
Showing 4 changed files with 40 additions and 19 deletions.
47 changes: 31 additions & 16 deletions cmd/root.go
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,9 @@ import (
"github.com/navidrome/navidrome/scheduler"
"github.com/navidrome/navidrome/server/backgrounds"
"github.com/prometheus/client_golang/prometheus/promhttp"
"golang.org/x/sync/errgroup"

"github.com/spf13/cobra"
"github.com/spf13/viper"
"golang.org/x/sync/errgroup"
)

var interrupted = errors.New("service was interrupted")
Expand All @@ -43,10 +42,14 @@ Complete documentation is available at https://www.navidrome.org/docs`,
Run: func(cmd *cobra.Command, args []string) {
runNavidrome()
},
PostRun: func(cmd *cobra.Command, args []string) {
postRun()
},
Version: consts.Version,
}
)

// Execute runs the root cobra command, which will start the Navidrome server by calling the runNavidrome function.
func Execute() {
rootCmd.SetVersionTemplate(`{{println .Version}}`)
if err := rootCmd.Execute(); err != nil {
Expand All @@ -62,21 +65,17 @@ func preRun() {
conf.Load()
}

func postRun() {
log.Info("Navidrome stopped, bye.")
}

// runNavidrome is the main entry point for the Navidrome server. It starts all the services and blocks.
// If any of the services returns an error, it will log it and exit. If the process receives a signal to exit,
// it will cancel the context and exit gracefully.
func runNavidrome() {
db.Init()
defer func() {
if err := db.Close(); err != nil {
log.Error("Error closing DB", err)
}
log.Info("Navidrome stopped, bye.")
}()
defer db.Init()()

ctx, cancel := signal.NotifyContext(context.Background(),
os.Interrupt,
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGABRT,
)
ctx, cancel := mainContext()
defer cancel()

g, ctx := errgroup.WithContext(ctx)
Expand All @@ -91,6 +90,17 @@ func runNavidrome() {
}
}

// mainContext returns a context that is cancelled when the process receives a signal to exit.
func mainContext() (context.Context, context.CancelFunc) {
return signal.NotifyContext(context.Background(),
os.Interrupt,
syscall.SIGHUP,
syscall.SIGTERM,
syscall.SIGABRT,
)
}

// startServer starts the Navidrome web server, adding all the necessary routers.
func startServer(ctx context.Context) func() error {
return func() error {
a := CreateServer(conf.Server.MusicFolder)
Expand Down Expand Up @@ -118,6 +128,7 @@ func startServer(ctx context.Context) func() error {
}
}

// schedulePeriodicScan schedules a periodic scan of the music library, if configured.
func schedulePeriodicScan(ctx context.Context) func() error {
return func() error {
schedule := conf.Server.ScanSchedule
Expand Down Expand Up @@ -147,6 +158,7 @@ func schedulePeriodicScan(ctx context.Context) func() error {
}
}

// startScheduler starts the Navidrome scheduler, which is used to run periodic tasks.
func startScheduler(ctx context.Context) func() error {
return func() error {
log.Info(ctx, "Starting scheduler")
Expand All @@ -156,12 +168,15 @@ func startScheduler(ctx context.Context) func() error {
}
}

// startPlaybackServer starts the Navidrome playback server, if configured.
// It is responsible for the Jukebox functionality
func startPlaybackServer(ctx context.Context) func() error {
return func() error {
if !conf.Server.Jukebox.Enabled {
log.Debug("Jukebox is DISABLED")
return nil
}
log.Info(ctx, "Starting playback server")
log.Info(ctx, "Starting Jukebox service")
playbackInstance := GetPlaybackServer()
return playbackInstance.Run(ctx)
}
Expand Down
8 changes: 7 additions & 1 deletion db/db.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ func Close() error {
return Db().Close()
}

func Init() {
func Init() func() {
db := Db()

// Disable foreign_keys to allow re-creating tables in migrations
Expand Down Expand Up @@ -74,6 +74,12 @@ func Init() {
if err != nil {
log.Fatal("Failed to apply new migrations", err)
}

return func() {
if err := Close(); err != nil {
log.Error("Error closing DB", err)
}
}
}

type statusLogger struct{ numPending int }
Expand Down
2 changes: 1 addition & 1 deletion persistence/persistence_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ func TestPersistence(t *testing.T) {
//os.Remove("./test-123.db")
//conf.Server.DbPath = "./test-123.db"
conf.Server.DbPath = "file::memory:?cache=shared"
db.Init()
defer db.Init()()
log.SetLevel(log.LevelError)
RegisterFailHandler(Fail)
RunSpecs(t, "Persistence Suite")
Expand Down
2 changes: 1 addition & 1 deletion scanner/scanner_suite_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
func TestScanner(t *testing.T) {
tests.Init(t, true)
conf.Server.DbPath = "file::memory:?cache=shared"
db.Init()
defer db.Init()()
log.SetLevel(log.LevelFatal)
RegisterFailHandler(Fail)
RunSpecs(t, "Scanner Suite")
Expand Down

0 comments on commit 885cd34

Please sign in to comment.