-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrunner.go
105 lines (84 loc) · 1.91 KB
/
runner.go
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
104
105
package tempo
import (
"context"
"errors"
"fmt"
"sync"
"go.temporal.io/api/enums/v1"
"go.temporal.io/sdk/client"
)
func Run(cli client.Client, queue string, id string, plan Plan, output any) error {
// TODO expose this to outside control
opts := client.StartWorkflowOptions{
ID: id,
TaskQueue: queue,
WorkflowIDReusePolicy: enums.WORKFLOW_ID_REUSE_POLICY_ALLOW_DUPLICATE_FAILED_ONLY,
WorkflowIDConflictPolicy: enums.WORKFLOW_ID_CONFLICT_POLICY_USE_EXISTING,
}
run, err := cli.ExecuteWorkflow(context.Background(), opts, plan.name, plan.input)
if err != nil {
return errors.Join(ErrWorkflowExecute, err)
}
err = run.Get(context.Background(), output)
if err != nil {
return errors.Join(ErrWorkflow, err)
}
return nil
}
func NewRunner(client client.Client, queue string, plans ...Plan) *Runner {
return &Runner{
client: client,
queue: queue,
plans: plans,
}
}
type Runner struct {
client client.Client
queue string
plans []Plan
limit chan struct{}
}
func (r *Runner) SetLimit(limit int) {
if limit < 0 {
r.limit = nil
return
}
if len(r.limit) != 0 {
panic(fmt.Errorf("tempo: modify limit while %v goroutines in the group are still active", len(r.limit)))
}
r.limit = make(chan struct{}, limit)
}
func (r *Runner) Run(prefix string) error {
var total = len(r.plans)
errs := make(chan error, total)
var wg sync.WaitGroup
wg.Add(total)
for _, plan := range r.plans {
go func(fn Plan) {
if r.limit != nil {
r.limit <- struct{}{}
}
defer func() {
if r.limit != nil {
<-r.limit
}
wg.Done()
}()
id := fmt.Sprintf("%s@%s", prefix, plan.name)
err := Run(r.client, r.queue, id, plan, nil)
if err != nil {
errs <- errors.Join(ErrTest, err)
}
}(plan)
}
wg.Wait()
close(errs)
if len(errs) == 0 {
return nil
}
var err error
for e := range errs {
err = errors.Join(err, e)
}
return err
}