diff --git a/go-sandbox/workflows/childworkflows/async.go b/go-sandbox/workflows/childworkflows/async.go new file mode 100644 index 0000000..1b8f331 --- /dev/null +++ b/go-sandbox/workflows/childworkflows/async.go @@ -0,0 +1,24 @@ +package childworkflows + +// @@@SNIPSTART go-child-workflow-async +import ( + "go.temporal.io/api/enums/v1" + "go.temporal.io/sdk/workflow" +) + +func YourAsyncWorkflowDefinition(ctx workflow.Context, params ParentParams) (ParentResp, error) { + childWorkflowOptions := workflow.ChildWorkflowOptions{ + ParentClosePolicy: enums.PARENT_CLOSE_POLICY_ABANDON, + } + ctx = workflow.WithChildOptions(ctx, childWorkflowOptions) + + childWorkflowFuture := workflow.ExecuteChildWorkflow(ctx, YourOtherWorkflowDefinition, ChildParams{Name: params.Name}) + // Wait for the Child Workflow Execution to spawn + var childWE workflow.Execution + if err := childWorkflowFuture.GetChildWorkflowExecution().Get(ctx, &childWE); err != nil { + return ParentResp{}, err + } + // ... + return ParentResp{ChildWorkflowID: childWE.ID}, nil +} +// @@@SNIPEND diff --git a/go-sandbox/workflows/childworkflows/child.go b/go-sandbox/workflows/childworkflows/child.go new file mode 100644 index 0000000..b78c16c --- /dev/null +++ b/go-sandbox/workflows/childworkflows/child.go @@ -0,0 +1,10 @@ +package childworkflows + +import "go.temporal.io/sdk/workflow" + +// @@@SNIPSTART go-child-workflow-definition +func YourOtherWorkflowDefinition(ctx workflow.Context, params ChildParams) (ChildResp, error) { + // ... + return ChildResp{Greeting: "Hello " + params.Name}, nil +} +// @@@SNIPEND diff --git a/go-sandbox/workflows/childworkflows/childworkflows_test.go b/go-sandbox/workflows/childworkflows/childworkflows_test.go new file mode 100644 index 0000000..7915adf --- /dev/null +++ b/go-sandbox/workflows/childworkflows/childworkflows_test.go @@ -0,0 +1,43 @@ +package childworkflows_test + +import ( + "testing" + + "github.com/stretchr/testify/suite" + "go.temporal.io/sdk/testsuite" + + "my-org/quickstart/workflows/childworkflows" +) + +type ChildWorkflowsTestSuite struct { + suite.Suite + testsuite.WorkflowTestSuite +} + +func TestChildWorkflowsTestSuite(t *testing.T) { + suite.Run(t, new(ChildWorkflowsTestSuite)) +} + +func (s *ChildWorkflowsTestSuite) run(wf interface{}) childworkflows.ParentResp { + env := s.NewTestWorkflowEnvironment() + env.RegisterWorkflow(childworkflows.YourOtherWorkflowDefinition) + env.ExecuteWorkflow(wf, childworkflows.ParentParams{Name: "Temporal"}) + s.True(env.IsWorkflowCompleted()) + s.NoError(env.GetWorkflowError()) + + var resp childworkflows.ParentResp + s.NoError(env.GetWorkflowResult(&resp)) + return resp +} + +func (s *ChildWorkflowsTestSuite) Test_ExecuteChildWorkflow() { + s.Equal("Hello Temporal", s.run(childworkflows.YourWorkflowDefinition).Greeting) +} + +func (s *ChildWorkflowsTestSuite) Test_AsyncChildWorkflow() { + s.NotEmpty(s.run(childworkflows.YourAsyncWorkflowDefinition).ChildWorkflowID) +} + +func (s *ChildWorkflowsTestSuite) Test_ParentClosePolicy() { + s.Equal("Hello Temporal", s.run(childworkflows.YourParentClosePolicyWorkflowDefinition).Greeting) +} diff --git a/go-sandbox/workflows/childworkflows/parent.go b/go-sandbox/workflows/childworkflows/parent.go new file mode 100644 index 0000000..dc042f9 --- /dev/null +++ b/go-sandbox/workflows/childworkflows/parent.go @@ -0,0 +1,18 @@ +package childworkflows + +import "go.temporal.io/sdk/workflow" + +// @@@SNIPSTART go-child-workflow-execute +func YourWorkflowDefinition(ctx workflow.Context, params ParentParams) (ParentResp, error) { + childWorkflowOptions := workflow.ChildWorkflowOptions{} + ctx = workflow.WithChildOptions(ctx, childWorkflowOptions) + + var result ChildResp + err := workflow.ExecuteChildWorkflow(ctx, YourOtherWorkflowDefinition, ChildParams{Name: params.Name}).Get(ctx, &result) + if err != nil { + return ParentResp{}, err + } + // ... + return ParentResp{Greeting: result.Greeting}, nil +} +// @@@SNIPEND diff --git a/go-sandbox/workflows/childworkflows/parentclose.go b/go-sandbox/workflows/childworkflows/parentclose.go new file mode 100644 index 0000000..82236a2 --- /dev/null +++ b/go-sandbox/workflows/childworkflows/parentclose.go @@ -0,0 +1,24 @@ +package childworkflows + +// @@@SNIPSTART go-child-workflow-parent-close-policy +import ( + "go.temporal.io/api/enums/v1" + "go.temporal.io/sdk/workflow" +) + +func YourParentClosePolicyWorkflowDefinition(ctx workflow.Context, params ParentParams) (ParentResp, error) { + // ... + childWorkflowOptions := workflow.ChildWorkflowOptions{ + // ... + ParentClosePolicy: enums.PARENT_CLOSE_POLICY_ABANDON, + } + ctx = workflow.WithChildOptions(ctx, childWorkflowOptions) + childWorkflowFuture := workflow.ExecuteChildWorkflow(ctx, YourOtherWorkflowDefinition, ChildParams{Name: params.Name}) + // ... + var result ChildResp + if err := childWorkflowFuture.Get(ctx, &result); err != nil { + return ParentResp{}, err + } + return ParentResp{Greeting: result.Greeting}, nil +} +// @@@SNIPEND diff --git a/go-sandbox/workflows/childworkflows/types.go b/go-sandbox/workflows/childworkflows/types.go new file mode 100644 index 0000000..9e69408 --- /dev/null +++ b/go-sandbox/workflows/childworkflows/types.go @@ -0,0 +1,18 @@ +package childworkflows + +type ParentParams struct { + Name string +} + +type ParentResp struct { + Greeting string + ChildWorkflowID string +} + +type ChildParams struct { + Name string +} + +type ChildResp struct { + Greeting string +}