Skip to content

Commit

Permalink
Add ErrRestart to signal that a player should restart
Browse files Browse the repository at this point in the history
  • Loading branch information
stephenafamo committed Feb 29, 2024
1 parent c48513f commit 08a9bf9
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 7 deletions.
17 changes: 10 additions & 7 deletions conductor.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package orchestra

import (
"context"
"errors"
"fmt"
"log/slog"
"sync"
Expand Down Expand Up @@ -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 {
Expand Down Expand Up @@ -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))
Expand Down
4 changes: 4 additions & 0 deletions errors.go
Original file line number Diff line number Diff line change
@@ -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 {
Expand Down

0 comments on commit 08a9bf9

Please sign in to comment.