diff --git a/docs/develop/go/core-application.mdx b/docs/develop/go/core-application.mdx index 8457b558cf..3cb2e730f7 100644 --- a/docs/develop/go/core-application.mdx +++ b/docs/develop/go/core-application.mdx @@ -56,6 +56,8 @@ In this section you can find the following: - [Start an Activity Execution](#activity-execution) - [Run a dev Worker](#develop-worker) - [Run a Temporal Cloud Worker](#run-a-temporal-cloud-worker) +- [Set a Dynamic Workflow](#set-a-dynamic-workflow) +- [Set a Dynamic Activity](#set-a-dynamic-activity) ## How to install the Temporal CLI and run a development server {#run-a-development-server} @@ -1593,3 +1595,67 @@ registerOptions := activity.RegisterOptions{ w.RegisterActivityWithOptions(a.YourActivityDefinition, registerOptions) // ... ``` + +## Set a Dynamic Workflow {#set-a-dynamic-workflow} + +**How to set a Dynamic Workflow using the Temporal Go SDK** + +A Dynamic Workflow in Temporal is a Workflow that is invoked dynamically at runtime if no other Workflow with the same name is registered. +A Workflow can be registered as dynamic by using `worker.RegisterDynamicWorkflow()`. +You must register the Workflow with the Worker before it can be invoked. +Only one Dynamic Workflow can be present on a Worker. + +The Workflow Definition must then accept a single argument of type `converter.EncodedValues`. +This code snippet is taken from the [Dynamic Workflow example from samples-go](https://github.com/temporalio/samples-go/tree/main/dynamic-workflows). + +```go +func DynamicWorkflow(ctx workflow.Context, args converter.EncodedValues) (string, error) { + var result string + info := workflow.GetInfo(ctx) + + var arg1, arg2 string + err := args.Get(&arg1, &arg2) + if err != nil { + return "", fmt.Errorf("failed to decode arguments: %w", err) + } + + if info.WorkflowType.Name == "dynamic-activity" { + ctx = workflow.WithActivityOptions(ctx, workflow.ActivityOptions{StartToCloseTimeout: 10 * time.Second}) + err := workflow.ExecuteActivity(ctx, "random-activity-name", arg1, arg2).Get(ctx, &result) + if err != nil { + return "", err + } + } else { + result = fmt.Sprintf("%s - %s - %s", info.WorkflowType.Name, arg1, arg2) + } + + return result, nil +} +``` + +## Set a Dynamic Activity {#set-a-dynamic-activity} + +**How to set a Dynamic Activity using the Temporal Go SDK** + +A Dynamic Activity in Temporal is an Activity that is invoked dynamically at runtime if no other Activity with the same name is registered. +A Workflow can be registered as dynamic by using `worker.RegisterDynamicActivity()`. +You must register the Activity with the Worker before it can be invoked. +Only one Dynamic Activity can be present on a Worker. + +The Workflow Definition must then accept a single argument of type `converter.EncodedValues`. +This code snippet is taken from the [Dynamic Workflow example from samples-go](https://github.com/temporalio/samples-go/tree/main/dynamic-workflows). + +```go +func DynamicActivity(ctx context.Context, args converter.EncodedValues) (string, error) { + var arg1, arg2 string + err := args.Get(&arg1, &arg2) + if err != nil { + return "", fmt.Errorf("failed to decode arguments: %w", err) + } + + info := activity.GetInfo(ctx) + result := fmt.Sprintf("%s - %s - %s", info.WorkflowType.Name, arg1, arg2) + + return result, nil +} +```