Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[Workflow] Updates for Go SDK #3895

Merged
merged 28 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 9 commits
Commits
Show all changes
28 commits
Select commit Hold shift + click to select a range
bec3842
prep for go workflow
hhunter-ms Dec 8, 2023
a7e270e
add early code examples and cross links
hhunter-ms Jan 2, 2024
6d3d614
Merge branch 'v1.13' into issue_3868
hhunter-ms Jan 3, 2024
c2b7342
Merge branch 'v1.13' into issue_3868
hhunter-ms Jan 26, 2024
1934dca
update go snippets
hhunter-ms Feb 1, 2024
9218028
Merge branch 'issue_3868' of https://github.com/hhunter-ms/docs into …
hhunter-ms Feb 1, 2024
6f67dc9
Merge branch 'v1.13' into issue_3868
hhunter-ms Feb 1, 2024
e49c436
add quickstart
hhunter-ms Feb 1, 2024
4fefea7
Merge branch 'issue_3868' of https://github.com/hhunter-ms/docs into …
hhunter-ms Feb 1, 2024
a45e25f
add supported SDKs
hhunter-ms Feb 2, 2024
0f50934
mike initial review
hhunter-ms Feb 5, 2024
664cff1
Merge branch 'v1.13' into issue_3868
hhunter-ms Feb 5, 2024
bd75fa6
Merge branch 'v1.13' into issue_3868
hhunter-ms Feb 9, 2024
d2ec174
Merge branch 'v1.13' into issue_3868
hhunter-ms Feb 9, 2024
c5ee281
update quickstart, start updating patterns
hhunter-ms Feb 14, 2024
c3a2b69
merge conflict
hhunter-ms Feb 14, 2024
04f6a22
Merge branch 'v1.13' into issue_3868
hhunter-ms Feb 14, 2024
6d9a344
add more pattern examples
hhunter-ms Feb 14, 2024
233c4f3
Merge branch 'issue_3868' of https://github.com/hhunter-ms/docs into …
hhunter-ms Feb 14, 2024
1e00080
Merge branch 'v1.13' into issue_3868
hhunter-ms Feb 15, 2024
8204495
add fan in/fan out
hhunter-ms Feb 16, 2024
e7a16cb
Merge branch 'issue_3868' of https://github.com/hhunter-ms/docs into …
hhunter-ms Feb 16, 2024
d9cbcba
Update daprdocs/content/en/developing-applications/building-blocks/wo…
hhunter-ms Feb 20, 2024
66b8bdd
Merge branch 'v1.13' into issue_3868
hhunter-ms Feb 20, 2024
e36ecc1
Update daprdocs/content/en/developing-applications/building-blocks/wo…
hhunter-ms Feb 20, 2024
b9a8038
updates per mark and marc
hhunter-ms Feb 20, 2024
43dd7f7
Merge branch 'issue_3868' of https://github.com/hhunter-ms/docs into …
hhunter-ms Feb 20, 2024
3c6c961
Update daprdocs/content/en/developing-applications/building-blocks/wo…
hhunter-ms Feb 20, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ The Dapr sidecar doesn’t load any workflow definitions. Rather, the sidecar si

[Workflow activities]({{< ref "workflow-features-concepts.md#workflow-activites" >}}) are the basic unit of work in a workflow and are the tasks that get orchestrated in the business process.

{{< tabs Python ".NET" Java >}}
{{< tabs Python ".NET" Java Go >}}

{{% codetab %}}

Expand Down Expand Up @@ -165,14 +165,63 @@ public class DemoWorkflowActivity implements WorkflowActivity {

{{% /codetab %}}

{{% codetab %}}

<!--go-->

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 {
ctx task.ActivityContext
}

func (wfac *ActivityContext) GetInput(v interface{}) error {
return wfac.ctx.GetInput(&v)
}

func (wfac *ActivityContext) Context() context.Context {
return wfac.ctx.Context()
}

type callActivityOption func(*callActivityOptions) error

type callActivityOptions struct {
rawInput *wrapperspb.StringValue
}

// 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
}
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
}
}
```
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

[See the Go SDK workflow activity example in context.](todo)
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

{{% /codetab %}}

{{< /tabs >}}

## Write the workflow

Next, register and call the activites in a workflow.

{{< tabs Python ".NET" Java >}}
{{< tabs Python ".NET" Java Go >}}

{{% codetab %}}

Expand Down Expand Up @@ -267,6 +316,69 @@ public class DemoWorkflowWorker {
[See the Java SDK workflow in context.](https://github.com/dapr/java-sdk/blob/master/examples/src/main/java/io/dapr/examples/workflows/DemoWorkflowWorker.java)


{{% /codetab %}}

{{% codetab %}}

<!--go-->

Next, register the workflow and workflow activities and start the workflow runtime.

```go
package workflow

