Tasks are functions that can run for a long time and provide strong resilience to failure.
task
id
fieldrun
functionrun()
will be executed when your task is triggered. It’s an async function that has two arguments:
ctx
about the run (Context), and any output from the optional init
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
optionsretry
field:
queue
optionsmachine
optionsmachine
field. For more information read the machines guide.
maxDuration
optionmaxDuration
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.
init.ts
file.trigger.config.ts
file, you can also register them anywhere in your codebase:
init.ts
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
functioninit
function that will be available in the params of the run
, cleanup
, onSuccess
, and onFailure
functions.
init
function are ignored.cleanup
functionrun
function is executed, regardless of whether the run was successful or not. It’s useful for cleaning up resources, logging, or other side effects.
cleanup
function will fail the attempt.middleware
and locals
functionsmiddleware
is just like an uncaught error in the run function: it will
propagate through to handleError()
and then will fail the attempt (causing a retry).locals
API allows you to share data between middleware and hooks.
getDb()
in your tasks run
function and all your hooks (global or task specific):
onStart
functiononStart
function is called. It’s useful for sending notifications, logging, and other side effects. This function will only be called one per run (not per retry). If you want to run code before each retry, use the init
function.
onStart
function in your trigger.config.ts
file to get notified when any task starts.
onStart
function are ignored.onWait
and onResume
functionsonSuccess
functiononSuccess
function is called. It’s useful for sending notifications, logging, syncing state to your database, or other side effects.
onSuccess
function in your trigger.config.ts
file to get notified when any task succeeds.
onSuccess
function are ignored.onComplete
functiononFailure
functiononFailure
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.
onFailure
function in your trigger.config.ts
file to get notified when any task fails.
onFailure
function are ignored.onFailure
doesn’t fire for some of the run statuses like Crashed
, System failures
, and Canceled
.handleError
functionsrun
function, that allows you to control how the error is handled and whether the task should be retried.
Read more about handleError
in our Errors and Retrying guide.
HANDLE_ERROR_ERROR
.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:
runPromise
in the onCancel
hook. After that
point the process will be killed.