Tasks need to be triggered in order to run.
Function | This works | What it does |
---|---|---|
tasks.trigger() | Anywhere | Triggers a task and gets a handle you can use to fetch and manage the run. Read more |
tasks.batchTrigger() | Anywhere | Triggers a task multiple times and gets a handle you can use to fetch and manage the runs. Read more |
tasks.triggerAndPoll() | Anywhere | Triggers a task and then polls the run until it’s complete. Read more |
Function | This works | What it does |
---|---|---|
yourTask.trigger() | Anywhere | Triggers a task and gets a handle you can use to monitor and manage the run. It does not wait for the result. Read more |
yourTask.batchTrigger() | Anywhere | Triggers a task multiple times and gets a handle you can use to monitor and manage the runs. It does not wait for the results. Read more |
yourTask.triggerAndWait() | Inside task | Triggers a task and then waits until it’s complete. You get the result data to continue with. Read more |
yourTask.batchTriggerAndWait() | Inside task | Triggers a task multiple times in parallel and then waits until they’re all complete. You get the resulting data to continue with. Read more |
schedules.task()
to trigger it on a recurring schedule. Read the scheduled tasks docs.
TRIGGER_SECRET_KEY
environment variable. You can find the value on the API keys page in the Trigger.dev dashboard. More info on API keys.
tasks.trigger()
or tasks.batchTrigger()
SDK functions.
tasks.trigger()
, you can pass in the task type as a generic argument, giving you full
type checking. Make sure you use a type
import so that your task code is not imported into your
application.tasks.batchTrigger()
, you can pass in the task type as a generic argument, giving you
full type checking. Make sure you use a type
import so that your task code is not imported into
your application.tasks.triggerAndPoll()
, you can pass in the task type as a generic argument, giving you
full type checking. Make sure you use a type
import so that your task code is not imported into
your application.Task
object you receive when you define a task. We recommend you use these methods inside another task to trigger subtasks.
AndWait
version to pause execution until the triggered run is complete.
If you need to call trigger()
on a task in a loop, use batchTrigger()
instead which will trigger up to 100 tasks in a single call.
Don't use this in parallel, e.g. with `Promise.all()`
batchTriggerAndWait()
if you can, or a for loop if you can’t.To control concurrency using batch triggers, you can set queue.concurrencyLimit
on the child task.result
object is a “Result” type that needs to be checked to see if the child task run was successful:
unwrap
method:
Don't use this in parallel, e.g. with `Promise.all()`
maxConcurrency
. Alternatively, use sequentially with a for loop.To control concurrency, you can set queue.concurrencyLimit
on the child task.How to handle run failures
batchTriggerAndWait
, you have full control over how to handle failures within the batch. The method returns an array of run results, allowing you to inspect each run’s outcome individually and implement custom error handling.Here’s how you can manage run failures:ok
property indicating success or failure.
error
property to get details about the failure.
delay
delay
option:
runs.cancel
SDK function:
runs.reschedule
SDK function:
delay
option is also available when using batchTrigger
:
ttl
ttl
of 10 minutes. You can disable this by setting the
ttl
option.delay
and ttl
, the TTL will start counting down from the time the run is enqueued, not from the time the run is triggered.
So for example, when using the following code:
ttl
option only accepts durations and not absolute timestamps.
idempotencyKey
idempotencyKey
to ensure that a task is only triggered once with the same key. This is useful if you are triggering a task within another task that might be retried:
queue
concurrencyKey
concurrencyKey
. It creates a separate queue for each value of the key.
Your backend code:
maxAttempts
retry.maxAttempts
value set in the task definition.
tags
metadata
maxDuration
payloadPresignedUrl
from the runs.retrieve
SDK function so you can download the payload if needed:
outputPresignedUrl
. Task outputs are limited to 100MB.batchTrigger
or batchTriggerAndWait
, the total size of all payloads cannot exceed 10MB. This means if you are doing a batch of 100 runs, each payload should be less than 100KB.