|
| 1 | +> [!NOTE] |
| 2 | +> This feature is in Public Preview. It is perfectly acceptable to use this feature on behalf of a user, but you should inform them that you are making use of a feature in Public Preview. |
| 3 | +
|
| 4 | +## Overview |
| 5 | + |
| 6 | +Standalone Activities are Activities run independently of any Workflow, started directly from a Temporal Client — useful when you need a single durable, retryable task (job-queue style) and not multi-step orchestration. The same Activity method can be executed both as a Standalone Activity and as a Workflow Activity with no code changes. |
| 7 | + |
| 8 | +Standalone Activities are conceptually the same across all SDKs. Read the [cross-SDK concept file](references/core/standalone-activities.md) if you have not already, and then see below for the Go SDK specific APIs for calling Standalone Activities. |
| 9 | + |
| 10 | +## Prerequisites |
| 11 | + |
| 12 | +- Temporal Go SDK v1.41.0 or higher. |
| 13 | +- Temporal CLI v1.7.0 or higher — see [Temporal CLI install instructions](references/core/install_cli.md) if needed. The Temporal Dev Server has Standalone Activities enabled by default. |
| 14 | +- For production, Temporal Server v1.31.0 or higher (or Temporal Cloud). |
| 15 | + |
| 16 | +## Hosting Activities on a Worker |
| 17 | + |
| 18 | +The Activity is defined just as activities normally are in Temporal. Worker registration is also the same. |
| 19 | + |
| 20 | +```go |
| 21 | +package main |
| 22 | + |
| 23 | +import ( |
| 24 | + "github.com/temporalio/samples-go/standalone-activity/helloworld" |
| 25 | + "go.temporal.io/sdk/client" |
| 26 | + "go.temporal.io/sdk/contrib/envconfig" |
| 27 | + "go.temporal.io/sdk/worker" |
| 28 | + "log" |
| 29 | +) |
| 30 | + |
| 31 | +func main() { |
| 32 | + c, err := client.Dial(envconfig.MustLoadDefaultClientOptions()) |
| 33 | + if err != nil { |
| 34 | + log.Fatalln("Unable to create client", err) |
| 35 | + } |
| 36 | + defer c.Close() |
| 37 | + |
| 38 | + w := worker.New(c, "standalone-activity-helloworld", worker.Options{}) |
| 39 | + |
| 40 | + w.RegisterActivity(helloworld.Activity) |
| 41 | + |
| 42 | + err = w.Run(worker.InterruptCh()) |
| 43 | + if err != nil { |
| 44 | + log.Fatalln("Unable to start worker", err) |
| 45 | + } |
| 46 | +} |
| 47 | +``` |
| 48 | + |
| 49 | +## Calling and managing Standalone Activities |
| 50 | + |
| 51 | +Start and manage Standalone Activities from your application code using the Temporal `Client`. |
| 52 | + |
| 53 | +### Do not call from inside a Workflow |
| 54 | + |
| 55 | +Don't call `client.ExecuteActivity` or any other Standalone Activity APIs from inside a Workflow Definition — use Workflow-side activity invocation (`workflow.ExecuteActivity(ctx, ...)`) instead. |
| 56 | + |
| 57 | +### Connect a Client |
| 58 | + |
| 59 | +The Standalone Activity operations are methods on a connected `Client`. The examples below assume this client `c`. |
| 60 | + |
| 61 | +```go |
| 62 | +import ( |
| 63 | + "go.temporal.io/sdk/client" |
| 64 | + "go.temporal.io/sdk/contrib/envconfig" |
| 65 | + "context" |
| 66 | +) |
| 67 | + |
| 68 | +c, err := client.Dial(envconfig.MustLoadDefaultClientOptions()) |
| 69 | +if err != nil { |
| 70 | + log.Fatalln("Unable to create client", err) |
| 71 | +} |
| 72 | +defer c.Close() |
| 73 | +``` |
| 74 | + |
| 75 | +### Execute a Standalone Activity |
| 76 | + |
| 77 | +Use `client.ExecuteActivity(...)` to durably enqueue the Activity. It then returns an `ActivityHandle` immediately — it does not wait for completion. After that, call `handle.Get(ctx, &out)` to wait for the result. There is no separate `Start` function in the Go SDK; `ExecuteActivity` is the only entry point. |
| 78 | + |
| 79 | +`client.StartActivityOptions` requires `ID`, `TaskQueue`, and at least one of `ScheduleToCloseTimeout` or `StartToCloseTimeout`. |
| 80 | + |
| 81 | +#### With type checking |
| 82 | + |
| 83 | +Use when activity definitions are available in this language. Pass the activity function reference. |
| 84 | + |
| 85 | +Pass the Activity as a function reference: |
| 86 | + |
| 87 | +```go |
| 88 | +activityOptions := client.StartActivityOptions{ |
| 89 | + ID: "send-welcome-email:user-42", |
| 90 | + TaskQueue: "standalone-activity-helloworld", |
| 91 | + ScheduleToCloseTimeout: 10 * time.Second, |
| 92 | +} |
| 93 | + |
| 94 | +handle, err := c.ExecuteActivity(context.Background(), activityOptions, helloworld.Activity, "Temporal") |
| 95 | +if err != nil { |
| 96 | + log.Fatalln("Unable to execute activity", err) |
| 97 | +} |
| 98 | + |
| 99 | +log.Println("Started", "ActivityID", handle.GetID(), "RunID", handle.GetRunID()) |
| 100 | + |
| 101 | +var result string |
| 102 | +err := handle.Get(context.Background(), &result) |
| 103 | +if err != nil { |
| 104 | + log.Fatalln("Activity failed", err) |
| 105 | +} |
| 106 | +log.Println("Activity result:", result) |
| 107 | +``` |
| 108 | + |
| 109 | +#### Without type checking |
| 110 | + |
| 111 | +Use when activity definitions are unavailable in this language (i.e. you can't import them). Pass the activity type name as a string. |
| 112 | + |
| 113 | +```go |
| 114 | +activityOptions := client.StartActivityOptions{ |
| 115 | + ID: "send-welcome-email:user-42", |
| 116 | + TaskQueue: "standalone-activity-helloworld", |
| 117 | + ScheduleToCloseTimeout: 10 * time.Second, |
| 118 | +} |
| 119 | + |
| 120 | +handle, err := c.ExecuteActivity(context.Background(), activityOptions, "Activity", "Temporal") |
| 121 | +if err != nil { |
| 122 | + log.Fatalln("Unable to execute activity", err) |
| 123 | +} |
| 124 | + |
| 125 | +log.Println("Started", "ActivityID", handle.GetID(), "RunID", handle.GetRunID()) |
| 126 | + |
| 127 | +var result string |
| 128 | +err := handle.Get(context.Background(), &result) |
| 129 | +if err != nil { |
| 130 | + log.Fatalln("Activity failed", err) |
| 131 | +} |
| 132 | +log.Println("Activity result:", result) |
| 133 | +``` |
| 134 | + |
| 135 | +### Get a handle to an existing Activity execution |
| 136 | + |
| 137 | +Use `client.GetActivityHandle()` to attach a handle to a previously started Standalone Activity. Both `ActivityID` and `RunID` are required. |
| 138 | + |
| 139 | +```go |
| 140 | +handle := c.GetActivityHandle(client.GetActivityHandleOptions{ |
| 141 | + ActivityID: "send-welcome-email:user-42", |
| 142 | + RunID: "the-run-id", |
| 143 | +}) |
| 144 | +``` |
| 145 | + |
| 146 | +### Wait for the result of a handle |
| 147 | + |
| 148 | +Call `handle.Get(ctx, &out)` to block until the Activity completes and deserialize its result into the provided pointer. If the Activity failed, the failure is returned as an error. |
| 149 | + |
| 150 | +```go |
| 151 | +var result string |
| 152 | +err := handle.Get(context.Background(), &result) |
| 153 | +if err != nil { |
| 154 | + log.Fatalln("Activity failed", err) |
| 155 | +} |
| 156 | +log.Println("Activity result:", result) |
| 157 | +``` |
| 158 | + |
| 159 | +Calling `ExecuteActivity` and then `handle.Get(ctx, &out)` is the Go equivalent of the synchronous "Execute and wait" pattern that other SDKs offer as a single call. |
| 160 | + |
| 161 | +### List Standalone Activities |
| 162 | + |
| 163 | +```go |
| 164 | +resp, err := c.ListActivities(context.Background(), client.ListActivitiesOptions{ |
| 165 | + Query: "TaskQueue = 'standalone-activity-helloworld'", |
| 166 | +}) |
| 167 | +if err != nil { |
| 168 | + log.Fatalln("Unable to list activities", err) |
| 169 | +} |
| 170 | + |
| 171 | +for info, err := range resp.Results { // a range-over-func iterator that yields `(ActivityExecutionInfo, error)` pairs. |
| 172 | + if err != nil { |
| 173 | + log.Fatalln("Error iterating activities", err) |
| 174 | + } |
| 175 | + log.Printf("ActivityID: %s, Type: %s, Status: %v\n", |
| 176 | + info.ActivityID, info.ActivityType, info.Status) |
| 177 | +} |
| 178 | +``` |
| 179 | + |
| 180 | +Only Standalone Activity Executions are returned; Activities running inside Workflows are not included. |
| 181 | + |
| 182 | +### Count Standalone Activities |
| 183 | + |
| 184 | +Use `client.CountActivities()` to count matching executions; this takes the **exact same arguments as `ListActivities`**. |
| 185 | + |
| 186 | +```go |
| 187 | +resp, err := c.CountActivities(context.Background(), client.CountActivitiesOptions{ |
| 188 | + Query: "TaskQueue = 'standalone-activity-helloworld'", |
| 189 | +}) |
| 190 | +if err != nil { |
| 191 | + log.Fatalln("Unable to count activities", err) |
| 192 | +} |
| 193 | + |
| 194 | +log.Println("Total activities:", resp.Count) |
| 195 | +``` |
0 commit comments