Skip to content

Commit c92edd6

Browse files
authored
New metadata system doc updates (#1590)
1 parent ab69b60 commit c92edd6

File tree

2 files changed

+140
-4
lines changed

2 files changed

+140
-4
lines changed

docs/runs/metadata.mdx

Lines changed: 139 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -4,11 +4,11 @@ sidebarTitle: "Metadata"
44
description: "Attach a small amount of data to a run and update it as the run progresses."
55
---
66

7-
You can attach up to 4KB (4,096 bytes) of metadata to a run, which you can then access from inside the run function, via the API, and in the dashboard. You can use metadata to store additional, structured information on a run. For example, you could store your user’s full name and corresponding unique identifier from your system on every task that is associated with that user.
7+
You can attach up to 256KB of metadata to a run, which you can then access from inside the run function, via the API, Realtime, and in the dashboard. You can use metadata to store additional, structured information on a run. For example, you could store your user’s full name and corresponding unique identifier from your system on every task that is associated with that user. Or you could store the progress of a long-running task, or intermediate results that you want to access later.
88

99
## Usage
1010

11-
Add metadata to a run by passing it as an object to the `trigger` function:
11+
Add metadata to a run when triggering by passing it as an object to the `trigger` function:
1212

1313
```ts
1414
const handle = await myTask.trigger(
@@ -49,6 +49,7 @@ export const myTask = task({
4949
});
5050

5151
async function doSomeWork() {
52+
// Set the value of a specific key
5253
metadata.set("progress", 0.5);
5354
}
5455
```
@@ -378,6 +379,141 @@ export const myTask = task({
378379
});
379380
```
380381

382+
## Fluent API
383+
384+
All of the update methods can be chained together in a fluent API:
385+
386+
```ts
387+
import { task, metadata } from "@trigger.dev/sdk/v3";
388+
389+
export const myTask = task({
390+
id: "my-task",
391+
run: async (payload: { message: string }) => {
392+
metadata
393+
.set("progress", 0.1)
394+
.append("logs", "Step 1 complete")
395+
.increment("progress", 0.4)
396+
.decrement("otherProgress", 0.1);
397+
},
398+
});
399+
```
400+
401+
## Parent & root updates
402+
403+
Tasks that have been triggered by a parent task (a.k.a. a "child task") can update the metadata of the parent task. This is useful for propagating progress information up the task hierarchy. You can also update the metadata of the root task (root = the initial task that was triggered externally, like from your backend).
404+
405+
To update the parent task's metadata, use the `metadata.parent` accessor:
406+
407+
```ts
408+
import { task, metadata } from "@trigger.dev/sdk/v3";
409+
410+
export const myParentTask = task({
411+
id: "my-parent-task",
412+
run: async (payload: { message: string }) => {
413+
// Do some work
414+
metadata.set("progress", 0.1);
415+
416+
// Trigger a child task
417+
await childTask.triggerAndWait({ message: "hello world" });
418+
},
419+
});
420+
421+
export const childTask = task({
422+
id: "child-task",
423+
run: async (payload: { message: string }) => {
424+
// This will update the parent task's metadata
425+
metadata.parent.set("progress", 0.5);
426+
},
427+
});
428+
```
429+
430+
All of the update methods are available on `metadata.parent` and `metadata.root`:
431+
432+
```ts
433+
metadata.parent.set("progress", 0.5);
434+
metadata.parent.append("logs", "Step 1 complete");
435+
metadata.parent.remove("logs", "Step 1 complete");
436+
metadata.parent.increment("progress", 0.4);
437+
metadata.parent.decrement("otherProgress", 0.1);
438+
metadata.parent.stream("llm", readableStream);
439+
440+
metadata.root.set("progress", 0.5);
441+
metadata.root.append("logs", "Step 1 complete");
442+
metadata.root.remove("logs", "Step 1 complete");
443+
metadata.root.increment("progress", 0.4);
444+
metadata.root.decrement("otherProgress", 0.1);
445+
metadata.root.stream("llm", readableStream);
446+
```
447+
448+
You can also chain the update methods together:
449+
450+
```ts
451+
metadata.parent
452+
.set("progress", 0.1)
453+
.append("logs", "Step 1 complete")
454+
.increment("progress", 0.4)
455+
.decrement("otherProgress", 0.1);
456+
```
457+
458+
### Example
459+
460+
An example of where you might use parent and root updates is in a task that triggers multiple child tasks in parallel. You could use the parent metadata to track the progress of the child tasks and update the parent task's progress as each child task completes:
461+
462+
```ts
463+
import { CSVRow, UploadedFileData, parseCSVFromUrl } from "@/utils";
464+
import { batch, logger, metadata, schemaTask } from "@trigger.dev/sdk/v3";
465+
466+
export const handleCSVRow = schemaTask({
467+
id: "handle-csv-row",
468+
schema: CSVRow,
469+
run: async (row, { ctx }) => {
470+
// Do some work with the row
471+
472+
// Update the parent task's metadata with the progress of this row
473+
metadata.parent.increment("processedRows", 1).append("rowRuns", ctx.run.id);
474+
475+
return row;
476+
},
477+
});
478+
479+
export const handleCSVUpload = schemaTask({
480+
id: "handle-csv-upload",
481+
schema: UploadedFileData,
482+
run: async (file, { ctx }) => {
483+
metadata.set("status", "fetching");
484+
485+
const rows = await parseCSVFromUrl(file.url);
486+
487+
metadata.set("status", "processing").set("totalRows", rows.length);
488+
489+
const results = await batch.triggerAndWait<typeof handleCSVRow>(
490+
rows.map((row) => ({ id: "handle-csv-row", payload: row }))
491+
);
492+
493+
metadata.set("status", "complete");
494+
495+
return {
496+
file,
497+
rows,
498+
results,
499+
};
500+
},
501+
});
502+
```
503+
504+
Combined with [Realtime](/realtime), you could use this to show a live progress bar of the CSV processing in your frontend, like this:
505+
506+
<video
507+
src="https://content.trigger.dev/csv-upload-realtime.mp4"
508+
preload="auto"
509+
controls={true}
510+
loop
511+
muted
512+
autoPlay={true}
513+
width="100%"
514+
height="100%"
515+
/>
516+
381517
## Metadata propagation
382518

383519
Metadata is NOT propagated to child tasks. If you want to pass metadata to a child task, you must do so explicitly:
@@ -488,4 +624,4 @@ See the [API reference](/management/runs/retrieve) for more information.
488624

489625
## Size limit
490626

491-
The maximum size of the metadata object is 4KB. If you exceed this limit, the SDK will throw an error. If you are self-hosting Trigger.dev, you can increase this limit by setting the `TASK_RUN_METADATA_MAXIMUM_SIZE` environment variable. For example, to increase the limit to 16KB, you would set `TASK_RUN_METADATA_MAXIMUM_SIZE=16384`.
627+
The maximum size of the metadata object is 256KB. If you exceed this limit, the SDK will throw an error. If you are self-hosting Trigger.dev, you can increase this limit by setting the `TASK_RUN_METADATA_MAXIMUM_SIZE` environment variable. For example, to increase the limit to 16KB, you would set `TASK_RUN_METADATA_MAXIMUM_SIZE=16384`.

packages/core/src/v3/types/tasks.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -763,7 +763,7 @@ export type TriggerOptions = {
763763
tags?: RunTags;
764764

765765
/**
766-
* Metadata to attach to the run. Metadata can be used to store additional information about the run. Limited to 4KB.
766+
* Metadata to attach to the run. Metadata can be used to store additional information about the run. Limited to 256KB.
767767
*/
768768
metadata?: Record<string, SerializableJson>;
769769

0 commit comments

Comments
 (0)