@@ -20,16 +20,11 @@ import (
20
20
"log"
21
21
"time"
22
22
23
- "github.com/dapr/go-sdk/client"
24
23
"github.com/dapr/go-sdk/workflow"
25
24
)
26
25
27
26
var stage = 0
28
27
29
- const (
30
- workflowComponent = "dapr"
31
- )
32
-
33
28
func main () {
34
29
w , err := workflow .NewWorker ()
35
30
if err != nil {
@@ -54,70 +49,49 @@ func main() {
54
49
}
55
50
fmt .Println ("runner started" )
56
51
57
- daprClient , err := client .NewClient ()
52
+ wfClient , err := workflow .NewClient ()
58
53
if err != nil {
59
54
log .Fatalf ("failed to intialise client: %v" , err )
60
55
}
61
- defer daprClient .Close ()
56
+ defer wfClient .Close ()
62
57
ctx := context .Background ()
63
58
64
59
// Start workflow test
65
- respStart , err := daprClient .StartWorkflowBeta1 (ctx , & client.StartWorkflowRequest {
66
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
67
- WorkflowComponent : workflowComponent ,
68
- WorkflowName : "TestWorkflow" ,
69
- Options : nil ,
70
- Input : 1 ,
71
- SendRawInput : false ,
72
- })
60
+ instanceID , err := wfClient .ScheduleNewWorkflow (ctx , "TestWorkflow" , workflow .WithInstanceID ("a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ), workflow .WithInput (1 ))
73
61
if err != nil {
74
62
log .Fatalf ("failed to start workflow: %v" , err )
75
63
}
76
- fmt .Printf ("workflow started with id: %v\n " , respStart . InstanceID )
64
+ fmt .Printf ("workflow started with id: %v\n " , instanceID )
77
65
78
66
// Pause workflow test
79
- err = daprClient .PauseWorkflowBeta1 (ctx , & client.PauseWorkflowRequest {
80
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
81
- WorkflowComponent : workflowComponent ,
82
- })
83
-
67
+ err = wfClient .SuspendWorkflow (ctx , instanceID , "" )
84
68
if err != nil {
85
69
log .Fatalf ("failed to pause workflow: %v" , err )
86
70
}
87
71
88
- respGet , err := daprClient .GetWorkflowBeta1 (ctx , & client.GetWorkflowRequest {
89
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
90
- WorkflowComponent : workflowComponent ,
91
- })
72
+ respFetch , err := wfClient .FetchWorkflowMetadata (ctx , instanceID , workflow .WithFetchPayloads (true ))
92
73
if err != nil {
93
- log .Fatalf ("failed to get workflow: %v" , err )
74
+ log .Fatalf ("failed to fetch workflow: %v" , err )
94
75
}
95
76
96
- if respGet .RuntimeStatus != workflow .StatusSuspended . String () {
97
- log .Fatalf ("workflow not paused: %v" , respGet .RuntimeStatus )
77
+ if respFetch .RuntimeStatus != workflow .StatusSuspended {
78
+ log .Fatalf ("workflow not paused: %v" , respFetch .RuntimeStatus )
98
79
}
99
80
100
81
fmt .Printf ("workflow paused\n " )
101
82
102
83
// Resume workflow test
103
- err = daprClient .ResumeWorkflowBeta1 (ctx , & client.ResumeWorkflowRequest {
104
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
105
- WorkflowComponent : workflowComponent ,
106
- })
107
-
84
+ err = wfClient .ResumeWorkflow (ctx , instanceID , "" )
108
85
if err != nil {
109
86
log .Fatalf ("failed to resume workflow: %v" , err )
110
87
}
111
88
112
- respGet , err = daprClient .GetWorkflowBeta1 (ctx , & client.GetWorkflowRequest {
113
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
114
- WorkflowComponent : workflowComponent ,
115
- })
89
+ respFetch , err = wfClient .FetchWorkflowMetadata (ctx , instanceID , workflow .WithFetchPayloads (true ))
116
90
if err != nil {
117
91
log .Fatalf ("failed to get workflow: %v" , err )
118
92
}
119
93
120
- if respGet .RuntimeStatus != workflow .StatusRunning . String () {
94
+ if respFetch .RuntimeStatus != workflow .StatusRunning {
121
95
log .Fatalf ("workflow not running" )
122
96
}
123
97
@@ -127,14 +101,7 @@ func main() {
127
101
128
102
// Raise Event Test
129
103
130
- err = daprClient .RaiseEventWorkflowBeta1 (ctx , & client.RaiseEventWorkflowRequest {
131
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
132
- WorkflowComponent : workflowComponent ,
133
- EventName : "testEvent" ,
134
- EventData : "testData" ,
135
- SendRawData : false ,
136
- })
137
-
104
+ err = wfClient .RaiseEvent (ctx , instanceID , "testEvent" , workflow .WithEventPayload ("testData" ))
138
105
if err != nil {
139
106
fmt .Printf ("failed to raise event: %v" , err )
140
107
}
@@ -145,152 +112,53 @@ func main() {
145
112
146
113
fmt .Printf ("stage: %d\n " , stage )
147
114
148
- respGet , err = daprClient .GetWorkflowBeta1 (ctx , & client.GetWorkflowRequest {
149
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
150
- WorkflowComponent : workflowComponent ,
151
- })
115
+ respFetch , err = wfClient .FetchWorkflowMetadata (ctx , instanceID , workflow .WithFetchPayloads (true ))
152
116
if err != nil {
153
117
log .Fatalf ("failed to get workflow: %v" , err )
154
118
}
155
119
156
- fmt .Printf ("workflow status: %v\n " , respGet .RuntimeStatus )
120
+ fmt .Printf ("workflow status: %v\n " , respFetch .RuntimeStatus )
157
121
158
122
// Purge workflow test
159
- err = daprClient .PurgeWorkflowBeta1 (ctx , & client.PurgeWorkflowRequest {
160
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
161
- WorkflowComponent : workflowComponent ,
162
- })
123
+ err = wfClient .PurgeWorkflow (ctx , instanceID )
163
124
if err != nil {
164
125
log .Fatalf ("failed to purge workflow: %v" , err )
165
126
}
166
127
167
- respGet , err = daprClient .GetWorkflowBeta1 (ctx , & client.GetWorkflowRequest {
168
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
169
- WorkflowComponent : workflowComponent ,
170
- })
171
- if err != nil && respGet != nil {
172
- log .Fatal ("failed to purge workflow" )
128
+ respFetch , err = wfClient .FetchWorkflowMetadata (ctx , instanceID , workflow .WithFetchPayloads (true ))
129
+ if err == nil || respFetch != nil {
130
+ log .Fatalf ("failed to purge workflow: %v" , err )
173
131
}
174
132
175
133
fmt .Println ("workflow purged" )
176
134
177
135
fmt .Printf ("stage: %d\n " , stage )
178
136
179
137
// Terminate workflow test
180
- respStart , err = daprClient .StartWorkflowBeta1 (ctx , & client.StartWorkflowRequest {
181
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
182
- WorkflowComponent : workflowComponent ,
183
- WorkflowName : "TestWorkflow" ,
184
- Options : nil ,
185
- Input : 1 ,
186
- SendRawInput : false ,
187
- })
138
+ id , err := wfClient .ScheduleNewWorkflow (ctx , "TestWorkflow" , workflow .WithInstanceID ("a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ), workflow .WithInput (1 ))
188
139
if err != nil {
189
140
log .Fatalf ("failed to start workflow: %v" , err )
190
141
}
142
+ fmt .Printf ("workflow started with id: %v\n " , instanceID )
191
143
192
- fmt .Printf ("workflow started with id: %s\n " , respStart .InstanceID )
193
-
194
- err = daprClient .TerminateWorkflowBeta1 (ctx , & client.TerminateWorkflowRequest {
195
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
196
- WorkflowComponent : workflowComponent ,
197
- })
144
+ metadata , err := wfClient .WaitForWorkflowStart (ctx , id )
198
145
if err != nil {
199
- log .Fatalf ("failed to terminate workflow: %v" , err )
146
+ log .Fatalf ("failed to get workflow: %v" , err )
200
147
}
148
+ fmt .Printf ("workflow status: %s\n " , metadata .RuntimeStatus .String ())
201
149
202
- respGet , err = daprClient .GetWorkflowBeta1 (ctx , & client.GetWorkflowRequest {
203
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
204
- WorkflowComponent : workflowComponent ,
205
- })
150
+ err = wfClient .TerminateWorkflow (ctx , id )
206
151
if err != nil {
207
- log .Fatalf ("failed to get workflow: %v" , err )
208
- }
209
- if respGet .RuntimeStatus != workflow .StatusTerminated .String () {
210
- log .Fatal ("failed to terminate workflow" )
152
+ log .Fatalf ("failed to terminate workflow: %v" , err )
211
153
}
212
-
213
154
fmt .Println ("workflow terminated" )
214
155
215
- err = daprClient .PurgeWorkflowBeta1 (ctx , & client.PurgeWorkflowRequest {
216
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
217
- WorkflowComponent : workflowComponent ,
218
- })
219
-
220
- respGet , err = daprClient .GetWorkflowBeta1 (ctx , & client.GetWorkflowRequest {
221
- InstanceID : "a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ,
222
- WorkflowComponent : workflowComponent ,
223
- })
224
- if err == nil || respGet != nil {
156
+ err = wfClient .PurgeWorkflow (ctx , id )
157
+ if err != nil {
225
158
log .Fatalf ("failed to purge workflow: %v" , err )
226
159
}
227
-
228
160
fmt .Println ("workflow purged" )
229
161
230
- // WFClient
231
- // TODO: Expand client validation
232
-
233
- stage = 0
234
- fmt .Println ("workflow client test" )
235
-
236
- wfClient , err := workflow .NewClient ()
237
- if err != nil {
238
- log .Fatalf ("[wfclient] faield to initialize: %v" , err )
239
- }
240
-
241
- id , err := wfClient .ScheduleNewWorkflow (ctx , "TestWorkflow" , workflow .WithInstanceID ("a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9" ), workflow .WithInput (1 ))
242
- if err != nil {
243
- log .Fatalf ("[wfclient] failed to start workflow: %v" , err )
244
- }
245
-
246
- fmt .Printf ("[wfclient] started workflow with id: %s\n " , id )
247
-
248
- metadata , err := wfClient .FetchWorkflowMetadata (ctx , id )
249
- if err != nil {
250
- log .Fatalf ("[wfclient] failed to get worfklow: %v" , err )
251
- }
252
-
253
- fmt .Printf ("[wfclient] workflow status: %v\n " , metadata .RuntimeStatus .String ())
254
-
255
- if stage != 1 {
256
- log .Fatalf ("Workflow assertion failed while validating the wfclient. Stage 1 expected, current: %d" , stage )
257
- }
258
-
259
- fmt .Printf ("[wfclient] stage: %d\n " , stage )
260
-
261
- // TODO: WaitForWorkflowStart
262
- // TODO: WaitForWorkflowCompletion
263
-
264
- // raise event
265
-
266
- if err := wfClient .RaiseEvent (ctx , id , "testEvent" , workflow .WithEventPayload ("testData" )); err != nil {
267
- log .Fatalf ("[wfclient] failed to raise event: %v" , err )
268
- }
269
-
270
- fmt .Println ("[wfclient] event raised" )
271
-
272
- // Sleep to allow the workflow to advance
273
- time .Sleep (time .Second )
274
-
275
- if stage != 2 {
276
- log .Fatalf ("Workflow assertion failed while validating the wfclient. Stage 2 expected, current: %d" , stage )
277
- }
278
-
279
- fmt .Printf ("[wfclient] stage: %d\n " , stage )
280
-
281
- // stop workflow
282
- if err := wfClient .TerminateWorkflow (ctx , id ); err != nil {
283
- log .Fatalf ("[wfclient] failed to terminate workflow: %v" , err )
284
- }
285
-
286
- fmt .Println ("[wfclient] workflow terminated" )
287
-
288
- if err := wfClient .PurgeWorkflow (ctx , id ); err != nil {
289
- log .Fatalf ("[wfclient] failed to purge workflow: %v" , err )
290
- }
291
-
292
- fmt .Println ("[wfclient] workflow purged" )
293
-
294
162
// stop workflow runtime
295
163
if err := w .Shutdown (); err != nil {
296
164
log .Fatalf ("failed to shutdown runtime: %v" , err )
0 commit comments