Skip to content

Commit 19aa9ab

Browse files
committed
Queuing and concurrency docs
1 parent 6cfe890 commit 19aa9ab

File tree

1 file changed

+101
-1
lines changed

1 file changed

+101
-1
lines changed

docs/v3/queue-concurrency.mdx

Lines changed: 101 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -78,4 +78,104 @@ export const task2 = task({
7878
});
7979
```
8080

81-
## Per-tenant queuing
81+
## Setting the concurrency when you trigger a run
82+
83+
When you trigger a task you can override the concurrency limit. This is really useful if you sometimes have high priority runs.
84+
85+
The task:
86+
87+
```ts /trigger/override-concurrency.ts
88+
const generatePullRequest = task({
89+
id: "generate-pull-request",
90+
queue: {
91+
//normally when triggering this task it will be limited to 1 run at a time
92+
concurrencyLimit: 1,
93+
},
94+
run: async (payload) => {
95+
//todo generate a PR using OpenAI
96+
},
97+
});
98+
```
99+
100+
Triggering from your backend and overriding the concurrency:
101+
102+
```ts app/api/push/route.ts
103+
import { generatePullRequest } from "~/trigger/override-concurrency";
104+
105+
export async function POST(request: Request) {
106+
const data = await request.json();
107+
108+
if (data.branch === "main") {
109+
//trigger the task, with a different queue
110+
const handle = await generatePullRequest.trigger({
111+
payload: data,
112+
options: {
113+
queue: {
114+
//the "main-branch" queue will have a concurrency limit of 10
115+
//this triggered run will use that queue
116+
name: "main-branch",
117+
concurrencyLimit: 10,
118+
},
119+
},
120+
});
121+
122+
return Response.json(handle);
123+
} else {
124+
//triggered with the default (concurrency of 1)
125+
const handle = await generatePullRequest.trigger({
126+
payload: data,
127+
});
128+
return Response.json(handle);
129+
}
130+
}
131+
```
132+
133+
## Concurrency keys and per-tenant queuing
134+
135+
If you're building an application where you want to run tasks for your users, you might want a separate queue for each of your users. (It doesn't have to be users, it can be any entity you want to separately limit the concurrency for.)
136+
137+
You can do this by using `concurrencyKey`. It creates a separate queue for each value of the key.
138+
139+
Your backend code:
140+
141+
```ts app/api/pr/route.ts
142+
import { generatePullRequest } from "~/trigger/override-concurrency";
143+
144+
export async function POST(request: Request) {
145+
const data = await request.json();
146+
147+
if (data.isFreeUser) {
148+
//free users can only have 1 PR generated at a time
149+
const handle = await generatePullRequest.trigger({
150+
payload: data,
151+
options: {
152+
queue: {
153+
//every free user gets a queue with a concurrency limit of 1
154+
name: "free-users",
155+
concurrencyLimit: 1,
156+
concurrencyKey: data.userId,
157+
},
158+
},
159+
});
160+
161+
//return a success response with the handle
162+
return Response.json(handle);
163+
} else {
164+
//trigger the task, with a different queue
165+
const handle = await generatePullRequest.trigger({
166+
payload: data,
167+
options: {
168+
queue: {
169+
//every paid user gets a queue with a concurrency limit of 10
170+
name: "paid-users",
171+
concurrencyLimit: 10,
172+
concurrencyKey: data.userId,
173+
},
174+
},
175+
});
176+
177+
//return a success response with the handle
178+
return Response.json(handle);
179+
}
180+
}
181+
```

0 commit comments

Comments
 (0)