// RegisterWorkflow adds a workflow function to the registry
func (ww *WorkflowWorker) RegisterWorkflow(w Workflow) error {
wrappedOrchestration := wrapWorkflow(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 = ww.tasks.AddOrchestratorN(name, wrappedOrchestration)
return err
}

// Activity wrapper
func wrapActivity(a Activity) task.Activity {
return func(ctx task.ActivityContext) (any, error) {
aCtx := ActivityContext{ctx: ctx}

return a(aCtx)
}
}

// RegisterActivity adds an activity function to the registry
func (ww *WorkflowWorker) RegisterActivity(a Activity) error {
wrappedActivity := wrapActivity(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 = ww.tasks.AddActivityN(name, wrappedActivity)
return err
}

// 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
}
```
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

[See the Go SDK workflow in context.](todo)
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

{{% /codetab %}}

{{< /tabs >}}
Expand All @@ -275,7 +387,7 @@ public class DemoWorkflowWorker {

Finally, compose the application using the workflow.

{{< tabs Python ".NET" Java >}}
{{< tabs Python ".NET" Java Go >}}

{{% codetab %}}

Expand Down Expand Up @@ -484,6 +596,25 @@ public class DemoWorkflow extends Workflow {

{{% /codetab %}}

{{% codetab %}}

<!--go-->

[As in the following example](todo), a hello-world application using the Go SDK and Dapr Workflow would include:

- A Go package called `todo` to receive the Go SDK client capabilities.
- An import of `todo`
- The `DemoWorkflow` class which extends `Workflow`
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved
- Creating the workflow with input and output.
- API calls. In the example below, these calls start and call the workflow activities.

```go

```

[See the full Go SDK workflow example in context.](todo)

{{% /codetab %}}

{{< /tabs >}}

Expand All @@ -506,3 +637,4 @@ Now that you've authored a workflow, learn how to manage it.
- [Python example](https://github.com/dapr/python-sdk/tree/master/examples/demo_workflow)
- [.NET example](https://github.com/dapr/dotnet-sdk/tree/master/examples/Workflow)
- [Java example](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/workflows)
- [Go example](todo)
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ Dapr Workflow is currently in beta. [See known limitations for {{% dapr-latest-v

Now that you've [authored the workflow and its activities in your application]({{< ref howto-author-workflow.md >}}), you can start, terminate, and get information about the workflow using HTTP API calls. For more information, read the [workflow API reference]({{< ref workflow_api.md >}}).

{{< tabs Python ".NET" Java HTTP >}}
{{< tabs Python ".NET" Java Go HTTP >}}

<!--Python-->
{{% codetab %}}
Expand Down Expand Up @@ -99,7 +99,7 @@ await daprClient.PurgeWorkflowAsync(orderId, workflowComponent);

{{% /codetab %}}

<!--Python-->
<!--Java-->
{{% codetab %}}

Manage your workflow within your code. [In the workflow example from the Java SDK](https://github.com/dapr/java-sdk/blob/master/examples/src/main/java/io/dapr/examples/workflows/DemoWorkflowClient.java), the workflow is registered in the code using the following APIs:
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved
Expand Down Expand Up @@ -164,6 +164,84 @@ public class DemoWorkflowClient {

{{% /codetab %}}

<!--Go-->
{{% codetab %}}

Manage your workflow within your code. [In the workflow example from the Go SDK](todo), the workflow is registered in the code using the following APIs:
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

- **StartWorkflow**: Starts a new workflow instance
- **GetWorkflow**: Get information on the status of the workflow
- **PauseWorkflow**: Pauses or suspends a workflow instance that can later be resumed
- **RaiseEventWorkflow**: Raises events/tasks for the running workflow instance
- **ResumeWorkflow**: Waits for the workflow to complete its tasks
- **PurgeWorkflow**: Removes all metadata related to a specific workflow instance
- **TerminateWorkflow**: Terminates the workflow

```go
// 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
}

type StartWorkflowResponse struct {
InstanceID string
}

// Get the workflow status
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
}

// Purge workflow
type PurgeWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}

// Terminate workflow
type TerminateWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}

// Pause workflow
type PauseWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}

// Resume workflow
type ResumeWorkflowRequest struct {
InstanceID string
WorkflowComponent string
}

// 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 %}}

<!--HTTP-->
{{% codetab %}}
Expand Down Expand Up @@ -244,5 +322,6 @@ Learn more about these HTTP calls in the [workflow API reference guide]({{< ref
- [Python example](https://github.com/dapr/python-sdk/blob/master/examples/demo_workflow/app.py)
- [.NET example](https://github.com/dapr/dotnet-sdk/tree/master/examples/Workflow)
- [Java example](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/workflows)
- [Go example](todo)
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved

- [Workflow API reference]({{< ref workflow_api.md >}})
Original file line number Diff line number Diff line change
Expand Up @@ -196,4 +196,5 @@ See the [Reminder usage and execution guarantees section]({{< ref "workflow-arch
- Try out the following examples:
- [Python](https://github.com/dapr/python-sdk/tree/master/examples/demo_workflow)
- [.NET](https://github.com/dapr/dotnet-sdk/tree/master/examples/Workflow)
- [Java](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/workflows)
- [Java](https://github.com/dapr/java-sdk/tree/master/examples/src/main/java/io/dapr/examples/workflows)
hhunter-ms marked this conversation as resolved.
Show resolved Hide resolved
- [Go example](todo)
Loading
Loading