Skip to main content
Temporal Ruby SDK

About this example

Free previewTemporal 101Ruby
  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 Service.

Next, the Temporal Service 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 Service, uses a Client to do this.

Screenshot showing actors in Workflow execution scenario

Workers and tasks

The assignment of work is indirect. The Temporal Service does not assign tasks to a Worker (in fact, the Temporal Service does not maintain a list of Workers).

Instead, the Workers continually poll the Temporal Service'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.

Screenshot showing Workers and tasks

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 a Workflow Execution, such as a call to the Workflow's execute_activity method, it sends a Command to the Temporal Service. The Service 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 Service uses this information to recreate the state of the Workflow to what it was immediately before the crash and then resumes progress from that point. This allows you, as a developer, to code as if this type of failure wasn't even a possibility.

Screenshot showing Commands

Workflow and Activity Definitions

The application defines two Activities: GreetInSpanish and FarewellInSpanish.

activities.rb
# frozen_string_literal: true

require 'temporalio/activity'
require 'net/http'

class GreetInSpanish < Temporalio::Activity::Definition
def execute(name)
call_service('get-spanish-greeting', name)
end
end

class FarewellInSpanish < Temporalio::Activity::Definition
def execute(name)
call_service('get-spanish-farewell', name)
end
end

# Utility method for making calls to the microservices
def call_service(stem, name)
base = "http://localhost:9999/#{stem}"
url = "#{base}?name=#{name}"
response = Net::HTTP.get_response(URI(url))
response.body
end

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

workflow.rb
# frozen_string_literal: true

require 'temporalio/workflow'
require_relative 'activities'

class GreetSomeone < Temporalio::Workflow::Definition
def execute(name)
greeting = Temporalio::Workflow.execute_activity(
GreetInSpanish,
name,
start_to_close_timeout: 5
)

farewell = Temporalio::Workflow.execute_activity(
FarewellInSpanish,
name,
start_to_close_timeout: 5
)

"#{greeting}\n#{farewell}"
end
end

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

worker.rb
# frozen_string_literal: true

require_relative 'workflow'
require_relative 'activities'
require 'temporalio/client'
require 'temporalio/worker'

# Create a Temporal client
client = Temporalio::Client.connect(
'localhost:7233',
'default',
)

# Create worker with the activities and workflow
worker = Temporalio::Worker.new(
client:,
task_queue: 'greeting-tasks',
workflows: [GreetSomeone],
activities: [GreetInSpanish, FarewellInSpanish]
)

# Run the worker until SIGINT
puts 'Starting worker (ctrl+c to exit)'
worker.run(shutdown_signals: ['SIGINT'])

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

In the next video, you will see how all the parts work together via a code walkthrough.

Get notified when we launch new educational content

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

Subscribe
Feedback