From 0372339e1b24d80183f1407c3cd2eb03094493a3 Mon Sep 17 00:00:00 2001 From: Deluan Date: Wed, 5 Mar 2025 14:18:27 -0800 Subject: [PATCH] fix(server): only build core.Agents once Signed-off-by: Deluan --- cmd/wire_gen.go | 8 ++++---- core/agents/agents.go | 9 ++++++++- core/agents/agents_test.go | 4 ++-- core/wire_providers.go | 2 +- 4 files changed, 15 insertions(+), 8 deletions(-) diff --git a/cmd/wire_gen.go b/cmd/wire_gen.go index d44b78ed8..e5e72bf4f 100644 --- a/cmd/wire_gen.go +++ b/cmd/wire_gen.go @@ -65,7 +65,7 @@ func CreateSubsonicAPIRouter(ctx context.Context) *subsonic.Router { dataStore := persistence.New(sqlDB) fileCache := artwork.GetImageCache() fFmpeg := ffmpeg.New() - agentsAgents := agents.New(dataStore) + agentsAgents := agents.GetAgents(dataStore) externalMetadata := core.NewExternalMetadata(dataStore, agentsAgents) artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, externalMetadata) transcodingCache := core.GetTranscodingCache() @@ -89,7 +89,7 @@ func CreatePublicRouter() *public.Router { dataStore := persistence.New(sqlDB) fileCache := artwork.GetImageCache() fFmpeg := ffmpeg.New() - agentsAgents := agents.New(dataStore) + agentsAgents := agents.GetAgents(dataStore) externalMetadata := core.NewExternalMetadata(dataStore, agentsAgents) artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, externalMetadata) transcodingCache := core.GetTranscodingCache() @@ -133,7 +133,7 @@ func CreateScanner(ctx context.Context) scanner.Scanner { dataStore := persistence.New(sqlDB) fileCache := artwork.GetImageCache() fFmpeg := ffmpeg.New() - agentsAgents := agents.New(dataStore) + agentsAgents := agents.GetAgents(dataStore) externalMetadata := core.NewExternalMetadata(dataStore, agentsAgents) artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, externalMetadata) cacheWarmer := artwork.NewCacheWarmer(artworkArtwork, fileCache) @@ -149,7 +149,7 @@ func CreateScanWatcher(ctx context.Context) scanner.Watcher { dataStore := persistence.New(sqlDB) fileCache := artwork.GetImageCache() fFmpeg := ffmpeg.New() - agentsAgents := agents.New(dataStore) + agentsAgents := agents.GetAgents(dataStore) externalMetadata := core.NewExternalMetadata(dataStore, agentsAgents) artworkArtwork := artwork.NewArtwork(dataStore, fileCache, fFmpeg, externalMetadata) cacheWarmer := artwork.NewCacheWarmer(artworkArtwork, fileCache) diff --git a/core/agents/agents.go b/core/agents/agents.go index 335ebc57f..50a1e04ad 100644 --- a/core/agents/agents.go +++ b/core/agents/agents.go @@ -10,6 +10,7 @@ import ( "github.com/navidrome/navidrome/log" "github.com/navidrome/navidrome/model" "github.com/navidrome/navidrome/utils" + "github.com/navidrome/navidrome/utils/singleton" ) type Agents struct { @@ -17,7 +18,13 @@ type Agents struct { agents []Interface } -func New(ds model.DataStore) *Agents { +func GetAgents(ds model.DataStore) *Agents { + return singleton.GetInstance(func() *Agents { + return createAgents(ds) + }) +} + +func createAgents(ds model.DataStore) *Agents { var order []string if conf.Server.Agents != "" { order = strings.Split(conf.Server.Agents, ",") diff --git a/core/agents/agents_test.go b/core/agents/agents_test.go index 53ca5099d..ea12fb746 100644 --- a/core/agents/agents_test.go +++ b/core/agents/agents_test.go @@ -29,7 +29,7 @@ var _ = Describe("Agents", func() { var ag *Agents BeforeEach(func() { conf.Server.Agents = "" - ag = New(ds) + ag = createAgents(ds) }) It("calls the placeholder GetArtistImages", func() { @@ -49,7 +49,7 @@ var _ = Describe("Agents", func() { Register("disabled", func(model.DataStore) Interface { return nil }) Register("empty", func(model.DataStore) Interface { return &emptyAgent{} }) conf.Server.Agents = "empty,fake,disabled" - ag = New(ds) + ag = createAgents(ds) Expect(ag.AgentName()).To(Equal("agents")) }) diff --git a/core/wire_providers.go b/core/wire_providers.go index 2a1a71dbe..6f9d326ec 100644 --- a/core/wire_providers.go +++ b/core/wire_providers.go @@ -17,7 +17,7 @@ var Set = wire.NewSet( NewPlayers, NewShare, NewPlaylists, - agents.New, + agents.GetAgents, ffmpeg.New, scrobbler.GetPlayTracker, playback.GetInstance,