-
Notifications
You must be signed in to change notification settings - Fork 23
Expand file tree
/
Copy pathrun_scenario_with_worker.go
More file actions
103 lines (94 loc) · 2.75 KB
/
run_scenario_with_worker.go
File metadata and controls
103 lines (94 loc) · 2.75 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
package cli
import (
"context"
"fmt"
"github.com/spf13/cobra"
"github.com/spf13/pflag"
"github.com/temporalio/omes/cmd/clioptions"
"github.com/temporalio/omes/workers"
)
func runScenarioWithWorkerCmd() *cobra.Command {
var r workerWithScenarioRunner
cmd := &cobra.Command{
Use: "run-scenario-with-worker",
Short: "Run a worker and a scenario",
PreRun: func(cmd *cobra.Command, args []string) {
r.preRun()
},
Run: func(cmd *cobra.Command, args []string) {
ctx, cancel := withCancelOnInterrupt(cmd.Context())
defer cancel()
if err := r.run(ctx); err != nil {
r.Logger.Fatal(err)
}
},
}
r.addCLIFlags(cmd.Flags())
cmd.MarkFlagRequired("scenario")
cmd.MarkFlagRequired("language")
return cmd
}
type workerWithScenarioRunner struct {
workerRunner
scenarioRunConfig
metricsOptions clioptions.MetricsOptions
}
func (r *workerWithScenarioRunner) addCLIFlags(fs *pflag.FlagSet) {
r.workerRunner.addCLIFlags(fs)
r.scenarioRunConfig.addCLIFlags(fs)
fs.AddFlagSet(r.metricsOptions.FlagSet(""))
}
func (r *workerWithScenarioRunner) preRun() {
r.workerRunner.preRun()
}
func (r *workerWithScenarioRunner) run(ctx context.Context) error {
ctx, cancel := context.WithCancel(ctx)
defer cancel()
// Start worker and wait on error or started
workerErrCh := make(chan error, 1)
workerStartCh := make(chan struct{})
r.OnWorkerStarted = func() { close(workerStartCh) }
go func() {
repoDir, err := getRepoDir()
if err != nil {
workerErrCh <- fmt.Errorf("failed to get root directory: %w", err)
return
}
workerErrCh <- r.Run(ctx, workers.BaseDir(repoDir, r.SdkOptions.Language))
}()
select {
case err := <-workerErrCh:
return fmt.Errorf("worker did not start: %w", err)
case <-workerStartCh:
}
// Run scenario
scenarioRunner := scenarioRunner{
logger: r.Logger,
scenario: r.ScenarioID,
scenarioRunConfig: scenarioRunConfig{
iterations: r.iterations,
duration: r.duration,
maxConcurrent: r.maxConcurrent,
maxIterationsPerSecond: r.maxIterationsPerSecond,
scenarioOptions: r.scenarioOptions,
timeout: r.timeout,
verificationTimeout: r.verificationTimeout,
doNotRegisterSearchAttributes: r.doNotRegisterSearchAttributes,
},
clientOptions: r.ClientOptions,
metricsOptions: r.metricsOptions,
}
scenarioErr := scenarioRunner.run(ctx)
cancel()
// Wait for worker complete
workerErr := <-workerErrCh
if scenarioErr != nil {
if workerErr != nil {
return fmt.Errorf("worker failed with: %v, scenario failed with: %w", workerErr, scenarioErr)
}
return fmt.Errorf("scenario failed: %w", scenarioErr)
} else if workerErr != nil {
return fmt.Errorf("worker failed: %w", workerErr)
}
return nil
}