Files
triggerdotdev--trigger.dev/docs/runs/heartbeats.mdx
T
2026-07-13 13:32:57 +08:00

39 lines
2.1 KiB
Plaintext
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
---
title: "Heartbeats"
sidebarTitle: "Heartbeats"
description: "Keep long-running or CPU-heavy tasks from being marked as stalled."
---
We send a heartbeat from your task to the platform every 30 seconds. If we don't receive a heartbeat within 5 minutes, we mark the run as stalled and stop it with a `TASK_RUN_STALLED_EXECUTING` error.
Code that blocks the event loop for too long (for example, a tight loop doing synchronous work on a large dataset) can prevent heartbeats from being sent. In that case, use `heartbeats.yield()` inside the loop so the runtime can yield to the event loop and send a heartbeat. You can call it every iteration; the implementation only yields when needed.
```ts
import { task, heartbeats } from "@trigger.dev/sdk";
export const processLargeDataset = task({
id: "process-large-dataset",
run: async (payload: { items: string[] }) => {
for (const row of payload.items) {
await heartbeats.yield();
processRow(row);
}
return { processed: payload.items.length };
},
});
function processRow(row: string) {
// synchronous CPU-heavy work
}
```
If you see `TASK_RUN_STALLED_EXECUTING`, see [Task run stalled executing](/troubleshooting#task-run-stalled-executing) in the troubleshooting guide.
## Sending progress to Trigger.dev
To stream progress or status updates to the dashboard and your app, use [run metadata](/runs/metadata). Call `metadata.set()` (or `metadata.append()`) as the task runs. The dashboard and [Realtime](/realtime) (including `runs.subscribeToRun` and the React hooks) receive those updates as they happen. See [Progress monitoring](/realtime/backend/subscribe#progress-monitoring) for a full example.
## Sending updates to your own system
Trigger.dev doesnt push run updates to external services. To send progress or heartbeats to your own backend (for example Supabase Realtime), call your API or client from inside the task when you want to emit an update—e.g. in the same loop where you call `heartbeats.yield()` or `metadata.set()`. Use whatever your stack supports: HTTP, the Supabase client, or another SDK.