You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: docs/triggering.mdx
+91Lines changed: 91 additions & 0 deletions
Display the source diff
Display the rich diff
Original file line number
Diff line number
Diff line change
@@ -831,6 +831,97 @@ export const myTask = task({
831
831
832
832
For more information, see our [Idempotency](/idempotency) documentation.
833
833
834
+
### `debounce`
835
+
836
+
You can debounce task triggers to consolidate multiple trigger calls into a single delayed run. When a run with the same debounce key already exists in the delayed state, subsequent triggers "push" the existing run's execution time later rather than creating new runs.
837
+
838
+
This is useful for scenarios like:
839
+
840
+
- Real-time document indexing where you want to wait for the user to finish typing
841
+
- Aggregating webhook events from the same source
842
+
- Rate limiting expensive operations while still processing the final request
843
+
844
+
```ts
845
+
// First trigger creates a new run, delayed by 5 seconds
// The run only executes after 5 seconds of no new triggers
852
+
// Note: The first payload is used (first trigger wins)
853
+
```
854
+
855
+
<Note>
856
+
Debounce keys are scoped to the task identifier, so different tasks can use the same key without
857
+
conflicts.
858
+
</Note>
859
+
860
+
The `debounce` option accepts:
861
+
862
+
-`key` - A unique string to identify the debounce group (scoped to the task)
863
+
-`delay` - Duration string specifying how long to delay (e.g., "5s", "1m", "30s")
864
+
-`mode` - Optional. Controls which trigger's data is used: `"leading"` (default) or `"trailing"`
865
+
866
+
**How it works:**
867
+
868
+
1. First trigger with a debounce key creates a new delayed run
869
+
2. Subsequent triggers with the same key (while the run is still delayed) push the execution time further
870
+
3. Once no new triggers occur within the delay duration, the run executes
871
+
4. After the run starts executing, a new trigger with the same key will create a new run
872
+
873
+
**Leading vs Trailing mode:**
874
+
875
+
By default, debounce uses **leading mode** - the run executes with data from the **first** trigger.
876
+
877
+
With **trailing mode**, each subsequent trigger updates the run's data (payload, metadata, tags, maxAttempts, maxDuration, and machine), so the run executes with data from the **last** trigger:
878
+
879
+
```ts
880
+
// Leading mode (default): runs with first payload
0 commit comments