-
Notifications
You must be signed in to change notification settings - Fork 295
Standalone activities in Go #4203
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
Open
yuandrew
wants to merge
4
commits into
temporalio:main
Choose a base branch
from
yuandrew:standalone-activities
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 hidden or 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 hidden or 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,153 @@ | ||
| --- | ||
| id: standalone-activities | ||
| title: Standalone Activities - Go SDK | ||
| sidebar_label: Standalone Activities | ||
| toc_max_heading_level: 4 | ||
| keywords: | ||
| - standalone activity | ||
| - activity execution | ||
| - execute activity | ||
| - activity handle | ||
| - list activities | ||
| - count activities | ||
| - go sdk | ||
| tags: | ||
| - Activities | ||
| - Temporal Client | ||
| - Go SDK | ||
| - Temporal SDKs | ||
| description: Execute Activities independently without a Workflow using the Temporal Go SDK. | ||
| --- | ||
|
|
||
| Standalone Activities are Activity Executions that run independently, without being orchestrated by a Workflow. | ||
| Instead of starting an Activity from within a Workflow Definition using `workflow.ExecuteActivity()`, you start a Standalone Activity directly from a Temporal Client using `client.ExecuteActivity()`. | ||
|
|
||
| The Activity definition and Worker registration are identical to regular Activities — only the execution path differs. | ||
|
|
||
| This page covers the following: | ||
|
|
||
| - [Execute a Standalone Activity](#execute-activity) | ||
| - [Get the result of a Standalone Activity](#get-activity-result) | ||
| - [Get a handle to an existing Standalone Activity](#get-activity-handle) | ||
|
|
||
| :::caution EXPERIMENTAL | ||
|
|
||
| Standalone Activities are [Experimental](/evaluate/development-production-features/release-stages). | ||
| The API may change in future releases. | ||
|
|
||
| ::: | ||
|
|
||
| :::info PREREQUISITES | ||
|
|
||
| Standalone Activities require server-side support. | ||
| If you are running a self-hosted Temporal Server, the following dynamic config values must be enabled: | ||
|
|
||
| - `activity.enableStandalone=true` | ||
| - `history.enableChasm=true` | ||
| - `history.enableTransitionHistory=true` | ||
|
|
||
| The `ListActivities` and `CountActivities` APIs use Go's `iter.Seq2` type, which requires **Go 1.23 or later**. | ||
|
|
||
| ::: | ||
|
|
||
|
|
||
| ## Execute a Standalone Activity {#execute-activity} | ||
|
|
||
| Use [`client.ExecuteActivity()`](https://pkg.go.dev/go.temporal.io/sdk/client#Client) to start a Standalone Activity Execution. | ||
| This is called from application code (for example, a starter program), not from inside a Workflow Definition. | ||
|
|
||
| `ExecuteActivity` returns an [`ActivityHandle`](https://pkg.go.dev/go.temporal.io/sdk/client#ActivityHandle) that you can use to get the result, describe, cancel, or terminate the Activity. | ||
|
|
||
| <div className="copycode-notice-container"> | ||
| <a href="https://github.com/temporalio/samples-go/blob/main/standalone-activity/helloworld/starter/main.go"> | ||
| View the source code | ||
| </a>{' '} | ||
| in the context of the rest of the application code. | ||
| </div> | ||
|
|
||
| ```go | ||
| import ( | ||
| "context" | ||
| "log" | ||
| "time" | ||
|
|
||
| "go.temporal.io/sdk/client" | ||
| ) | ||
|
|
||
| func main() { | ||
| c, err := client.Dial(client.Options{}) | ||
| if err != nil { | ||
| log.Fatalln("Unable to create client", err) | ||
| } | ||
| defer c.Close() | ||
|
|
||
| activityOptions := client.StartActivityOptions{ | ||
| ID: "my-standalone-activity-id", | ||
| TaskQueue: "my-task-queue", | ||
| ScheduleToCloseTimeout: 10 * time.Second, | ||
| } | ||
|
|
||
| handle, err := c.ExecuteActivity( | ||
| context.Background(), activityOptions, MyActivity, "Temporal", | ||
| ) | ||
| if err != nil { | ||
| log.Fatalln("Unable to execute standalone activity", err) | ||
| } | ||
|
|
||
| log.Println("Started standalone activity", | ||
| "ActivityID", handle.GetID(), | ||
| "RunID", handle.GetRunID(), | ||
| ) | ||
| } | ||
| ``` | ||
|
|
||
| You can pass the Activity as either a function reference or a string Activity type name: | ||
|
|
||
| ```go | ||
| // Using a function reference (recommended — enables compile-time parameter validation) | ||
| handle, err := c.ExecuteActivity(ctx, options, MyActivity, "arg1") | ||
|
|
||
| // Using a string type name | ||
| handle, err := c.ExecuteActivity(ctx, options, "MyActivity", "arg1") | ||
| ``` | ||
|
|
||
| `client.StartActivityOptions` requires `ID`, `TaskQueue`, and at least one of `ScheduleToCloseTimeout` or `StartToCloseTimeout`. | ||
| See [`StartActivityOptions`](https://pkg.go.dev/go.temporal.io/sdk/client#StartActivityOptions) in the API reference for the full set of options. | ||
|
|
||
| ## Get the result of a Standalone Activity {#get-activity-result} | ||
|
|
||
| Use `ActivityHandle.Get()` to block until the Activity completes and retrieve its result. | ||
| This is analogous to calling `Get()` on a `WorkflowRun`. | ||
|
|
||
| ```go | ||
| var result string | ||
| err = handle.Get(context.Background(), &result) | ||
| if err != nil { | ||
| log.Fatalln("Activity failed", err) | ||
| } | ||
| log.Println("Activity result:", result) | ||
| ``` | ||
|
|
||
| If the Activity completed successfully, the result is deserialized into the provided pointer. | ||
| If the Activity failed, the failure is returned as an error. | ||
|
|
||
| ## Get a handle to an existing Standalone Activity {#get-activity-handle} | ||
|
|
||
| Use `client.GetActivityHandle()` to create a handle to a previously started Standalone Activity. | ||
| This is analogous to `client.GetWorkflow()` for Workflow Executions. | ||
|
|
||
| Both `ActivityID` and `RunID` are required. | ||
|
|
||
| ```go | ||
| handle := c.GetActivityHandle(client.GetActivityHandleOptions{ | ||
| ActivityID: "my-standalone-activity-id", | ||
| RunID: "the-run-id", | ||
| }) | ||
|
|
||
| // Use the handle to get the result, describe, cancel, or terminate | ||
| var result string | ||
| err := handle.Get(context.Background(), &result) | ||
| if err != nil { | ||
| log.Fatalln("Unable to get activity result", err) | ||
| } | ||
| ``` | ||
This file contains hidden or 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
Oops, something went wrong.
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.
Uh oh!
There was an error while loading. Please reload this page.