-
Notifications
You must be signed in to change notification settings - Fork 204
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
Async activity Invocation Example in Go #286
Open
edmondop
wants to merge
4
commits into
temporalio:main
Choose a base branch
from
edmondop:async-activity
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,19 @@ | ||
# Async Activities Invocation in GoLang | ||
|
||
This sample shows how to submit two activities in non-blocking fashion and wait until both are completed to return, | ||
or fail if one of the two fails. The example uses two activity invocation, registers that on a selector, and invoke | ||
`selector.Select(ctx)` twice, since each selector call will proceed as soon as one result is available. | ||
|
||
|
||
|
||
### Running this sample | ||
|
||
```bash | ||
go run activities-async/worker/main.go | ||
``` | ||
|
||
Start the Workflow Execution: | ||
|
||
```bash | ||
go run activities-async/starter/main.go | ||
``` |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
package activities_async | ||
|
||
import ( | ||
"context" | ||
) | ||
|
||
func SayHello(ctx context.Context, param string) (string, error) { | ||
return "Hello " + param + "!", nil | ||
} | ||
|
||
func SayGoodbye(ctx context.Context, param string) (string, error) { | ||
return "Goodbye " + param + "!", nil | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
package main | ||
|
||
import ( | ||
"context" | ||
activities_async "github.com/temporalio/samples-go/activities-async" | ||
"log" | ||
|
||
"go.temporal.io/sdk/client" | ||
) | ||
|
||
func main() { | ||
// The client is a heavyweight object that should be created once per process. | ||
c, err := client.Dial(client.Options{}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
|
||
workflowOptions := client.StartWorkflowOptions{ | ||
TaskQueue: "async-activities-task-queue", | ||
} | ||
|
||
we, err := c.ExecuteWorkflow(context.Background(), workflowOptions, activities_async.AsyncActivitiesWorkflow) | ||
if err != nil { | ||
log.Fatalln("Unable to execute workflow", err) | ||
} | ||
|
||
log.Println("Started workflow", "WorkflowID", we.GetID(), "RunID", we.GetRunID()) | ||
|
||
// Synchronously wait for the workflow completion. | ||
var result string | ||
err = we.Get(context.Background(), &result) | ||
if err != nil { | ||
log.Fatalln("Unable get workflow result", err) | ||
} | ||
log.Println("Workflow completed with result ", result) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,27 @@ | ||
package main | ||
|
||
import ( | ||
activities_async "github.com/temporalio/samples-go/activities-async" | ||
"log" | ||
|
||
"go.temporal.io/sdk/client" | ||
"go.temporal.io/sdk/worker" | ||
) | ||
|
||
func main() { | ||
// The client and worker are heavyweight objects that should be created once per process. | ||
c, err := client.Dial(client.Options{}) | ||
if err != nil { | ||
log.Fatalln("Unable to create client", err) | ||
} | ||
defer c.Close() | ||
w := worker.New(c, "async-activities-task-queue", worker.Options{}) | ||
w.RegisterWorkflow(activities_async.AsyncActivitiesWorkflow) | ||
|
||
w.RegisterActivity(activities_async.SayHello) | ||
w.RegisterActivity(activities_async.SayGoodbye) | ||
err = w.Run(worker.InterruptCh()) | ||
if err != nil { | ||
log.Fatalln("Unable to start worker", err) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,33 @@ | ||
package activities_async | ||
|
||
import ( | ||
"go.temporal.io/sdk/workflow" | ||
"time" | ||
) | ||
|
||
func AsyncActivitiesWorkflow(ctx workflow.Context) (res string, err error) { | ||
selector := workflow.NewSelector(ctx) | ||
var res1 string | ||
var res2 string | ||
ao := workflow.ActivityOptions{ | ||
StartToCloseTimeout: 10 * time.Second, | ||
} | ||
ctx = workflow.WithActivityOptions(ctx, ao) | ||
fut1 := workflow.ExecuteActivity(ctx, SayHello, "Temporal") | ||
fut2 := workflow.ExecuteActivity(ctx, SayGoodbye, "Temporal") | ||
selector.AddFuture(fut1, func(future workflow.Future) { | ||
err = future.Get(ctx, &res1) | ||
}) | ||
selector.AddFuture(fut2, func(future workflow.Future) { | ||
err = future.Get(ctx, &res2) | ||
}) | ||
selector.Select(ctx) | ||
if err != nil { | ||
return | ||
} | ||
selector.Select(ctx) | ||
if err == nil { | ||
res = res1 + " It was great to meet you, but time has come. " + res2 | ||
} | ||
return | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package activities_async | ||
|
||
import ( | ||
"context" | ||
"errors" | ||
"github.com/stretchr/testify/mock" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.temporal.io/sdk/testsuite" | ||
) | ||
|
||
func Test_Workflow_Succeeds(t *testing.T) { | ||
testSuite := &testsuite.WorkflowTestSuite{} | ||
env := testSuite.NewTestWorkflowEnvironment() | ||
|
||
env.RegisterActivity(SayHello) | ||
env.RegisterActivity(SayGoodbye) | ||
env.ExecuteWorkflow(AsyncActivitiesWorkflow) | ||
|
||
require.True(t, env.IsWorkflowCompleted()) | ||
require.NoError(t, env.GetWorkflowError()) | ||
var res string | ||
err := env.GetWorkflowResult(&res) | ||
require.NoError(t, err) | ||
require.Equal(t, "Hello Temporal! It was great to meet you, but time has come. Goodbye Temporal!", res) | ||
} | ||
|
||
func testWorkflowFailsWhenActivityFail(t *testing.T, a interface{}) { | ||
testSuite := &testsuite.WorkflowTestSuite{} | ||
env := testSuite.NewTestWorkflowEnvironment() | ||
env.RegisterActivity(SayHello) | ||
env.RegisterActivity(SayGoodbye) | ||
env.OnActivity(a, mock.Anything, mock.Anything).Return(func(ctx context.Context, arg string) (string, error) { | ||
return "", errors.New("activity failed") | ||
}) | ||
env.ExecuteWorkflow(AsyncActivitiesWorkflow) | ||
require.True(t, env.IsWorkflowCompleted()) | ||
require.Error(t, env.GetWorkflowError()) | ||
} | ||
|
||
func Test_Workflow_FailsIfSayHelloFails(t *testing.T) { | ||
testWorkflowFailsWhenActivityFail(t, SayHello) | ||
} | ||
func Test_Workflow_FailsIfSayGoodbyeFails(t *testing.T) { | ||
testWorkflowFailsWhenActivityFail(t, SayGoodbye) | ||
} |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
If an
err
was reported by the first select, you have swallowed itThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed with 7156e31