Concepts
Live progress
A running task can report metadata and milestones, and start other tasks without waiting. Your app follows them live on the run stream.
On this page
Report progress from a task#
The context has three calls for work in progress. None of them waits, and none of them is a workflow step, so plain tasks can use them too.
import {loadRows} from './warehouse.mjs'; // Reports progress while it works, then starts a report// without waiting for it.export async function importFile({file, rows}, ctx) { ctx.metadata.set('file', file); const batches = Math.ceil(rows.length / 200); for (let i = 0; i < batches; i++) { const batch = rows.slice(i * 200, (i + 1) * 200); await loadRows(batch, `${ctx.idempotencyKey}:${i}`); ctx.metadata.append('loaded', batch.length); ctx.milestone(`batch ${i + 1}`, { progress: (i + 1) / batches, detail: `${batch.length} rows`, }); } const report = ctx.trigger('reports.build', {file}, {key: 'report'}); return {rows: rows.length, report: report.id};}import math from warehouse import load_rows # Reports progress while it works, then starts a report# without waiting for it.def import_file(payload, ctx): file, rows = payload["file"], payload["rows"] ctx.metadata.set("file", file) batches = math.ceil(len(rows) / 200) for i in range(batches): batch = rows[i * 200:(i + 1) * 200] load_rows(batch, f"{ctx.idempotency_key}:{i}") ctx.metadata.append("loaded", len(batch)) ctx.milestone( f"batch {i + 1}", progress=(i + 1) / batches, detail=f"{len(batch)} rows", ) report = ctx.trigger("reports.build", {"file": file}, key="report") return {"rows": len(rows), "report": report["id"]}| Node | Python | What it does |
|---|---|---|
ctx.metadata.set(key, value) | ctx.metadata.set(key, value) | Sets a key in the run's metadata. |
ctx.metadata.append(key, value) | ctx.metadata.append(key, value) | Appends to the array at key, creating it when absent. |
ctx.metadata.get(key) | ctx.metadata.get(key, default=None) | A copy of the value at key. |
ctx.metadata.current() | ctx.metadata.current() | A copy of the whole object. |
ctx.milestone(name, {progress, detail}) | ctx.milestone(name, progress=None, detail=None) | Records a named step. progress is a number from 0 to 1, and detail a short string. |
ctx.trigger(task, input, {key, machine, scope, delay, debounce}) | ctx.trigger(task, input, key=None, machine=None, scope=None, delay=None, debounce=None) | Starts another task without waiting, and returns {id} at once. |
Metadata#
Metadata is a JSON object of at most 64 KiB, kept with the run. It survives retries: a task's next attempt reads what earlier attempts stored, so a retry can carry on counting where the last attempt stopped. get and current show the stored object with this attempt's own changes. A workflow starts each replay from an empty object and rebuilds it as its code runs again. Use metadata for counters and state a dashboard shows, such as loaded or currentStep.
In a workflow, code that runs again during a replay does not send its updates twice.
Milestones#
A milestone is an entry in the run's timeline: a name of 1 to 128 bytes, an optional progress and an optional detail of at most 1,024 bytes. Milestones are never overwritten. Each one is an event on the run stream, and a run's snapshot carries its newest 20.
Start tasks without waiting#
ctx.trigger starts a child run and returns its ID at once. The parent does not wait for it, and the child's result does not reach the parent.
- Retries never duplicate a child. The child's ID comes from the parent's run ID and the trigger's key. A retried parent that triggers with the same key gets the same child, not a second one. A retry from failure does too: its children's IDs come from its first run's ID. Without a key, the nth trigger of a task in an execution uses
trigger:<task>:<n>. Adding a trigger of another task shifts nothing, but keep the order of one task's triggers stable or pass keys. - A key is the parent run's, whatever its length. To start one child across every run in the environment, pass
scope: 'environment'(scope="environment"in Python) with the key. The child's ID then comes from the environment, the task and the key, and every run that triggers the task with it gets the one child. One key used for two tasks starts two children, as in Trigger.dev. The child belongs to the run that triggered it first. Tasks written the Trigger.dev way get this foridempotencyKeys.createkeys, as in Trigger.dev. - The ID is not a promise. Bok Joy can still refuse a trigger, for example for a task the deployment does not have. The refusal does not fail the parent. It is recorded on the parent's attempt as an
update.refusedevent. Check that a child you depend on exists before you rely on it. - Later, or once for many triggers.
delaystarts the child later, anddebouncecollapses triggers with one key into one child, as on a trigger from your backend. A baddelayordebouncethrows wherectx.triggeris called, and no child starts. A trigger a debounce folds into a child that already waits returns the ID it made, which then names that child. - To wait for the child, use
ctx.triggerAndWait, orctx.callTaskin a workflow, when the task needs its result.ctx.triggeris for work that can finish on its own.
A triggered child runs the parent's deployment, in the same environment, with the same placement and variables. Pass machine to size it differently.
Follow it from your app#
The run stream carries every change to a run and the runs below it: status changes, attempts, metadata and milestones. Subscribe from your backend:
for await (const message of runs.subscribe(runId)) { if (message.type === 'event' && message.kind === 'milestone') { console.log(message.data.name, message.data.progress); }}for message in runs.subscribe(run_id): if message["type"] == "event" and message["kind"] == "milestone": print(message["data"]["name"], message["data"]["progress"])Read, cancel and follow runs covers the messages in full. From the CLI, durable-compute watch <run id> prints the same changes, and the console shows them as a live trace.
Limits#
Each attempt can send at most 1,400 updates. 1,000 of them are kept for ctx.trigger, so progress reports can never crowd out a child:
- Past 400 metadata and milestone updates, or 6 MiB of them, later ones are dropped with one warning in the logs. The task keeps running.
- Past 1,000 triggers in one attempt,
ctx.triggerthrows. On a worker agent from before this release the limits are 500 updates and 100 triggers. - One update is at most 256 KiB, and all updates of an attempt at most 8 MiB.
A run tree is at most 32 runs deep, and holds at most 10,000 triggered and batched runs by default: the children of ctx.trigger and of ctx.batchTriggerAndWait count together. A trigger past either bound throws, or is refused. A batch past them starts nothing and fails the workflow's attempt. Limits lists every number.