# Activity Timeouts

> For the complete documentation index, see [llms.txt](https://docs.temporal.io/llms.txt).
> Any documentation page is available as raw Markdown by appending `.md` to its URL.
> For related questions, query https://docs.temporal.io/api/retrieve?q=<question>&jtb=<job-to-be-done>&src=md-page&agent=<your-name> (no auth; returns JSON `{content, source_url, page_url}`, with `source_url` pointing to Markdown for docs pages). Don't include personal data or credentials.
> If this page is wrong or missing something you needed, or Temporal behaves differently from what it says, report it with https://docs.temporal.io/api/feedback?title=<summary>&message=<what-happened>&category=documentation&page_url=/develop/ruby/activities/timeouts&src=md-page&agent=<your-name> (use `category=product` when the product, not the page, is wrong). Report only a specific problem you hit, send it once, and don't include personal data or credentials.

> Set the Schedule-To-Close, Start-To-Close, and Heartbeat Timeouts on a Ruby Activity, tune its Retry Policy, and override the next retry delay.

## Activity timeouts 

Each Activity Timeout controls a different aspect of how long an Activity Execution can take:

- **[Schedule-To-Close Timeout](/encyclopedia/detecting-activity-failures#schedule-to-close-timeout)**
- **[Start-To-Close Timeout](/encyclopedia/detecting-activity-failures#start-to-close-timeout)**
- **[Schedule-To-Start Timeout](/encyclopedia/detecting-activity-failures#schedule-to-start-timeout)**

At least one of `start_to_close_timeout` or `schedule_to_close_timeout` is required.
The Temporal Service fills in whichever timeouts you leave unset and caps the rest against each other, so the values it stores can differ from the ones you passed. See [How the Temporal Service resolves Activity timeouts](/encyclopedia/detecting-activity-failures#timeout-resolution).

<!--SNIPSTART ruby-activity-timeouts-start-to-close {"enable_code_dedenting": true}-->
[ruby-sandbox/activities/timeouts/workflows.rb](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/ruby-sandbox/activities/timeouts/workflows.rb)
```rb
Temporalio::Workflow.execute_activity(
  MyActivity,
  { greeting: 'Hello', name: },
  start_to_close_timeout: 5 * 60
)
```
<!--SNIPEND-->

### 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.

<!--SNIPSTART ruby-activity-timeouts-retry-policy {"enable_code_dedenting": true}-->
[ruby-sandbox/activities/timeouts/workflows.rb](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/ruby-sandbox/activities/timeouts/workflows.rb)
```rb
Temporalio::Workflow.execute_activity(
  MyActivity,
  { greeting: 'Hello', name: },
  start_to_close_timeout: 5 * 60,
  retry_policy: Temporalio::RetryPolicy.new(max_interval: 10)
)
```
<!--SNIPEND-->

### 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.

<!--SNIPSTART ruby-activity-timeouts-next-retry-delay {"enable_code_dedenting": true}-->
[ruby-sandbox/activities/timeouts/next_retry_delay_activity.rb](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/ruby-sandbox/activities/timeouts/next_retry_delay_activity.rb)
```rb
raise Temporalio::Error::ApplicationError.new(
  'Some error',
  type: 'SomeErrorType',
  next_retry_delay: 3 * Temporalio::Activity::Context.current.info.attempt
)
```
<!--SNIPEND-->

## 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.

<!--SNIPSTART ruby-activity-timeouts-heartbeat {"enable_code_dedenting": true}-->
[ruby-sandbox/activities/timeouts/heartbeat_activity.rb](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/ruby-sandbox/activities/timeouts/heartbeat_activity.rb)
```rb
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
```
<!--SNIPEND-->

### Heartbeat Timeout 

The Heartbeat Timeout sets the maximum duration between Heartbeats before the Temporal Service considers the Activity failed.

<!--SNIPSTART ruby-activity-timeouts-heartbeat-timeout {"enable_code_dedenting": true}-->
[ruby-sandbox/activities/timeouts/workflows.rb](https://github.com/temporalio/documentation-sdk-code-examples/blob/main/ruby-sandbox/activities/timeouts/workflows.rb)
```rb
Temporalio::Workflow.execute_activity(
  MyActivity,
  { greeting: 'Hello', name: },
  start_to_close_timeout: 5 * 60,
  heartbeat_timeout: 5
)
```
<!--SNIPEND-->
