- Regular tasks — triggered manually from your backend or from another task.
- Scheduled tasks — triggered on a recurring cron schedule. See Scheduled tasks.
Hello world task
Here is a minimal task definition:/trigger/hello-world.ts
- From the dashboard using the Test feature.
- From your backend code using the SDK. See Triggering.
Your backend code
Defining a task
Thetask() function accepts a configuration object with the following fields.
id
A unique string identifier for your task. Used to trigger it, manage it, and view runs in the dashboard. It must be unique across your entire project.
run
Your custom logic. It is an async function that receives two arguments:
- Payload — the data passed when triggering the task.
- Context object — contains
ctx(run context) and any output from the optionalinitfunction.
return from run becomes the task output. Returned values must be JSON-serializable.
retry options
Tasks are retried on uncaught errors. By default a task retries up to 3 times with exponential backoff.
/trigger/retry.ts
queue options
Queues control the concurrency of your tasks — how many can run at the same time.
/trigger/one-at-a-time.ts
machine options
Some tasks require more vCPUs or RAM. Specify machine requirements with the machine field.
/trigger/heavy-task.ts
maxDuration option
By default, tasks can run indefinitely. Set maxDuration (in seconds) to stop a run that runs too long.
/trigger/long-task.ts
Global lifecycle hooks
When specifying global lifecycle hooks, we recommend placing them in an
init.ts file at the root
of your trigger directory.init.ts
Lifecycle functions
middleware and locals
Task middleware runs at the top level, wrapping the entire task lifecycle including all hooks. Use the locals API to share data between middleware and hooks.
db.ts
getDb():
onStartAttempt
Called before each run attempt. Useful for per-attempt setup, logging, or notifications.
/trigger/on-start-attempt.ts
Errors thrown in
onStartAttempt will cause the attempt to fail.onWait and onResume
Called when a run is paused at a waitpoint or resumed from one:
onSuccess
Called when a run completes successfully. Useful for sending notifications or syncing state to your database.
/trigger/on-success.ts
Errors thrown in
onSuccess are ignored but visible in the dashboard.onComplete
Called when a run finishes, regardless of whether it succeeded or failed:
/trigger/on-complete.ts
onFailure
Called once all retry attempts are exhausted. Useful for alerts or cleanup.
/trigger/on-failure.ts
onFailure does not fire for Crashed, System failure, or Canceled run statuses.catchError
A function called when an error is thrown in run, giving you control over retry behavior.
Read more in the Errors & Retrying guide.
onCancel
Called when a run is canceled. Use this to clean up resources or save partial results.
Next steps
Triggering
Learn how to trigger your tasks from your backend or another task.
Errors & Retrying
Handle errors and configure retry behavior.
Scheduled tasks
Run tasks on a recurring cron schedule.
Concurrency & Queues
Control how many runs execute at once.