Skip to content

Commit 4372027

Browse files
committed
update howto author wfs
Signed-off-by: Cassandra Coyle <[email protected]>
1 parent 3d7ae77 commit 4372027

File tree

1 file changed

+105
-18
lines changed

1 file changed

+105
-18
lines changed

daprdocs/content/en/developing-applications/building-blocks/workflow/howto-author-workflow.md

Lines changed: 105 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -197,10 +197,12 @@ public class DemoWorkflowActivity implements WorkflowActivity {
197197

198198
<!--go-->
199199

200+
### Define workflow activities
201+
200202
Define each workflow activity you'd like your workflow to perform. The Activity input can be unmarshalled from the context with `ctx.GetInput`. Activities should be defined as taking a `ctx workflow.ActivityContext` parameter and returning an interface and error.
201203

202204
```go
203-
func TestActivity(ctx workflow.ActivityContext) (any, error) {
205+
func BusinessActivity(ctx workflow.ActivityContext) (any, error) {
204206
var input int
205207
if err := ctx.GetInput(&input); err != nil {
206208
return "", err
@@ -211,6 +213,92 @@ func TestActivity(ctx workflow.ActivityContext) (any, error) {
211213
}
212214
```
213215

216+
### Define the workflow
217+
218+
Define your workflow function with the parameter `ctx *workflow.WorkflowContext` and return any and error. Invoke your defined activities from within your workflow.
219+
220+
```go
221+
func BusinessWorkflow(ctx *workflow.WorkflowContext) (any, error) {
222+
var input int
223+
if err := ctx.GetInput(&input); err != nil {
224+
return nil, err
225+
}
226+
var output string
227+
if err := ctx.CallActivity(BusinessActivity, workflow.ActivityInput(input)).Await(&output); err != nil {
228+
return nil, err
229+
}
230+
if err := ctx.WaitForExternalEvent("businessEvent", time.Second*60).Await(&output); err != nil {
231+
return nil, err
232+
}
233+
234+
if err := ctx.CreateTimer(time.Second).Await(nil); err != nil {
235+
return nil, nil
236+
}
237+
return output, nil
238+
}
239+
```
240+
241+
### Register workflows and activities
242+
243+
Before your application can execute workflows, you must register both the workflow orchestrator and its activities with a workflow worker. This is a crucial step that ensures Dapr knows which functions to call when executing your workflow.
244+
245+
```go
246+
func main() {
247+
// Create a workflow worker
248+
w, err := workflow.NewWorker()
249+
if err != nil {
250+
log.Fatal(err)
251+
}
252+
fmt.Println("Worker initialized")
253+
254+
// Register the workflow orchestrator
255+
if err := w.RegisterWorkflow(BusinessWorkflow); err != nil {
256+
log.Fatal(err)
257+
}
258+
fmt.Println("BusinessWorkflow registered")
259+
260+
// Register the workflow activities
261+
if err := w.RegisterActivity(BusinessActivity); err != nil {
262+
log.Fatal(err)
263+
}
264+
fmt.Println("BusinessActivity registered")
265+
266+
// Start workflow worker
267+
if err := w.Start(); err != nil {
268+
log.Fatal(err)
269+
}
270+
fmt.Println("Workflow worker started")
271+
272+
// Create workflow client for managing workflows
273+
wfClient, err := workflow.NewClient()
274+
if err != nil {
275+
log.Fatalf("failed to initialize client: %v", err)
276+
}
277+
defer wfClient.Close()
278+
279+
// Your application logic continues here...
280+
// Example: Start a workflow
281+
ctx := context.Background()
282+
instanceID, err := wfClient.ScheduleNewWorkflow(ctx, "BusinessWorkflow", workflow.WithInput(1))
283+
if err != nil {
284+
log.Fatalf("failed to start workflow: %v", err)
285+
}
286+
fmt.Printf("workflow started with id: %v\n", instanceID)
287+
288+
// Stop workflow worker when done
289+
if err := w.Shutdown(); err != nil {
290+
log.Fatalf("failed to shutdown worker: %v", err)
291+
}
292+
}
293+
```
294+
295+
**Key points about registration:**
296+
- Use `workflow.NewWorker()` to create a workflow worker
297+
- Use `w.RegisterWorkflow()` to register workflow functions
298+
- Use `w.RegisterActivity()` to register activity functions
299+
- Call `w.Start()` to begin processing workflows
300+
- Use `workflow.NewClient()` to create a client for managing workflows
301+
214302
[See the Go SDK workflow activity example in context.](https://github.com/dapr/go-sdk/tree/main/examples/workflow/README.md)
215303

216304
{{% /tab %}}
@@ -383,16 +471,16 @@ public class DemoWorkflowWorker {
383471
Define your workflow function with the parameter `ctx *workflow.WorkflowContext` and return any and error. Invoke your defined activities from within your workflow.
384472

385473
```go
386-
func TestWorkflow(ctx *workflow.WorkflowContext) (any, error) {
474+
func BusinessWorkflow(ctx *workflow.WorkflowContext) (any, error) {
387475
var input int
388476
if err := ctx.GetInput(&input); err != nil {
389477
return nil, err
390478
}
391479
var output string
392-
if err := ctx.CallActivity(TestActivity, workflow.ActivityInput(input)).Await(&output); err != nil {
480+
if err := ctx.CallActivity(BusinessActivity, workflow.ActivityInput(input)).Await(&output); err != nil {
393481
return nil, err
394482
}
395-
if err := ctx.WaitForExternalEvent("testEvent", time.Second*60).Await(&output); err != nil {
483+
if err := ctx.WaitForExternalEvent("businessEvent", time.Second*60).Await(&output); err != nil {
396484
return nil, err
397485
}
398486

@@ -864,7 +952,7 @@ public class DemoWorkflow extends Workflow {
864952
[As in the following example](https://github.com/dapr/go-sdk/tree/main/examples/workflow/README.md), a hello-world application using the Go SDK and Dapr Workflow would include:
865953
866954
- A Go package called `client` to receive the Go SDK client capabilities.
867-
- The `TestWorkflow` method
955+
- The `BusinessWorkflow` method
868956
- Creating the workflow with input and output.
869957
- API calls. In the example below, these calls start and call the workflow activities.
870958
@@ -889,15 +977,15 @@ var failActivityTries = 0
889977
func main() {
890978
r := workflow.NewRegistry()
891979

892-
if err := r.AddWorkflow(TestWorkflow); err != nil {
980+
if err := r.AddWorkflow(BusinessWorkflow); err != nil {
893981
log.Fatal(err)
894982
}
895-
fmt.Println("TestWorkflow registered")
983+
fmt.Println("BusinessWorkflow registered")
896984

897-
if err := r.AddActivity(TestActivity); err != nil {
985+
if err := r.AddActivity(BusinessActivity); err != nil {
898986
log.Fatal(err)
899987
}
900-
fmt.Println("TestActivity registered")
988+
fmt.Println("BusinessActivity registered")
901989

902990
if err := r.AddActivity(FailActivity); err != nil {
903991
log.Fatal(err)
@@ -921,7 +1009,7 @@ func main() {
9211009
// "start". This is useful for increasing the throughput of creating
9221010
// workflows.
9231011
// workflow.WithStartTime(time.Now())
924-
instanceID, err := wclient.ScheduleWorkflow(ctx, "TestWorkflow", workflow.WithInstanceID("a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9"), workflow.WithInput(1))
1012+
instanceID, err := wclient.ScheduleWorkflow(ctx, "BusinessWorkflow", workflow.WithInstanceID("a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9"), workflow.WithInput(1))
9251013
if err != nil {
9261014
log.Fatalf("failed to start workflow: %v", err)
9271015
}
@@ -963,9 +1051,8 @@ func main() {
9631051

9641052
fmt.Printf("stage: %d\n", stage)
9651053

966-
// Raise Event Test
967-
968-
err = wclient.RaiseEvent(ctx, instanceID, "testEvent", workflow.WithEventPayload("testData"))
1054+
// Raise Event
1055+
err = wclient.RaiseEvent(ctx, instanceID, "businessEvent", workflow.WithEventPayload("testData"))
9691056
if err != nil {
9701057
fmt.Printf("failed to raise event: %v", err)
9711058
}
@@ -1037,22 +1124,22 @@ func main() {
10371124
fmt.Println("workflow worker successfully shutdown")
10381125
}
10391126

1040-
func TestWorkflow(ctx *workflow.WorkflowContext) (any, error) {
1127+
func BusinessWorkflow(ctx *workflow.WorkflowContext) (any, error) {
10411128
var input int
10421129
if err := ctx.GetInput(&input); err != nil {
10431130
return nil, err
10441131
}
10451132
var output string
1046-
if err := ctx.CallActivity(TestActivity, workflow.WithActivityInput(input)).Await(&output); err != nil {
1133+
if err := ctx.CallActivity(BusinessActivity, workflow.WithActivityInput(input)).Await(&output); err != nil {
10471134
return nil, err
10481135
}
10491136

1050-
err := ctx.WaitForExternalEvent("testEvent", time.Second*60).Await(&output)
1137+
err := ctx.WaitForExternalEvent("businessEvent", time.Second*60).Await(&output)
10511138
if err != nil {
10521139
return nil, err
10531140
}
10541141

1055-
if err := ctx.CallActivity(TestActivity, workflow.WithActivityInput(input)).Await(&output); err != nil {
1142+
if err := ctx.CallActivity(BusinessActivity, workflow.WithActivityInput(input)).Await(&output); err != nil {
10561143
return nil, err
10571144
}
10581145

@@ -1068,7 +1155,7 @@ func TestWorkflow(ctx *workflow.WorkflowContext) (any, error) {
10681155
return output, nil
10691156
}
10701157

1071-
func TestActivity(ctx workflow.ActivityContext) (any, error) {
1158+
func BusinessActivity(ctx workflow.ActivityContext) (any, error) {
10721159
var input int
10731160
if err := ctx.GetInput(&input); err != nil {
10741161
return "", err

0 commit comments

Comments
 (0)