-
Notifications
You must be signed in to change notification settings - Fork 145
/
child.go
75 lines (58 loc) · 1.54 KB
/
child.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
package worker
import (
"encoding/json"
"fmt"
"github.com/rs/zerolog"
"github.com/hatchet-dev/hatchet/pkg/client"
dispatchercontracts "github.com/hatchet-dev/hatchet/internal/services/dispatcher/contracts"
)
type ChildWorkflow struct {
workflowRunId string
// client client.Client
l *zerolog.Logger
listener *client.WorkflowRunsListener
}
type ChildWorkflowResult struct {
workflowRun *dispatchercontracts.WorkflowRunEvent
}
func (r *ChildWorkflowResult) StepOutput(key string, v interface{}) error {
var outputBytes []byte
for _, stepRunResult := range r.workflowRun.Results {
if stepRunResult.StepReadableId == key {
if stepRunResult.Error != nil {
return fmt.Errorf("step run failed: %s", *stepRunResult.Error)
}
if stepRunResult.Output != nil {
outputBytes = []byte(*stepRunResult.Output)
}
}
}
if outputBytes == nil {
return fmt.Errorf("step output for %s not found", key)
}
if err := json.Unmarshal(outputBytes, v); err != nil {
return fmt.Errorf("failed to unmarshal output: %w", err)
}
return nil
}
func (c *ChildWorkflow) Result() (*ChildWorkflowResult, error) {
resChan := make(chan *ChildWorkflowResult)
err := c.listener.AddWorkflowRun(
c.workflowRunId,
func(event client.WorkflowRunEvent) error {
// non-blocking send
select {
case resChan <- &ChildWorkflowResult{
workflowRun: event,
}: // continue
default:
}
return nil
},
)
if err != nil {
return nil, fmt.Errorf("failed to listen for workflow events: %w", err)
}
res := <-resChan
return res, nil
}