Skip to content

Commit 44e2756

Browse files
committed
update go code to use vanity client
Signed-off-by: Cassandra Coyle <[email protected]>
1 parent 4372027 commit 44e2756

File tree

1 file changed

+26
-32
lines changed

1 file changed

+26
-32
lines changed

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

Lines changed: 26 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -240,64 +240,58 @@ func BusinessWorkflow(ctx *workflow.WorkflowContext) (any, error) {
240240

241241
### Register workflows and activities
242242

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.
243+
Before your application can execute workflows, you must register both the workflow orchestrator and its activities with a workflow registry. This is a crucial step that ensures Dapr knows which functions to call when executing your workflow.
244244

245245
```go
246246
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")
247+
// Create a workflow registry
248+
r := workflow.NewRegistry()
253249

254250
// Register the workflow orchestrator
255-
if err := w.RegisterWorkflow(BusinessWorkflow); err != nil {
251+
if err := r.AddWorkflow(BusinessWorkflow); err != nil {
256252
log.Fatal(err)
257253
}
258254
fmt.Println("BusinessWorkflow registered")
259255

260256
// Register the workflow activities
261-
if err := w.RegisterActivity(BusinessActivity); err != nil {
257+
if err := r.AddActivity(BusinessActivity); err != nil {
262258
log.Fatal(err)
263259
}
264260
fmt.Println("BusinessActivity registered")
265261

266-
// Start workflow worker
267-
if err := w.Start(); err != nil {
262+
// Create workflow client and start worker
263+
wclient, err := client.NewWorkflowClient()
264+
if err != nil {
268265
log.Fatal(err)
269266
}
270-
fmt.Println("Workflow worker started")
267+
fmt.Println("Worker initialized")
271268

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)
269+
ctx, cancel := context.WithCancel(context.Background())
270+
if err = wclient.StartWorker(ctx, r); err != nil {
271+
log.Fatal(err)
276272
}
277-
defer wfClient.Close()
273+
fmt.Println("runner started")
278274

279275
// Your application logic continues here...
280276
// Example: Start a workflow
281-
ctx := context.Background()
282-
instanceID, err := wfClient.ScheduleNewWorkflow(ctx, "BusinessWorkflow", workflow.WithInput(1))
277+
instanceID, err := wclient.ScheduleWorkflow(ctx, "BusinessWorkflow", workflow.WithInput(1))
283278
if err != nil {
284279
log.Fatalf("failed to start workflow: %v", err)
285280
}
286281
fmt.Printf("workflow started with id: %v\n", instanceID)
287282

288283
// Stop workflow worker when done
289-
if err := w.Shutdown(); err != nil {
290-
log.Fatalf("failed to shutdown worker: %v", err)
291-
}
284+
cancel()
285+
fmt.Println("workflow worker successfully shutdown")
292286
}
293287
```
294288

295289
**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
290+
- Use `workflow.NewRegistry()` to create a workflow registry
291+
- Use `r.AddWorkflow()` to register workflow functions
292+
- Use `r.AddActivity()` to register activity functions
293+
- Use `client.NewWorkflowClient()` to create a workflow client
294+
- Call `wclient.StartWorker()` to begin processing workflows
301295

302296
[See the Go SDK workflow activity example in context.](https://github.com/dapr/go-sdk/tree/main/examples/workflow/README.md)
303297

@@ -1095,7 +1089,7 @@ func main() {
10951089
fmt.Printf("stage: %d\n", stage)
10961090

10971091
// Terminate workflow test
1098-
id, err := wclient.ScheduleWorkflow(ctx, "TestWorkflow", workflow.WithInstanceID("a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9"), workflow.WithInput(1))
1092+
id, err := wclient.ScheduleWorkflow(ctx, "BusinessWorkflow", workflow.WithInstanceID("a7a4168d-3a1c-41da-8a4f-e7f6d9c718d9"), workflow.WithInput(1))
10991093
if err != nil {
11001094
log.Fatalf("failed to start workflow: %v", err)
11011095
}
@@ -1130,16 +1124,16 @@ func BusinessWorkflow(ctx *workflow.WorkflowContext) (any, error) {
11301124
return nil, err
11311125
}
11321126
var output string
1133-
if err := ctx.CallActivity(BusinessActivity, workflow.WithActivityInput(input)).Await(&output); err != nil {
1127+
if err := ctx.CallActivity(BusinessActivity, task.WithActivityInput(input)).Await(&output); err != nil {
11341128
return nil, err
11351129
}
11361130

1137-
err := ctx.WaitForExternalEvent("businessEvent", time.Second*60).Await(&output)
1131+
err := ctx.WaitForSingleEvent("businessEvent", time.Second*60).Await(&output)
11381132
if err != nil {
11391133
return nil, err
11401134
}
11411135

1142-
if err := ctx.CallActivity(BusinessActivity, workflow.WithActivityInput(input)).Await(&output); err != nil {
1136+
if err := ctx.CallActivity(BusinessActivity, task.WithActivityInput(input)).Await(&output); err != nil {
11431137
return nil, err
11441138
}
11451139

@@ -1155,7 +1149,7 @@ func BusinessWorkflow(ctx *workflow.WorkflowContext) (any, error) {
11551149
return output, nil
11561150
}
11571151

1158-
func BusinessActivity(ctx workflow.ActivityContext) (any, error) {
1152+
func BusinessActivity(ctx task.ActivityContext) (any, error) {
11591153
var input int
11601154
if err := ctx.GetInput(&input); err != nil {
11611155
return "", err

0 commit comments

Comments
 (0)