Use singleton in other places as well

This commit is contained in:
Deluan 2021-06-21 18:41:11 -04:00
parent 1f997357a9
commit 743e469795
7 changed files with 39 additions and 104 deletions

View file

@ -10,6 +10,7 @@ import (
"github.com/navidrome/navidrome/consts"
"github.com/navidrome/navidrome/db"
"github.com/navidrome/navidrome/log"
"github.com/navidrome/navidrome/scheduler"
"github.com/oklog/run"
"github.com/spf13/cobra"
"github.com/spf13/viper"
@ -121,10 +122,10 @@ func startSignaler() (func() error, func(err error)) {
func schedulePeriodicScan(schedule string) {
scanner := GetScanner()
scheduler := GetScheduler()
schedulerInstance := scheduler.GetInstance()
log.Info("Scheduling periodic scan", "schedule", schedule)
err := scheduler.Add(schedule, func() {
err := schedulerInstance.Add(schedule, func() {
_ = scanner.RescanAll(context.Background(), false)
})
if err != nil {
@ -140,11 +141,11 @@ func schedulePeriodicScan(schedule string) {
func startScheduler() (func() error, func(err error)) {
log.Info("Starting scheduler")
scheduler := GetScheduler()
schedulerInstance := scheduler.GetInstance()
ctx, cancel := context.WithCancel(context.Background())
return func() error {
scheduler.Run(ctx)
schedulerInstance.Run(ctx)
return nil
}, func(err error) {

View file

@ -13,7 +13,6 @@ import (
"github.com/navidrome/navidrome/core/transcoder"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/scanner"
"github.com/navidrome/navidrome/scheduler"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/server/nativeapi"
@ -31,7 +30,7 @@ func CreateServer(musicFolder string) *server.Server {
func CreateNativeAPIRouter() *nativeapi.Router {
dataStore := persistence.New()
broker := GetBroker()
broker := events.GetBroker()
share := core.NewShare(dataStore)
router := nativeapi.New(dataStore, broker, share)
return router
@ -48,8 +47,8 @@ func CreateSubsonicAPIRouter() *subsonic.Router {
players := core.NewPlayers(dataStore)
externalMetadata := core.NewExternalMetadata(dataStore)
scanner := GetScanner()
broker := GetBroker()
scrobblerScrobbler := scrobbler.New(dataStore)
broker := events.GetBroker()
scrobblerScrobbler := scrobbler.GetInstance(dataStore)
router := subsonic.New(dataStore, artwork, mediaStreamer, archiver, players, externalMetadata, scanner, broker, scrobblerScrobbler)
return router
}
@ -65,24 +64,14 @@ func createScanner() scanner.Scanner {
artworkCache := core.GetImageCache()
artwork := core.NewArtwork(dataStore, artworkCache)
cacheWarmer := core.NewCacheWarmer(artwork, artworkCache)
broker := GetBroker()
broker := events.GetBroker()
scannerScanner := scanner.New(dataStore, cacheWarmer, broker)
return scannerScanner
}
func createBroker() events.Broker {
broker := events.NewBroker()
return broker
}
func createScheduler() scheduler.Scheduler {
schedulerScheduler := scheduler.New()
return schedulerScheduler
}
// wire_injectors.go:
var allProviders = wire.NewSet(core.Set, subsonic.New, nativeapi.New, persistence.New, lastfm.NewRouter, GetBroker)
var allProviders = wire.NewSet(core.Set, subsonic.New, nativeapi.New, persistence.New, lastfm.NewRouter, events.GetBroker)
// Scanner must be a Singleton
var (
@ -96,29 +85,3 @@ func GetScanner() scanner.Scanner {
})
return scannerInstance
}
// Broker must be a Singleton
var (
onceBroker sync.Once
brokerInstance events.Broker
)
func GetBroker() events.Broker {
onceBroker.Do(func() {
brokerInstance = createBroker()
})
return brokerInstance
}
// Scheduler must be a Singleton
var (
onceScheduler sync.Once
schedulerInstance scheduler.Scheduler
)
func GetScheduler() scheduler.Scheduler {
onceScheduler.Do(func() {
schedulerInstance = createScheduler()
})
return schedulerInstance
}

View file

@ -5,14 +5,14 @@ package cmd
import (
"sync"
"github.com/navidrome/navidrome/server/events"
"github.com/google/wire"
"github.com/navidrome/navidrome/core"
"github.com/navidrome/navidrome/core/agents/lastfm"
"github.com/navidrome/navidrome/persistence"
"github.com/navidrome/navidrome/scanner"
"github.com/navidrome/navidrome/scheduler"
"github.com/navidrome/navidrome/server"
"github.com/navidrome/navidrome/server/events"
"github.com/navidrome/navidrome/server/nativeapi"
"github.com/navidrome/navidrome/server/subsonic"
)
@ -23,7 +23,7 @@ var allProviders = wire.NewSet(
nativeapi.New,
persistence.New,
lastfm.NewRouter,
GetBroker,
events.GetBroker,
)
func CreateServer(musicFolder string) *server.Server {
@ -71,41 +71,3 @@ func createScanner() scanner.Scanner {
scanner.New,
))
}
// Broker must be a Singleton
var (
onceBroker sync.Once
brokerInstance events.Broker
)
func GetBroker() events.Broker {
onceBroker.Do(func() {
brokerInstance = createBroker()
})
return brokerInstance
}
func createBroker() events.Broker {
panic(wire.Build(
events.NewBroker,
))
}
// Scheduler must be a Singleton
var (
onceScheduler sync.Once
schedulerInstance scheduler.Scheduler
)
func GetScheduler() scheduler.Scheduler {
onceScheduler.Do(func() {
schedulerInstance = createScheduler()
})
return schedulerInstance
}
func createScheduler() scheduler.Scheduler {
panic(wire.Build(
scheduler.New,
))
}