Skip to content

Commit 2b0d052

Browse files
committed
added heartbeat docs
1 parent 48a96ef commit 2b0d052

File tree

2 files changed

+39
-0
lines changed

2 files changed

+39
-0
lines changed

docs/docs.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,7 @@
7070
"machines",
7171
"idempotency",
7272
"runs/max-duration",
73+
"runs/heartbeats",
7374
"tags",
7475
"runs/metadata",
7576
"tasks/streams",

docs/runs/heartbeats.mdx

Lines changed: 38 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
---
2+
title: "Heartbeats"
3+
sidebarTitle: "Heartbeats"
4+
description: "Keep long-running or CPU-heavy tasks from being marked as stalled."
5+
---
6+
7+
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.
8+
9+
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.
10+
11+
```ts
12+
import { task, heartbeats } from "@trigger.dev/sdk";
13+
14+
export const processLargeDataset = task({
15+
id: "process-large-dataset",
16+
run: async (payload: { items: string[] }) => {
17+
for (const row of payload.items) {
18+
process(row);
19+
await heartbeats.yield();
20+
}
21+
return { processed: payload.items.length };
22+
},
23+
});
24+
25+
function process(row: string) {
26+
// synchronous CPU-heavy work
27+
}
28+
```
29+
30+
If you see `TASK_RUN_STALLED_EXECUTING`, see [Task run stalled executing](/troubleshooting#task-run-stalled-executing) in the troubleshooting guide.
31+
32+
## Sending progress to Trigger.dev
33+
34+
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.
35+
36+
## Sending updates to your own system
37+
38+
Trigger.dev doesn’t 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.

0 commit comments

Comments
 (0)