Skip to main content

Quickstart

View Markdown

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, and only the execution path differs.

Note

This documentation uses source code from the go-sandbox/activities/standalone sample.

Get started with Standalone Activities​

Prerequisites:

  • Go 1.26+, the version the sample's go.mod requires

  • Temporal Go SDK (v1.49.0 or higher)

  • Temporal CLI v1.9.1 or higher. Install with Homebrew, or see the Temporal CLI install guide for other platforms. Verify the installation with temporal --version.

Start the Temporal development server with temporal server start-dev.

This command automatically starts the Temporal development server with the Web UI, and creates the default Namespace. It uses an in-memory database, so do not use it for real use cases.

The Temporal Server should now be available for client connections on localhost:7233, and the Temporal Web UI should now be accessible at http://localhost:8233.

brew install temporal
temporal --version
temporal server start-dev

Clone the sample​

Clone the documentation-sdk-code-examples repository and switch to the go-sandbox directory to follow along. Run every command on this page from that directory.

git clone https://github.com/temporalio/documentation-sdk-code-examples.git
cd documentation-sdk-code-examples/go-sandbox

The sample is structured as follows:

activities/standalone/
├── activity.go
├── worker/
│ └── main.go
└── starter/
└── main.go

Define your Activity​

Define your Activity in a shared package so that both the Worker and the starter can reference it. The package also holds the Task Queue name and Activity ID that the Worker and the starter use.

package standalone

import (
"context"
"fmt"
)

const (
TaskQueue = "standalone-activity-helloworld"
ActivityID = "standalone_activity_helloworld_ActivityID"
)

func Activity(ctx context.Context, name string) (string, error) {
return fmt.Sprintf("Hello %s!", name), nil
}

Run a Worker with the Activity registered​

Running a Worker for Standalone Activities is the same as running a Worker for Workflow-driven Activities — you create a Worker, register the Activity, and call Run(). The Worker doesn't need to know whether the Activity will be invoked from a Workflow or as a Standalone Activity.

See Develop a Worker in Go for more details on Worker setup and configuration options.

Open a new terminal, switch to the go-sandbox directory, and run the Worker. Leave this terminal running - the Worker needs to stay up to process Activities.

package main

import (
"log"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/contrib/envconfig"
"go.temporal.io/sdk/worker"

"my-org/quickstart/activities/standalone"
)

func main() {
c, err := client.Dial(envconfig.MustLoadDefaultClientOptions())
if err != nil {
log.Fatalln("Unable to create client", err)
}
defer c.Close()

// Registering a Standalone Activity is the same as registering any other Activity.
w := worker.New(c, standalone.TaskQueue, worker.Options{})
w.RegisterActivity(standalone.Activity)

if err := w.Run(worker.InterruptCh()); err != nil {
log.Fatalln("Unable to start Worker", err)
}
}
go run ./activities/standalone/worker

Execute a Standalone Activity​

Use client.ExecuteActivity() 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 that you can use to get the result, describe, cancel, or terminate the Activity. The starter calls handle.Get() to wait for the Activity to finish and read its result.

client.StartActivityOptions requires ID, TaskQueue, and at least one of ScheduleToCloseTimeout or StartToCloseTimeout. See StartActivityOptions in the API reference for the full set of options.

To run the starter:

  1. Make sure the Temporal Server is running (from the Get Started step above).
  2. Make sure the Worker is running (from the Run a Worker step above).
  3. Open a new terminal, switch to the go-sandbox directory, and run:
    go run ./activities/standalone/starter

The last line of output shows the result:

Activity result: Hello Temporal!

Or use the Temporal CLI to execute a Standalone Activity.

options := client.StartActivityOptions{
ID: standalone.ActivityID,
TaskQueue: standalone.TaskQueue,
StartToCloseTimeout: 10 * time.Second,
}

handle, err := c.ExecuteActivity(context.Background(), options, standalone.Activity, "Temporal")
if err != nil {
log.Fatalln("Unable to execute activity", err)
}
log.Println("Started Standalone Activity", "ActivityID", handle.GetID(), "RunID", handle.GetRunID())
var result string
err = handle.Get(context.Background(), &result)
if err != nil {
log.Fatalln("Activity failed", err)
}
log.Println("Activity result:", result)
go run ./activities/standalone/starter
temporal activity execute \
--type Activity \
--activity-id standalone_activity_helloworld_ActivityID \
--task-queue standalone-activity-helloworld \
--start-to-close-timeout 10s \
--input '"Temporal"'

Execute a Standalone Activity by type name​

You can pass the Activity to ExecuteActivity as a function reference, as in the previous step, or as a string Activity type name. Use the type name when the code that starts the Activity can't import the Activity function, for example when the Activity is written in another language.

The type name of a Go Activity registered with RegisterActivity is the function name, Activity in this sample.

handle, err := c.ExecuteActivity(context.Background(), options, "Activity", "Temporal")
go run ./activities/standalone/starter -command start-by-name

List and count Standalone Activities​

Use client.ListActivities() and client.CountActivities() to find and count the Standalone Activity Executions that match a List Filter query. These APIs return only Standalone Activity Executions. Activities running inside Workflows are not included.

Run the starter with -command list to list the Activities on the sample's Task Queue, and with -command count to count them. See List Standalone Activities in the feature guide for more about the query syntax.

resp, err := c.ListActivities(context.Background(), client.ListActivitiesOptions{
Query: "TaskQueue = 'standalone-activity-helloworld'",
})
if err != nil {
log.Fatalln("Unable to list activities", err)
}

for info, err := range resp.Results {
if err != nil {
log.Fatalln("Error iterating activities", err)
}
log.Printf("ActivityID: %s, Type: %s, Status: %v\n",
info.ActivityID, info.ActivityType, info.Status)
}
resp, err := c.CountActivities(context.Background(), client.CountActivitiesOptions{
Query: "TaskQueue = 'standalone-activity-helloworld'",
})
if err != nil {
log.Fatalln("Unable to count activities", err)
}

log.Println("Total activities:", resp.Count)
go run ./activities/standalone/starter -command list
go run ./activities/standalone/starter -command count

Run with Temporal Cloud​

All code samples on this page use envconfig.MustLoadDefaultClientOptions() to configure the Temporal Client connection. It responds to environment variables and TOML configuration files, so the same code works against a local dev server and Temporal Cloud without changes. See Run Standalone Activities with Temporal Cloud in the feature guide for mTLS and API key setup.

Next steps​