Failure detection - Ruby SDK
This page shows how to do the following:
- Workflow timeouts
- Workflow retries
- Activity timeouts
- Activity Retry Policy
- Heartbeat an Activity
- Heartbeat Timeout
Workflow timeouts
Each Workflow timeout controls the maximum duration of a different aspect of a Workflow Execution.
- Workflow Execution Timeout: Limits how long the full Workflow Execution can run.
- Workflow Run Timeout: Limits the duration of an individual run of a Workflow Execution.
- Workflow Task Timeout: Limits the time allowed for a Worker to process a Workflow Task.
Set these values as keyword parameter options when starting a Workflow.
result = my_client.execute_workflow(
MyWorkflow, 'some-input',
id: 'my-workflow-id', task_queue: 'my-task-queue',
execution_timeout: 5 * 60
)
Workflow retries
A Retry Policy can work in cooperation with the timeouts to provide fine controls to optimize the execution experience.
Use a Retry Policy to automatically retry Workflow Executions on failure. Workflow Executions do not retry by default.
Workflow Executions do not retry by default, and Retry Policies should be used with Workflow Executions only in certain situations.
The retry_policy
can be set when calling start_workflow
or execute_workflow
.
result = my_client.execute_workflow(
MyWorkflow, 'some-input',
id: 'my-workflow-id', task_queue: 'my-task-queue',
retry_policy: Temporalio::RetryPolicy.new(max_interval: 10)
)
Activity timeouts
Each Activity Timeout controls a different aspect of how long an Activity Execution can take:
At least one of start_to_close_timeout
or schedule_to_close_timeout
is required.
Temporalio::Workflow.execute_activity(
MyActivity,
{ greeting: 'Hello', name: },
start_to_close_timeout: 5 * 60
)
Activity Retry Policy
By default, Activities use a system Retry Policy. You can override it by specifying a custom Retry Policy.
To create an Activity Retry Policy in Ruby, set the retry_policy
parameter when executing an activity.
Temporalio::Workflow.execute_activity(
MyActivity,
{ greeting: 'Hello', name: },
start_to_close_timeout: 5 * 60,
retry_policy: Temporalio::RetryPolicy.new(max_interval: 10)
)
Override the retry interval with next_retry_delay
If you raise an application-level error, you can override the Retry Policy's delay by specifying a new delay.
raise Temporalio::ApplicationError.new(
'Some error',
type: 'SomeErrorType',
next_retry_delay: 3 * Temporalio::Activity::Context.current.info.attempt
)
Heartbeat an Activity
A Heartbeat is a periodic signal from the Worker to the Temporal Service indicating the Activity is still alive and making progress.
- Heartbeats are used to detect Worker failure.
- Cancellations are delivered via Heartbeats.
- Heartbeats may contain custom progress details.
class MyActivity < Temporalio::Activity::Definition
def execute
# This is a naive loop simulating work, but similar heartbeat logic
# applies to other scenarios as well
loop do
# Send heartbeat
Temporalio::Activity::Context.current.heartbeat
# Sleep before heartbeating again
sleep(3)
end
end
end
Heartbeat Timeout
The Heartbeat Timeout sets the maximum duration between Heartbeats before the Temporal Service considers the Activity failed.
Temporalio::Workflow.execute_activity(
MyActivity,
{ greeting: 'Hello', name: },
start_to_close_timeout: 5 * 60,
heartbeat_timeout: 5
)