Skip to content

Commit 6cfe890

Browse files
committed
Queuing docs simplified, partial written
1 parent f480fd5 commit 6cfe890

File tree

8 files changed

+86
-39
lines changed

8 files changed

+86
-39
lines changed

docs/mint.json

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -485,11 +485,7 @@
485485
"v3/wait-for-request"
486486
]
487487
},
488-
"v3/environment-variables",
489-
{
490-
"group": "Queues",
491-
"pages": ["v3/concurrency", "v3/one-at-a-time", "v3/parallelism", "v3/per-tenant"]
492-
},
488+
"v3/queue-concurrency",
493489
{
494490
"group": "Testing",
495491
"pages": ["v3/run-tests", "v3/automated-tests"]

docs/v3/concurrency.mdx

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/v3/environment-variables.mdx

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/v3/one-at-a-time.mdx

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/v3/parallelism.mdx

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/v3/per-tenant.mdx

Lines changed: 0 additions & 6 deletions
This file was deleted.

docs/v3/queue-concurrency.mdx

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,81 @@
1+
---
2+
title: "Concurrency & Queues"
3+
description: "Configure what you want to happen when there is more than one run at a time."
4+
---
5+
6+
Controlling concurrency is useful when you have a task that can't be run concurrently, or when you want to limit the number of runs to avoid overloading a resource.
7+
8+
## One at a time
9+
10+
This task will only ever have a single run executing at a time. All other runs will be queued until the current run is complete.
11+
12+
```ts /trigger/one-at-a-time.ts
13+
export const oneAtATime = task({
14+
id: "one-at-a-time",
15+
queue: {
16+
concurrencyLimit: 1,
17+
},
18+
run: async ({ payload, ctx }) => {
19+
//...
20+
},
21+
});
22+
```
23+
24+
## Parallelism
25+
26+
You can execute lots of tasks at once by combining high concurrency with [batch triggering](/v3/triggering) (or just triggering in a loop).
27+
28+
```ts /trigger/parallelism.ts
29+
export const parallelism = task({
30+
id: "parallelism",
31+
queue: {
32+
concurrencyLimit: 100,
33+
},
34+
run: async ({ payload, ctx }) => {
35+
//...
36+
},
37+
});
38+
```
39+
40+
<Warning>
41+
Be careful with high concurrency. If you're doing API requests you might hit rate limits. If
42+
you're hitting your database you might overload it.
43+
</Warning>
44+
45+
<Note>
46+
Your organization has a maximum concurrency limit which depends on your plan. If you're a paying
47+
customer you can request a higher limit by [contacting us](https://www.trigger.dev/contact).
48+
</Note>
49+
50+
## Defining a queue
51+
52+
As well as putting queue settings directly on a task, you can define a queue and reuse it across multiple tasks. This allows you to share the same concurrency limit:
53+
54+
```ts /trigger/queue.ts
55+
const myQueue = queue({
56+
name: "my-queue",
57+
concurrencyLimit: 1,
58+
});
59+
60+
export const task1 = task({
61+
id: "task-1",
62+
queue: {
63+
name: "my-queue",
64+
},
65+
run: async (payload: { message: string }) => {
66+
// ...
67+
},
68+
});
69+
70+
export const task2 = task({
71+
id: "task-2",
72+
queue: {
73+
name: "my-queue",
74+
},
75+
run: async (payload: { message: string }) => {
76+
// ...
77+
},
78+
});
79+
```
80+
81+
## Per-tenant queuing

docs/v3/tasks-overview.mdx

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -69,7 +69,7 @@ A task is retried if an error is thrown, by default we retry 3 times.
6969

7070
You can set the number of retries and the delay between retries in the `retry` field:
7171

72-
```ts
72+
```ts /trigger/retry.ts
7373
export const taskWithRetries = task({
7474
id: "task-with-retries",
7575
retry: {
@@ -91,9 +91,9 @@ It's also worth mentioning that you can [retry a block of code](/v3/retrying) in
9191

9292
### `queue` options
9393

94-
Queues allow you to control the concurrency of your tasks. The simplest use cases are to have [one-at-a-time](/v3/one-at-a-time) execution or lots of [parallel](/v3/parallelism) executions. There are more advanced situations where you want to [control the concurrency for different sets of your users](/v3/per-tenant). For more information read [the queues guide](/v3/concurrency).
94+
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](/v3/queue-concurrency).
9595

96-
```ts
96+
```ts /trigger/one-at-a-time.ts
9797
export const oneAtATime = task({
9898
id: "one-at-a-time",
9999
queue: {
@@ -109,7 +109,7 @@ export const oneAtATime = task({
109109

110110
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](/v3/machines).
111111

112-
```ts
112+
```ts /trigger/heavy-task.ts
113113
export const heavyTask = task({
114114
id: "heavy-task",
115115
machine: {

0 commit comments

Comments
 (0)