Repository navigation
Expand file tree
/
Copy pathflow.go
More file actions
153 lines (129 loc) · 4.69 KB
/
Copy pathflow.go
File metadata and controls
153 lines (129 loc) · 4.69 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
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
package gush
import (
"context"
"time"
"go.temporal.io/sdk/client"
)
// FlowHandlerFunc is the signature of a flow.
type FlowHandlerFunc func(ctx *FlowContext) (output any, err error)
// FlowHandler is a flow registered under an explicit name.
type FlowHandler struct {
Name string
Func FlowHandlerFunc
}
// NewFlowHandler names a flow. Use it when the function's own name is not unique or not
// stable, such as for a closure.
//
// handler := gush.NewFlowHandler("data-pipeline", DataPipeline)
// worker := gush.NewWorker("my-queue").WithFlowHandlers(handler)
func NewFlowHandler(name string, fn FlowHandlerFunc) *FlowHandler {
handler := &FlowHandler{Name: name, Func: fn}
return handler
}
// FlowRequest says which flow to run, where, and with what data.
type FlowRequest struct {
// Queue is the task queue to run on. A client must set it; a child flow left empty
// runs on its parent's queue.
Queue string
// Flow is the flow function or the name it is registered under.
Flow any
// Input is the flow's input. See Encode for how it is serialized.
Input any
// Output receives the result of RunFlow, unless it is nil. StartFlow does not use it.
Output any
}
// FlowInfo describes a flow execution.
type FlowInfo struct {
ID string // generated if not set at start
Status string // Running, Completed, Failed, Canceled, Terminated, TimedOut or ContinuedAsNew
ParentID string // empty for a top-level flow
Queue string
Priority Priority
FairKey string
// Attempt counts from 1. It is set inside a running flow; GetFlow leaves it zero
// because Temporal does not report it there.
Attempt int32
StartTime *time.Time
CloseTime *time.Time // nil while running
}
// RunFlow starts a flow and waits for its result.
func (c *Client) RunFlow(ctx context.Context, request FlowRequest, options ...FlowOption) error {
config := DefaultFlowConfig()
config.Apply(options...)
run, err := c.startFlow(ctx, request, config)
if err != nil {
return err
}
var result []byte
err = run.Get(ctx, &result)
if err != nil {
return unwrapError(err)
}
return DecodePayload(result, request.Output)
}
// StartFlow starts a flow and returns its ID without waiting for it to finish.
func (c *Client) StartFlow(ctx context.Context, request FlowRequest, options ...FlowOption) (string, error) {
config := DefaultFlowConfig()
config.Apply(options...)
run, err := c.startFlow(ctx, request, config)
if err != nil {
return "", err
}
return run.GetID(), nil
}
func (c *Client) startFlow(ctx context.Context, request FlowRequest, config *FlowConfig) (client.WorkflowRun, error) {
flowName, err := handlerName("flow", request.Flow)
if err != nil {
return nil, err
}
payload, err := EncodePayload(request.Input)
if err != nil {
return nil, err
}
err = config.validate()
if err != nil {
return nil, err
}
startOptions := newStartWorkflowOptions(request.Queue, config)
return c.temporalClient.ExecuteWorkflow(ctx, startOptions, flowName, payload, config.LogAttributes)
}
// CancelFlow asks a flow to stop. Its steps and the children it waits for are canceled too,
// so a RunStep in progress returns a canceled error, or waits first with WithWaitForCancel.
// A child begun with StartFlow is independent and keeps running. Cleanup steps
// need a disconnected context; see FlowContext.WithContext. A step that is already running
// only finds out when it calls Heartbeat, and WithHeartbeatTimeout makes that prompt.
func (c *Client) CancelFlow(ctx context.Context, flowID string) error {
return c.temporalClient.CancelWorkflow(ctx, flowID, "")
}
// TerminateFlow stops a flow immediately, with no cleanup. Prefer CancelFlow.
func (c *Client) TerminateFlow(ctx context.Context, flowID, reason string) error {
return c.temporalClient.TerminateWorkflow(ctx, flowID, "", reason)
}
// GetFlow describes a flow execution.
func (c *Client) GetFlow(ctx context.Context, flowID string) (*FlowInfo, error) {
description, err := c.temporalClient.DescribeWorkflowExecution(ctx, flowID, "")
if err != nil {
return nil, err
}
execution := description.WorkflowExecutionInfo
flow := &FlowInfo{Status: execution.Status.String(), Queue: execution.TaskQueue}
if execution.Execution != nil {
flow.ID = execution.Execution.WorkflowId
}
if execution.ParentExecution != nil {
flow.ParentID = execution.ParentExecution.WorkflowId
}
if execution.Priority != nil {
flow.Priority = NewPriority(int(execution.Priority.GetPriorityKey()))
flow.FairKey = execution.Priority.GetFairnessKey()
}
if execution.StartTime != nil {
startTime := execution.GetStartTime().AsTime()
flow.StartTime = &startTime
}
if execution.CloseTime != nil {
closeTime := execution.GetCloseTime().AsTime()
flow.CloseTime = &closeTime
}
return flow, nil
}