Skip to content

Commit

Permalink
manager: refactor a bit more
Browse files Browse the repository at this point in the history
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
  • Loading branch information
Wessie committed Jun 3, 2024
1 parent c506adc commit 8af325b
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 11 deletions.
13 changes: 11 additions & 2 deletions manager/api.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
Expand Down
16 changes: 7 additions & 9 deletions manager/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
}
Expand Down Expand Up @@ -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{},
Expand All @@ -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
Expand Down

0 comments on commit 8af325b

Please sign in to comment.