Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
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
24 changes: 24 additions & 0 deletions go-sandbox/workflows/childworkflows/async.go
Original file line number Diff line number Diff line change
@@ -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
10 changes: 10 additions & 0 deletions go-sandbox/workflows/childworkflows/child.go
Original file line number Diff line number Diff line change
@@ -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
43 changes: 43 additions & 0 deletions go-sandbox/workflows/childworkflows/childworkflows_test.go
Original file line number Diff line number Diff line change
@@ -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)
}
18 changes: 18 additions & 0 deletions go-sandbox/workflows/childworkflows/parent.go
Original file line number Diff line number Diff line change
@@ -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
24 changes: 24 additions & 0 deletions go-sandbox/workflows/childworkflows/parentclose.go
Original file line number Diff line number Diff line change
@@ -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
18 changes: 18 additions & 0 deletions go-sandbox/workflows/childworkflows/types.go
Original file line number Diff line number Diff line change
@@ -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
}
Loading