Understanding the lifecycle of task run execution in Trigger.dev
In Trigger.dev, the concepts of runs and attempts are fundamental to understanding how tasks are executed and managed. This article explains these concepts in detail and provides insights into the various states a run can go through during its lifecycle.
A run is created when you trigger a task (e.g. calling yourTask.trigger({ foo: "bar" })
). It represents a single instance of a task being executed and contains the following key information:
A run can go through various states during its lifecycle. The following diagram illustrates a typical state transition where a single run is triggered and completes successfully:
Runs can also find themselves in lots of other states depending on what’s happening at any given time. The following sections describe all the possible states in more detail.
Waiting for deploy: If a task is triggered before it has been deployed, the run enters this state and waits for the task to be deployed.
Delayed: When a run is triggered with a delay, it enters this state until the specified delay period has passed.
Queued: The run is ready to be executed and is waiting in the queue.
Executing: The task is currently running.
Reattempting: The task has failed and is being retried.
Waiting: You have used a triggerAndWait(), batchTriggerAndWait() or a wait function. When the wait is complete, the task will resume execution.
Completed: The task has successfully finished execution.
Canceled: The run was manually canceled by the user.
Failed: The task has failed to complete successfully.
Timed out: Task has
failed because it exceeded its maxDuration
.
Crashed: The worker process crashed during execution (likely due to an Out of Memory error).
Interrupted: In development mode, when the CLI is disconnected.
System failure: An unrecoverable system error has occurred.
Expired: The run’s Time-to-Live (TTL) has passed before it could start executing.
An attempt represents a single execution of a task within a run. A run can have one or more attempts, depending on the task’s retry settings and whether it fails. Each attempt has:
When a task fails, it will be retried according to its retry settings, creating new attempts until it either succeeds or reaches the retry limit.
A run is considered finished when:
At this point, the run will have either an output (if successful) or an error (if failed).
When triggering a task, you can provide an idempotency key to ensure the task is executed only once, even if triggered multiple times. This is useful for preventing duplicate executions in distributed systems.
See our Idempotency docs for more information.
You can cancel an in-progress run using the API or the dashboard:
When a run is canceled:
– The task execution is stopped
– The run is marked as canceled
– The task will not be retried
– Any in-progress child runs are also canceled
TTL is a time-to-live setting that defines the maximum duration a run can remain in a queued state before being automatically expired. You can set a TTL when triggering a run:
If the run hasn’t started within the specified TTL, it will automatically expire, returning the status Expired
. This is useful for time-sensitive tasks where immediate execution is important. For example, when you queue many runs simultaneously and exceed your concurrency limits, some runs might be delayed - using TTL ensures they only execute if they can start within your specified timeframe.
Note that dev runs automatically have a 10-minute TTL. In Staging and Production environments, no TTL is set by default.
You can schedule a run to start after a specified delay:
This is useful for tasks that need to be executed at a specific time in the future.
You can create a new run with the same payload as a previous run:
This is useful for re-running a task with the same input, especially for debugging or recovering from failures. The new run will use the latest version of the task.
You can also replay runs from the dashboard using the same or different payload. Learn how to do this here.
The triggerAndWait()
function triggers a task and then lets you wait for the result before continuing. Learn more about triggerAndWait().
Similar to triggerAndWait()
, the batchTriggerAndWait()
function lets you batch trigger a task and wait for all the results Learn more about batchTriggerAndWait().
List runs in a specific environment. You can filter the runs by status, created at, task identifier, version, and more:
You can also use an Async Iterator to get all runs:
You can provide multiple filters to the list()
function to narrow down the results:
Fetch a single run by it’s ID:
You can provide the type of the task to correctly type the run.payload
and run.output
:
If you have just triggered a run, you can pass the entire response object to retrieve()
and the response will already be typed:
Cancel a run:
Replay a run:
Updates a delayed run with a new delay. Only valid when the run is in the DELAYED state.
Subscribe to changes to a specific run in real-time:
Similar to runs.retrieve()
, you can provide the type of the task to correctly type the run.payload
and run.output
:
For more on real-time updates, see the Realtime documentation.
It’s possible to trigger a run for a task that hasn’t been deployed yet. The run will enter the “Waiting for deploy” state until the task is deployed. Once deployed, the run will be queued and executed normally. This feature is particularly useful in CI/CD pipelines where you want to trigger tasks before the deployment is complete.