-
Notifications
You must be signed in to change notification settings - Fork 126
/
main.go
230 lines (175 loc) · 4.66 KB
/
main.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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
package main
import (
"fmt"
"sync"
"time"
"github.com/joho/godotenv"
"golang.org/x/sync/errgroup"
"github.com/hatchet-dev/hatchet/pkg/client"
"github.com/hatchet-dev/hatchet/pkg/cmdutils"
"github.com/hatchet-dev/hatchet/pkg/worker"
)
const NUM_CHILDREN = 50
type proceduralChildInput struct {
Index int `json:"index"`
}
type proceduralChildOutput struct {
Index int `json:"index"`
}
type proceduralParentOutput struct {
ChildSum int `json:"child_sum"`
}
func main() {
err := godotenv.Load()
if err != nil {
panic(err)
}
events := make(chan string, 5*NUM_CHILDREN)
interrupt := cmdutils.InterruptChan()
cleanup, err := run(events)
if err != nil {
panic(err)
}
<-interrupt
if err := cleanup(); err != nil {
panic(fmt.Errorf("error cleaning up: %w", err))
}
}
func run(events chan<- string) (func() error, error) {
c, err := client.New()
if err != nil {
return nil, fmt.Errorf("error creating client: %w", err)
}
w, err := worker.NewWorker(
worker.WithClient(
c,
),
)
if err != nil {
return nil, fmt.Errorf("error creating worker: %w", err)
}
testSvc := w.NewService("test")
err = testSvc.On(
worker.NoTrigger(),
&worker.WorkflowJob{
Name: "procedural-parent-workflow",
Description: "This is a test of procedural workflows.",
Steps: []*worker.WorkflowStep{
worker.Fn(
func(ctx worker.HatchetContext) (result *proceduralParentOutput, err error) {
childWorkflows := make([]*worker.ChildWorkflow, NUM_CHILDREN)
for i := 0; i < NUM_CHILDREN; i++ {
childInput := proceduralChildInput{
Index: i,
}
childWorkflow, err := ctx.SpawnWorkflow("procedural-child-workflow", childInput, &worker.SpawnWorkflowOpts{})
if err != nil {
return nil, err
}
childWorkflows[i] = childWorkflow
events <- fmt.Sprintf("child-%d-started", i)
}
eg := errgroup.Group{}
eg.SetLimit(NUM_CHILDREN)
childOutputs := make([]int, 0)
childOutputsMu := sync.Mutex{}
for i, childWorkflow := range childWorkflows {
eg.Go(func(i int, childWorkflow *worker.ChildWorkflow) func() error {
return func() error {
childResult, err := childWorkflow.Result()
if err != nil {
return err
}
childOutput := proceduralChildOutput{}
err = childResult.StepOutput("step-one", &childOutput)
if err != nil {
return err
}
childOutputsMu.Lock()
childOutputs = append(childOutputs, childOutput.Index)
childOutputsMu.Unlock()
events <- fmt.Sprintf("child-%d-completed", childOutput.Index)
return nil
}
}(i, childWorkflow))
}
finishedCh := make(chan struct{})
go func() {
defer close(finishedCh)
err = eg.Wait()
}()
timer := time.NewTimer(60 * time.Second)
select {
case <-finishedCh:
if err != nil {
return nil, err
}
case <-timer.C:
incomplete := make([]int, 0)
// print non-complete children
for i := range childWorkflows {
completed := false
for _, childOutput := range childOutputs {
if childOutput == i {
completed = true
break
}
}
if !completed {
incomplete = append(incomplete, i)
}
}
return nil, fmt.Errorf("timed out waiting for the following child workflows to complete: %v", incomplete)
}
sum := 0
for _, childOutput := range childOutputs {
sum += childOutput
}
return &proceduralParentOutput{
ChildSum: sum,
}, nil
},
).SetTimeout("10m"),
},
},
)
if err != nil {
return nil, fmt.Errorf("error registering workflow: %w", err)
}
err = testSvc.On(
worker.NoTrigger(),
&worker.WorkflowJob{
Name: "procedural-child-workflow",
Description: "This is a test of procedural workflows.",
Steps: []*worker.WorkflowStep{
worker.Fn(
func(ctx worker.HatchetContext) (result *proceduralChildOutput, err error) {
input := proceduralChildInput{}
err = ctx.WorkflowInput(&input)
if err != nil {
return nil, err
}
return &proceduralChildOutput{
Index: input.Index,
}, nil
},
).SetName("step-one"),
},
},
)
if err != nil {
return nil, fmt.Errorf("error registering workflow: %w", err)
}
go func() {
time.Sleep(1 * time.Second)
_, err := c.Admin().RunWorkflow("procedural-parent-workflow", nil)
if err != nil {
panic(fmt.Errorf("error running workflow: %w", err))
}
}()
cleanup, err := w.Start()
if err != nil {
panic(err)
}
return cleanup, nil
}