update go snippets

Signed-off-by: Hannah Hunter <hannahhunter@microsoft.com>
This commit is contained in:
Hannah Hunter 2024-02-01 13:22:26 -05:00
parent a7e270e02f
commit 1934dcac52
3 changed files with 87 additions and 59 deletions

View File

@ -169,7 +169,7 @@ public class DemoWorkflowActivity implements WorkflowActivity {
<!--go-->
Define the workflow activities you'd like your workflow to perform. Activities are wrapped in the public `callActivityOptions` class, which implements the workflow activities.
Define the workflow activities you'd like your workflow to perform. Activities are wrapped in the public `callActivityOptions` method, which implements the workflow activities.
```go
type ActivityContext struct {
@ -190,13 +190,22 @@ type callActivityOptions struct {
rawInput *wrapperspb.StringValue
}
func WithActivityInput(input any) callActivityOption {
return func(opt *callActivityOptions) error {
// ActivityInput is an option to pass a JSON-serializable input
func ActivityInput(input any) callActivityOption {
return func(opts *callActivityOptions) error {
data, err := marshalData(input)
if err != nil {
return err
}
opt.rawInput = wrapperspb.String(string(data))
opts.rawInput = wrapperspb.String(string(data))
return nil
}
}
// ActivityRawInput is an option to pass a byte slice as an input
func ActivityRawInput(input string) callActivityOption {
return func(opts *callActivityOptions) error {
opts.rawInput = wrapperspb.String(input)
return nil
}
}
@ -313,22 +322,22 @@ public class DemoWorkflowWorker {
<!--go-->
Next, register the workflow with the `WorkflowRuntimeBuilder` and start the workflow runtime.
Next, register the workflow and workflow activities and start the workflow runtime.
```go
package workflow
// Register workflow
func (wr *WorkflowRuntime) RegisterWorkflow(w Workflow) error {
// RegisterWorkflow adds a workflow function to the registry
func (ww *WorkflowWorker) RegisterWorkflow(w Workflow) error {
wrappedOrchestration := wrapWorkflow(w)
// get decorator for workflow
name, err := getDecorator(w)
// get the function name for the passed workflow
name, err := getFunctionName(w)
if err != nil {
return fmt.Errorf("failed to get workflow decorator: %v", err)
}
err = wr.tasks.AddOrchestratorN(name, wrappedOrchestration)
err = ww.tasks.AddOrchestratorN(name, wrappedOrchestration)
return err
}
@ -341,31 +350,29 @@ func wrapActivity(a Activity) task.Activity {
}
}
// Register wrapped activity
func (wr *WorkflowRuntime) RegisterActivity(a Activity) error {
// RegisterActivity adds an activity function to the registry
func (ww *WorkflowWorker) RegisterActivity(a Activity) error {
wrappedActivity := wrapActivity(a)
// get decorator for activity
name, err := getDecorator(a)
// get the function name for the passed activity
name, err := getFunctionName(a)
if err != nil {
return fmt.Errorf("failed to get activity decorator: %v", err)
}
err = wr.tasks.AddActivityN(name, wrappedActivity)
err = ww.tasks.AddActivityN(name, wrappedActivity)
return err
}
// Start workflow runtime
func (wr *WorkflowRuntime) Start() error {
// go func start
go func() {
err := wr.client.StartWorkItemListener(context.Background(), wr.tasks)
if err != nil {
log.Fatalf("failed to start work stream: %v", err)
}
}()
<-wr.quit
// Start initialises a non-blocking worker to handle workflows and activities registered
// prior to this being called.
func (ww *WorkflowWorker) Start() error {
ctx, cancel := context.WithCancel(context.Background())
ww.cancel = cancel
if err := ww.client.StartWorkItemListener(ctx, ww.tasks); err != nil {
return fmt.Errorf("failed to start work stream: %v", err)
}
log.Println("work item listener started")
return nil
}
```

View File

@ -178,47 +178,67 @@ Manage your workflow within your code. [In the workflow example from the Go SDK]
- **TerminateWorkflow**: Terminates the workflow
```go
// StartWorkflowAlpha1 starts a workflow.
StartWorkflowAlpha1(ctx context.Context, req *StartWorkflowRequest) (*StartWorkflowResponse, error)
// Start workflow
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
}
// GetWorkflowAlpha1 gets a workflow.
GetWorkflowAlpha1(ctx context.Context, req *GetWorkflowRequest) (*GetWorkflowResponse, error)
type StartWorkflowResponse struct {
InstanceID string
}
// PurgeWorkflowAlpha1 purges a workflow.
PurgeWorkflowAlpha1(ctx context.Context, req *PurgeWorkflowRequest) error
// Get the workflow status
type GetWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
// TerminateWorkflowAlpha1 terminates a workflow.
TerminateWorkflowAlpha1(ctx context.Context, req *TerminateWorkflowRequest) error
type GetWorkflowResponse struct {
InstanceID string
WorkflowName string
CreatedAt time.Time
LastUpdatedAt time.Time
RuntimeStatus string
Properties map[string]string
}
// PauseWorkflowAlpha1 pauses a workflow.
PauseWorkflowAlpha1(ctx context.Context, req *PauseWorkflowRequest) error
// Purge workflow
type PurgeWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
// ResumeWorkflowAlpha1 resumes a workflow.
ResumeWorkflowAlpha1(ctx context.Context, req *ResumeWorkflowRequest) error
// Terminate workflow
type TerminateWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
// RaiseEventWorkflowAlpha1 raises an event for a workflow.
RaiseEventWorkflowAlpha1(ctx context.Context, req *RaiseEventWorkflowRequest) error
// Pause workflow
type PauseWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
// StartWorkflowBeta1 starts a workflow.
StartWorkflowBeta1(ctx context.Context, req *StartWorkflowRequest) (*StartWorkflowResponse, error)
// Resume workflow
type ResumeWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}
// GetWorkflowBeta1 gets a workflow.
GetWorkflowBeta1(ctx context.Context, req *GetWorkflowRequest) (*GetWorkflowResponse, error)
// PurgeWorkflowBeta1 purges a workflow.
PurgeWorkflowBeta1(ctx context.Context, req *PurgeWorkflowRequest) error
// TerminateWorkflowBeta1 terminates a workflow.
TerminateWorkflowBeta1(ctx context.Context, req *TerminateWorkflowRequest) error
// PauseWorkflowBeta1 pauses a workflow.
PauseWorkflowBeta1(ctx context.Context, req *PauseWorkflowRequest) error
// ResumeWorkflowBeta1 resumes a workflow.
ResumeWorkflowBeta1(ctx context.Context, req *ResumeWorkflowRequest) error
// RaiseEventWorkflowBeta1 raises an event for a workflow.
RaiseEventWorkflowBeta1(ctx context.Context, req *RaiseEventWorkflowRequest) error
// Raise an event for the running workflow
type RaiseEventWorkflowRequest struct {
InstanceID string
WorkflowComponent string
EventName string
EventData any
SendRawData bool // Set to True in order to disable serialization on the data
}
```
{{% /codetab %}}

View File

@ -85,6 +85,7 @@ Want to put workflows to the test? Walk through the following quickstart and tut
| [Workflow Python SDK example](https://github.com/dapr/python-sdk/tree/master/examples/demo_workflow) | Learn how to create a Dapr Workflow and invoke it using the Python `DaprClient` package. |
| [Workflow .NET SDK example](https://github.com/dapr/dotnet-sdk/tree/master/examples/Workflow) | Learn how to create a Dapr Workflow and invoke it using ASP.NET Core web APIs. |
| [Workflow Java SDK example](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/workflows) | Learn how to create a Dapr Workflow and invoke it using the Java `io.dapr.workflows` package. |
| [Workflow Go SDK example](todo) | Learn how to create a Dapr Workflow and invoke it using the Go `workflow` package. |
### Start using workflows directly in your app