Skip to main content

How to develop a Worker Program in Go

Create an instance of Worker by calling worker.New(), available via the go.temporal.io/sdk/worker package, and pass it the following parameters:

  1. An instance of the Temporal Go SDK Client.
  2. The name of the Task Queue that it will poll.
  3. An instance of worker.Options, which can be empty.

Then, register the Workflow Types and the Activity Types that the Worker will be capable of executing.

Lastly, call either the Start() or the Run() method on the instance of the Worker. Run accepts an interrupt channel as a parameter, so that the Worker can be stopped in the terminal. Otherwise the Stop() method must be called to stop the Worker.

package main
import (    "go.temporal.io/sdk/client"    "go.temporal.io/sdk/worker")
func main() {    c, err := client.NewClient(client.Options{})    if err != nil {        // ...    }    defer c.Close()    w := worker.New(c, "your-task-queue", worker.Options{})    w.RegisterWorkflow(YourWorkflowDefinition)    w.RegisterActivity(YourActivityDefinition)    err = w.Run(worker.InterruptCh())    if err != nil        // ...    }  // ...}
func YourWorkflowDefinition(ctx workflow.Context, param YourWorkflowParam) (YourWorkflowResponse, error) {  // ...}
func YourActivityDefinition(ctx context.Context, param YourActivityParam) (YourActivityResponse, error) {  // ...}

Start the Worker Process by running go run <filename>.go.

tip

If you have gow installed, the Worker Process automatically "reloads" when you update the file:

go install github.com/mitranim/gow@latestgow run worker/main.go # automatically reload when file changed

The RegisterWorkflow() and RegisterActivity calls essentially create an in-memory mapping between the Workflow Types and their implementations, inside the Worker process.

Notice that the Task Queue name is the same as the name provided when the Workflow Execution is spawned.

The name of the Task Queue that is provided to the Worker must be the same Task Queue name that is provided with the invocation of the Workflow Execution.

note

All Workers listening to the same Task Queue name must be registered to handle the exact same Workflows Types and Activity Types.

If a Worker polls a Task for a Workflow Type or Activity Type it does not know about, it will fail that Task. However, the failure of the Task will not cause the associated Workflow Execution to fail.

Register multiple Types#

To register multiple Activity Types and/or Workflow Types with the Worker Entity, just make multiple Activity registration calls, but make sure each Activity Type name is unique:

w.registerActivity(ActivityA)w.registerActivity(ActivityB)w.registerActivity(ActivityC)w.registerWorkflow(WorkflowA)w.registerWorkflow(WorkflowB)w.registerWorkflow(WorkflowC)

An Activity Type name can be customized to something other than the function name using the RegisterActivityWithOptions call.

A Workflow Type name can be customized to something other than the function name using the RegisterWorkflowWithOptions call.

Get notified of updates