From 08a9bf9630cc9d148fa9e3ec00cfa790e8304fcf Mon Sep 17 00:00:00 2001 From: Stephen Afam-Osemene Date: Thu, 29 Feb 2024 23:36:19 +0000 Subject: [PATCH] Add ErrRestart to signal that a player should restart --- conductor.go | 17 ++++++++++------- errors.go | 4 ++++ 2 files changed, 14 insertions(+), 7 deletions(-) diff --git a/conductor.go b/conductor.go index 417f451..8da2833 100644 --- a/conductor.go +++ b/conductor.go @@ -2,6 +2,7 @@ package orchestra import ( "context" + "errors" "fmt" "log/slog" "sync" @@ -42,7 +43,7 @@ func (c *Conductor) playWithLogger(ctx context.Context, logger Logger) error { defer cancel() // shutdown players no matter how it exits // This will be called after the main context is cancelled - timedCtx, cancelTimed := context.WithCancel(context.Background()) + timedCtx, cancelTimed := context.WithCancel(context.WithoutCancel(ctx)) defer cancelTimed() // release resources at the end regardless if c.Timeout < 1 { @@ -100,15 +101,17 @@ func (c *Conductor) conductPlayer(ctx context.Context, wg *sync.WaitGroup, lock l.Log("starting player", slog.String("name", name)) - var err error - if c, ok := p.(*Conductor); ok { - err = c.playWithLogger(ctx, subConductorLogger{name: name, l: l}) - } else { - err = p.Play(ctx) + err := ErrRestart + for errors.Is(err, ErrRestart) { + if c, ok := p.(*Conductor); ok { + err = c.playWithLogger(ctx, subConductorLogger{name: name, l: l}) + } else { + err = p.Play(ctx) + } } if err != nil { - l.Log("error in " + name) + l.Log("error in player", slog.String("name", name)) errs <- InstrumentError{name, err} } l.Log("stopped player", slog.String("name", name)) diff --git a/errors.go b/errors.go index ad0e4f3..39c68ac 100644 --- a/errors.go +++ b/errors.go @@ -1,10 +1,14 @@ package orchestra import ( + "errors" "fmt" "strings" ) +// Return this error from a player to signal the conductor to restart it +var ErrRestart = errors.New("restart") + // InstrumentError is an error that happens in an instrument started by a conductor // It carries the name of the instrument type InstrumentError struct {