Activity Heartbeats - .NET SDK feature guide
This page shows how to do the following:
Heartbeat an Activity
How to Heartbeat an Activity using the Temporal .NET SDK
An Activity Heartbeat is a ping from the Worker Process that is executing the Activity to the Temporal Cluster. Each Heartbeat informs the Temporal Cluster that the Activity Execution is making progress and the Worker has not crashed. If the Cluster does not receive a Heartbeat within a Heartbeat Timeout time period, the Activity will be considered failed and another Activity Task Execution may be scheduled according to the Retry Policy.
Heartbeats may not always be sent to the Cluster—they may be throttled by the Worker.
Activity Cancellations are delivered to Activities from the Cluster when they Heartbeat. Activities that don't Heartbeat can't receive a Cancellation. Heartbeat throttling may lead to Cancellation getting delivered later than expected.
Heartbeats can contain a Details
field describing the Activity's current progress.
If an Activity gets retried, the Activity can access the Details
from the last Heartbeat that was sent to the Cluster.
To Heartbeat an Activity Execution in .NET, use the Heartbeat()
method on the ActivityExecutionContext
.
[Activity]
public async Task MyActivityAsync()
{
while (true)
{
// Send heartbeat
ActivityExecutionContext.Current.Heartbeat();
// Do some work, passing the cancellation token
await Task.Delay(1000, ActivityExecutionContext.Current.CancellationToken);
}
}
In addition to obtaining cancellation information, Heartbeats also support detail data that persists on the server for retrieval during Activity retry.
If an Activity calls Heartbeat(123, 456)
and then fails and is retried, HeartbeatDetails
on the ActivityInfo
returns an collection containing 123
and 456
on the next Run.
Set a Heartbeat Timeout
How to set a Heartbeat Timeout using the Temporal .NET SDK
A Heartbeat Timeout works in conjunction with Activity Heartbeats.
HeartbeatTimeout
is a property on ActivityOptions
for ExecuteActivityAsync
used to set the maximum time between Activity Heartbeats.
await Workflow.ExecuteActivityAsync(
(MyActivities a) => a.MyActivity(param),
new()
{
StartToCloseTimeout = TimeSpan.FromMinutes(5),
HeartbeatTimeout = TimeSpan.FromSeconds(30),
});