From 8af325b7f8c109da355cc11e674b82c048fc0f16 Mon Sep 17 00:00:00 2001 From: Wessie Date: Tue, 4 Jun 2024 00:17:29 +0100 Subject: [PATCH] manager: refactor a bit more NewManager now asks for the things it needs instead of taking a config. Manager.runStatusUpdates now doesn't run the finish-up on the initial song --- manager/api.go | 13 +++++++++++-- manager/main.go | 16 +++++++--------- 2 files changed, 18 insertions(+), 11 deletions(-) diff --git a/manager/api.go b/manager/api.go index d876278..b850068 100644 --- a/manager/api.go +++ b/manager/api.go @@ -173,6 +173,9 @@ func (m *Manager) runStatusUpdates(ctx context.Context) { var listenerCount radio.Listeners var songStartListenerCount radio.Listeners + // indicates if this is the first song, we don't want to call finishSong + // on the initial song until an actual song change occurs afterwards + var firstSong = true for { var sendStatus = true var songUpdate = false @@ -191,8 +194,14 @@ func (m *Manager) runStatusUpdates(ctx context.Context) { m.mu.Lock() // if we're about to update the song, we need to do some bookkeeping on // the previous song - if err := m.finishSong(ctx, m.status, songStartListenerCount); err != nil { - zerolog.Ctx(ctx).Error().Err(err).Msg("failed finishSong") + if songUpdate { + if !firstSong { // don't finish the initial song + if err := m.finishSong(ctx, m.status, songStartListenerCount); err != nil { + zerolog.Ctx(ctx).Error().Err(err).Msg("failed finishSong") + } + } else { + firstSong = false + } } // update user diff --git a/manager/main.go b/manager/main.go index e42e16c..7922930 100644 --- a/manager/main.go +++ b/manager/main.go @@ -18,7 +18,12 @@ import ( // Execute executes a manager with the context and configuration given; it returns with // any error that occurs; Execution can be interrupted by canceling the context given. func Execute(ctx context.Context, cfg config.Config) error { - m, err := NewManager(ctx, cfg) + store, err := storage.Open(ctx, cfg) + if err != nil { + return err + } + + m, err := NewManager(ctx, store) if err != nil { return err } @@ -52,14 +57,8 @@ func Execute(ctx context.Context, cfg config.Config) error { } // NewManager returns a manager ready for use -func NewManager(ctx context.Context, cfg config.Config) (*Manager, error) { - store, err := storage.Open(ctx, cfg) - if err != nil { - return nil, err - } - +func NewManager(ctx context.Context, store radio.StorageService) (*Manager, error) { m := Manager{ - Config: cfg, logger: zerolog.Ctx(ctx), Storage: store, status: radio.Status{}, @@ -86,7 +85,6 @@ func NewManager(ctx context.Context, cfg config.Config) (*Manager, error) { // Manager manages shared state between different processes type Manager struct { - config.Config logger *zerolog.Logger Storage radio.StorageService