Hello world task and how to trigger it
Here’s an incredibly simple task:/trigger/hello-world.ts
- From the dashboard using the “Test” feature.
- Trigger it from your backend code. See the full triggering guide here.
Your backend code
Defining a task
The task function takes an object with the following fields.
The id
field
This is used to identify your task so it can be triggered, managed, and you can view runs in the dashboard. This must be unique in your project – we recommend making it descriptive and unique.
The run
function
Your custom code inside run()
will be executed when your task is triggered. It’s an async function that has two arguments:
- The run payload - the data that you pass to the task when you trigger it.
- An object with
ctx
about the run (Context), and any output from the optionalinit
function that runs before every run attempt.
run
function will be the result of the task. Data you return must be JSON serializable: strings, numbers, booleans, arrays, objects, and null.
retry
options
A task is retried if an error is thrown, by default we retry 3 times.
You can set the number of retries and the delay between retries in the retry
field:
/trigger/retry.ts
queue
options
Queues allow you to control the concurrency of your tasks. This allows you to have one-at-a-time execution and parallel executions. There are also more advanced techniques like having different concurrencies for different sets of your users. For more information read the concurrency & queues guide.
/trigger/one-at-a-time.ts
machine
options
Some tasks require more vCPUs or GBs of RAM. You can specify these requirements in the machine
field. For more information read the machines guide.
/trigger/heavy-task.ts
maxDuration
option
By default tasks can execute indefinitely, which can be great! But you also might want to set a maxDuration
to prevent a task from running too long. You can set the maxDuration
on a task, and all runs of that task will be stopped if they exceed the duration.
/trigger/long-task.ts
Global lifecycle hooks
When specifying global lifecycle hooks, we recommend using the
init.ts
file.trigger.config.ts
file, you can also register them anywhere in your codebase:
init.ts
If you create a init.ts
file at the root of your trigger directory, it will be automatically loaded when a task is executed. This is useful if you want to register global lifecycle hooks, or initialize a database connection, etc.
init.ts
Lifecycle functions

middleware
and locals
functions
Our task middleware system runs at the top level, executing before and after all lifecycle hooks. This allows you to wrap the entire task execution lifecycle with custom logic.
An error thrown in
middleware
is just like an uncaught error in the run function: it will
propagate through to catchError()
function and then will fail the attempt (either causing a
retry or failing the run).locals
API allows you to share data between middleware and hooks.
db.ts
getDb()
in your tasks run
function and all your hooks (global or task specific):
onStartAttempt
function
The
onStartAttempt
function was introduced in v4.1.0onStartAttempt
function is called. It’s useful for sending notifications, logging, and other side effects.
/trigger/on-start.ts
onStartAttempt
function using tasks.onStartAttempt()
.
init.ts
Errors thrown in the
onStartAttempt
function will cause the attempt to fail.onStartAttempt
function and check ctx.run.attempt.number === 1
:
/trigger/on-start-attempt.ts
onWait
and onResume
functions
These lifecycle hooks allow you to run code when a run is paused or resumed because of a wait:
onWait
and onResume
functions using tasks.onWait()
and tasks.onResume()
:
init.ts
onSuccess
function
When a task run succeeds, the onSuccess
function is called. It’s useful for sending notifications, logging, syncing state to your database, or other side effects.
/trigger/on-success.ts
onSuccess
function using tasks.onSuccess()
.
init.ts
Errors thrown in the
onSuccess
function will be ignored, but you will still be able to see them
in the dashboard.onComplete
function
This hook is executed when a run completes, regardless of whether it succeeded or failed:
/trigger/on-complete.ts
onComplete
function using tasks.onComplete()
.
init.ts
Errors thrown in the
onComplete
function will be ignored, but you will still be able to see them
in the dashboard.onFailure
function
When a task run fails, the onFailure
function is called. It’s useful for sending notifications, logging, or other side effects. It will only be executed once the task run has exhausted all its retries.
/trigger/on-failure.ts
onFailure
function using tasks.onFailure()
.
init.ts
Errors thrown in the
onFailure
function will be ignored, but you will still be able to see them
in the dashboard.onFailure
doesn’t fire for some of the run statuses like Crashed
, System failures
, and
Canceled
.catchError
functions
You can define a function that will be called when an error is thrown in the run
function, that allows you to control how the error is handled and whether the task should be retried.
Read more about catchError
in our Errors and Retrying guide.
Uncaught errors will throw a special internal error of the type
HANDLE_ERROR_ERROR
.onCancel
function
You can define an onCancel
hook that is called when a run is cancelled. This is useful if you want to clean up any resources that were allocated for the run.
onCancel
hook along with the signal
passed into the run function to interrupt a call to an external service, for example using the streamText function from the AI SDK:
onCancel
hook can optionally wait for the run
function to finish, and access the output of the run:
You will have up to 30 seconds to complete the
runPromise
in the onCancel
hook. After that
point the process will be killed.onStart
function (deprecated)
The
onStart
function was deprecated in v4.1.0. Use onStartAttempt
instead.onStart
function is called. It’s useful for sending notifications, logging, and other side effects.
This function will only be called once per run (not per attempt). If you want to run code before
each attempt, use a middleware function or the
onStartAttempt
function./trigger/on-start.ts
onStart
function using tasks.onStart()
.
init.ts
Errors thrown in the
onStart
function will cause the attempt to fail.init
function (deprecated)
The
init
hook is deprecated and will be removed in the future. Use
middleware instead./trigger/init.ts
init
function that will be available in the params of the run
, cleanup
, onSuccess
, and onFailure
functions.
/trigger/init-return.ts
Errors thrown in the
init
function will cause the attempt to fail.cleanup
function (deprecated)
The
cleanup
hook is deprecated and will be removed in the future. Use
middleware instead.run
function is executed, regardless of whether the run was successful or not. It’s useful for cleaning up resources, logging, or other side effects.
/trigger/cleanup.ts
Errors thrown in the
cleanup
function will cause the attempt to fail.