runner.go
This commit is contained in:
+8
-43
@@ -19,6 +19,7 @@ import (
|
||||
"github.com/qvest-digital/go-mxl/mxl"
|
||||
"github.com/spf13/pflag"
|
||||
|
||||
"mxl-pattern-generator/internal/app"
|
||||
"mxl-pattern-generator/internal/audio"
|
||||
"mxl-pattern-generator/internal/flowdef"
|
||||
"mxl-pattern-generator/internal/video"
|
||||
@@ -59,16 +60,6 @@ type parseResult struct {
|
||||
shouldRun bool
|
||||
}
|
||||
|
||||
type namedRunner struct {
|
||||
name string
|
||||
run func(context.Context) error
|
||||
}
|
||||
|
||||
type runnerResult struct {
|
||||
name string
|
||||
err error
|
||||
}
|
||||
|
||||
var frameRates = map[string]mxl.Rational{
|
||||
"23.97": {Num: 24000, Den: 1001},
|
||||
"24": {Num: 24, Den: 1},
|
||||
@@ -464,48 +455,22 @@ func run(ctx context.Context, args appArgs) (runErr error) {
|
||||
}
|
||||
}()
|
||||
|
||||
runners := []namedRunner{
|
||||
runners := []app.Runner{
|
||||
{
|
||||
name: "video",
|
||||
run: func(ctx context.Context) error {
|
||||
Name: "video",
|
||||
Run: func(ctx context.Context) error {
|
||||
return video.Run(ctx, inst, videoCfg)
|
||||
},
|
||||
},
|
||||
}
|
||||
if audioCfg != nil {
|
||||
runners = append(runners, namedRunner{
|
||||
name: "audio",
|
||||
run: func(ctx context.Context) error {
|
||||
runners = append(runners, app.Runner{
|
||||
Name: "audio",
|
||||
Run: func(ctx context.Context) error {
|
||||
return audio.Run(ctx, inst, *audioCfg)
|
||||
},
|
||||
})
|
||||
}
|
||||
|
||||
return runConcurrent(ctx, runners...)
|
||||
}
|
||||
|
||||
func runConcurrent(ctx context.Context, runners ...namedRunner) error {
|
||||
if len(runners) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
ctx, cancel := context.WithCancel(ctx)
|
||||
defer cancel()
|
||||
results := make(chan runnerResult, len(runners))
|
||||
for _, runner := range runners {
|
||||
runner := runner
|
||||
go func() {
|
||||
results <- runnerResult{name: runner.name, err: runner.run(ctx)}
|
||||
}()
|
||||
}
|
||||
|
||||
var resultErr error
|
||||
for range runners {
|
||||
result := <-results
|
||||
if result.err != nil {
|
||||
resultErr = errors.Join(resultErr, fmt.Errorf("%s flow: %w", result.name, result.err))
|
||||
cancel()
|
||||
}
|
||||
}
|
||||
return resultErr
|
||||
return app.RunConcurrent(ctx, runners...)
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user