Skip to main content
Temporal Go SDK

About this example

Free previewTemporal 101Go
  1. About this example
  2. Code walkthrough

During the previous exercise, you executed a Workflow that included two Activities, both of which made a call to a microservice that provided a customized message in Spanish. That exercise demonstrates many of the key concepts you've learned during this course. Although you now have first-hand experience with developing and running applications on the Temporal Platform, you'll gain a deeper understanding of how Temporal works by looking at what happens during Workflow Execution.

Actors in the scenario

Let's begin by identifying the actors in this scenario, which will help to reiterate some important concepts.

First, the example includes a Worker, which executes the Workflow and Activity code, and uses a Client to communicate with the Cluster.

Next, the Temporal Cluster orchestrates the execution of that code by coordinating with the Worker, using a shared task queue.

Finally, the program that starts the Workflow, which will be referred to as a Client application because it requests Workflow Execution as well as the result from the Temporal Cluster, uses a Client to do this.

Diagram showing actors in Workflow execution scenario

Workers and tasks

The assignment of work is indirect. The Temporal Cluster does not assign tasks to a Worker (in fact, the Temporal Cluster does not maintain a list of Workers). Instead, the Workers continually poll the Temporal Cluster's Task Queue and accept tasks when they have spare capacity to process them. There are several benefits to this approach, but one of them is that tasks will just sit in the queue if there aren't enough Workers, which means that you can increase throughput and scalability by adding more Workers.

Diagram showing Workers polling tasks from the Task Queue

As you learned earlier, Temporal applications in production will typically have multiple Workers; however, this example uses a single Worker for the sake of simplicity.

Commands

Another thing that will help you understand Temporal is the role of Commands. When the Worker encounters certain API calls during Workflow Execution, such as a call to the Workflow's ExecuteActivity function, it sends a Command to the Temporal Cluster. The Cluster acts on these Commands, for example, by creating an Activity Task, but also stores them in case of failure.

For example, if the Worker crashes, the Temporal Cluster sends the stored information to another Worker to recreate the state of the Workflow to what it was immediately before the crash, and the new Worker resumes progress from that point. This allows you, as a developer, to code as if this type of failure wasn't even a possibility.

Diagram showing Worker sending Commands to the Temporal Cluster

Activity Definitions

The application defines two Activities: GreetInSpanish and FarewellInSpanish, plus a utility function that both Activities use to call the translation service.

activity.go
func GreetInSpanish(ctx context.Context, name string) (string, error) {
greeting, err := callService("get-spanish-greeting", name)
return greeting, err
}

func FarewellInSpanish(ctx context.Context, name string) (string, error) {
greeting, err := callService("get-spanish-farewell", name)
return greeting, err
}

//utility function for making calls to the microservices
func callService(stem string, name string) (string, error) {
base := "http://localhost:9999/" + stem + "?name=%s"
url := fmt.Sprintf(base, url.QueryEscape(name))

resp, err := http.Get(url)
if err != nil {
return "", err
}
defer resp.Body.Close()

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
return "", err
}

translation := string(body)

status := resp.StatusCode
if status >= 400 {
message := fmt.Sprintf("HTTP Error %d: %s", status, translation)
return "", errors.New(message)
}

return translation, nil
}

Workflow Definition

The Workflow Definition executes those two Activities and returns a string created from their output.

workflow.go
package farewell

import (
"time"

"go.temporal.io/sdk/workflow"
)

func GreetSomeone(ctx workflow.Context, name string) (string, error) {
options := workflow.ActivityOptions{
StartToCloseTimeout: time.Second * 5,
}
ctx = workflow.WithActivityOptions(ctx, options)

var spanishGreeting string
err := workflow.ExecuteActivity(ctx, GreetInSpanish, name).Get(ctx, &spanishGreeting)
if err != nil {
return "", err
}

var spanishFarewell string
err = workflow.ExecuteActivity(ctx, FarewellInSpanish, name).Get(ctx, &spanishFarewell)
if err != nil {
return "", err
}

var helloGoodbye = "\n" + spanishGreeting + "\n" + spanishFarewell

return helloGoodbye, nil
}

Worker initialization

And here's the Worker initialization code, which registers the Workflow and Activity Definitions.

main.go
package main

import (
"log"
farewell "temporal101/exercises/farewell-workflow/solution"

"go.temporal.io/sdk/client"
"go.temporal.io/sdk/worker"
)

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

w := worker.New(c, "greeting-tasks", worker.Options{})

w.RegisterWorkflow(farewell.GreetSomeone)
w.RegisterActivity(farewell.GreetInSpanish)
w.RegisterActivity(farewell.FarewellInSpanish)

err = w.Run(worker.InterruptCh())
if err != nil {
log.Fatalln("Unable to start worker", err)
}
}

Summary

In this course, you saw how the parts of a Temporal Application - a Worker, the Temporal Cluster and the Client Application - work together during a Workflow Execution.

In the next video, you will see a code walkthrough that shows how all of these parts work together.

Get notified when we launch new educational content

New courses, tutorials, and learning resources - straight to your inbox.

Subscribe
Feedback