-
Notifications
You must be signed in to change notification settings - Fork 175
/
Copy pathworkflow.go
268 lines (242 loc) · 7.94 KB
/
workflow.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
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/*
Copyright 2024 The Dapr Authors
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
package client
import (
"context"
"encoding/json"
"errors"
"fmt"
"time"
"github.com/google/uuid"
pb "github.com/dapr/dapr/pkg/proto/runtime/v1"
)
const (
DefaultWorkflowComponent = "dapr"
)
type StartWorkflowRequest struct {
InstanceID string // Optional instance identifier
WorkflowComponent string
WorkflowName string
Options map[string]string // Optional metadata
Input any // Optional input
SendRawInput bool // Set to True in order to disable serialization on the input
}
type StartWorkflowResponse struct {
InstanceID string
}
type GetWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
type GetWorkflowResponse struct {
InstanceID string
WorkflowName string
CreatedAt time.Time
LastUpdatedAt time.Time
RuntimeStatus string
Properties map[string]string
}
type PurgeWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
type TerminateWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
type PauseWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
type ResumeWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
type RaiseEventWorkflowRequest struct {
InstanceID string
WorkflowComponent string
EventName string
EventData any
SendRawData bool // Set to True in order to disable serialization on the data
}
// StartWorkflowBeta1 starts a workflow using the beta1 spec.
func (c *GRPCClient) StartWorkflowBeta1(ctx context.Context, req *StartWorkflowRequest) (*StartWorkflowResponse, error) {
if req.InstanceID == "" {
req.InstanceID = uuid.New().String()
}
if req.WorkflowComponent == "" {
req.WorkflowComponent = DefaultWorkflowComponent
}
if req.WorkflowName == "" {
return nil, errors.New("failed to start workflow: WorkflowName must be supplied")
}
var input []byte
var err error
if req.SendRawInput {
var ok bool
if input, ok = req.Input.([]byte); !ok {
return nil, errors.New("failed to start workflow: sendrawinput is true however, input is not a byte slice")
}
} else {
input, err = marshalInput(req.Input)
if err != nil {
return nil, fmt.Errorf("failed to start workflow: %v", err)
}
}
resp, err := c.protoClient.StartWorkflowBeta1(ctx, &pb.StartWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
WorkflowName: req.WorkflowName,
Options: req.Options,
Input: input,
})
if err != nil {
return nil, fmt.Errorf("failed to start workflow instance: %v", err)
}
return &StartWorkflowResponse{
InstanceID: resp.GetInstanceId(),
}, nil
}
// GetWorkflowBeta1 gets the status of a workflow using the beta1 spec.
func (c *GRPCClient) GetWorkflowBeta1(ctx context.Context, req *GetWorkflowRequest) (*GetWorkflowResponse, error) {
if req.InstanceID == "" {
return nil, errors.New("failed to get workflow status: InstanceID must be supplied")
}
if req.WorkflowComponent == "" {
req.WorkflowComponent = DefaultWorkflowComponent
}
resp, err := c.protoClient.GetWorkflowBeta1(ctx, &pb.GetWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
})
if err != nil {
return nil, fmt.Errorf("failed to get workflow status: %v", err)
}
return &GetWorkflowResponse{
InstanceID: resp.GetInstanceId(),
WorkflowName: resp.GetWorkflowName(),
CreatedAt: resp.GetCreatedAt().AsTime(),
LastUpdatedAt: resp.GetLastUpdatedAt().AsTime(),
RuntimeStatus: resp.GetRuntimeStatus(),
Properties: resp.GetProperties(),
}, nil
}
// PurgeWorkflowBeta1 removes all metadata relating to a specific workflow using the beta1 spec.
func (c *GRPCClient) PurgeWorkflowBeta1(ctx context.Context, req *PurgeWorkflowRequest) error {
if req.InstanceID == "" {
return errors.New("failed to purge workflow: InstanceID must be supplied")
}
if req.WorkflowComponent == "" {
req.WorkflowComponent = DefaultWorkflowComponent
}
_, err := c.protoClient.PurgeWorkflowBeta1(ctx, &pb.PurgeWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
})
if err != nil {
return fmt.Errorf("failed to purge workflow: %v", err)
}
return nil
}
// TerminateWorkflowBeta1 stops a workflow using the beta1 spec.
func (c *GRPCClient) TerminateWorkflowBeta1(ctx context.Context, req *TerminateWorkflowRequest) error {
if req.InstanceID == "" {
return errors.New("failed to terminate workflow: InstanceID must be supplied")
}
if req.WorkflowComponent == "" {
req.WorkflowComponent = DefaultWorkflowComponent
}
_, err := c.protoClient.TerminateWorkflowBeta1(ctx, &pb.TerminateWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
})
if err != nil {
return fmt.Errorf("failed to terminate workflow: %v", err)
}
return nil
}
// PauseWorkflowBeta1 pauses a workflow that can be resumed later using the beta1 spec.
func (c *GRPCClient) PauseWorkflowBeta1(ctx context.Context, req *PauseWorkflowRequest) error {
if req.InstanceID == "" {
return errors.New("failed to pause workflow: InstanceID must be supplied")
}
if req.WorkflowComponent == "" {
req.WorkflowComponent = DefaultWorkflowComponent
}
_, err := c.protoClient.PauseWorkflowBeta1(ctx, &pb.PauseWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
})
if err != nil {
return fmt.Errorf("failed to pause workflow: %v", err)
}
return nil
}
// ResumeWorkflowBeta1 resumes a paused workflow using the beta1 spec.
func (c *GRPCClient) ResumeWorkflowBeta1(ctx context.Context, req *ResumeWorkflowRequest) error {
if req.InstanceID == "" {
return errors.New("failed to resume workflow: InstanceID must be supplied")
}
if req.WorkflowComponent == "" {
req.WorkflowComponent = DefaultWorkflowComponent
}
_, err := c.protoClient.ResumeWorkflowBeta1(ctx, &pb.ResumeWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
})
if err != nil {
return fmt.Errorf("failed to resume workflow: %v", err)
}
return nil
}
// RaiseEventWorkflowBeta1 raises an event on a workflow using the beta1 spec.
func (c *GRPCClient) RaiseEventWorkflowBeta1(ctx context.Context, req *RaiseEventWorkflowRequest) error {
if req.InstanceID == "" {
return errors.New("failed to raise event on workflow: InstanceID must be supplied")
}
if req.WorkflowComponent == "" {
req.WorkflowComponent = DefaultWorkflowComponent
}
if req.EventName == "" {
return errors.New("failed to raise event on workflow: EventName must be supplied")
}
var eventData []byte
var err error
if req.SendRawData {
var ok bool
if eventData, ok = req.EventData.([]byte); !ok {
return errors.New("failed to raise event on workflow: SendRawData is true however, eventData is not a byte slice")
}
} else {
eventData, err = marshalInput(req.EventData)
if err != nil {
return fmt.Errorf("failed to raise an event on workflow: %v", err)
}
}
_, err = c.protoClient.RaiseEventWorkflowBeta1(ctx, &pb.RaiseEventWorkflowRequest{
InstanceId: req.InstanceID,
WorkflowComponent: req.WorkflowComponent,
EventName: req.EventName,
EventData: eventData,
})
if err != nil {
return fmt.Errorf("failed to raise event on workflow: %v", err)
}
return nil
}
func marshalInput(input any) (data []byte, err error) {
if input == nil {
return nil, nil
}
return json.Marshal(input)
}