Configure what you want to happen when there is more than one run at a time.
concurrencyLimit property on the task’s queue. This limits the number of runs that can be executing at any one time:// This task will only run one at a timeexport const oneAtATime = task({ id: "one-at-a-time", queue: { concurrencyLimit: 1, }, run: async (payload)=> { //... },});export const myQueue = queue({ name: "my-queue", concurrencyLimit: 1,});export const task1 = task({ id: "task-1", queue: myQueue, run: async (payload: {message: string })=> { // ... },});export const task2 = task({ id: "task-2", queue: myQueue, run: async (payload: {message: string })=> { // ... },});task1 andtask2 share the same queue, so only one of them can run at a time.const paidQueue = queue({ name: "paid-users", concurrencyLimit: 10,});export const generatePullRequest = task({ id: "generate-pull-request", queue: { //normally when triggering this task it will be limited to 1 run at a time concurrencyLimit: 1, }, run: async (payload)=> { //todo generate a PR using OpenAI },});import {generatePullRequest }from "~/trigger/override-concurrency";export async function POST(request: Request) { const data = await request.json(); if (data.branch === "main") { //trigger the task, with the paid users queue const handle = await generatePullRequest.trigger(data, { // Set the paid users queue queue: "paid-users", }); return Response.json(handle); }else { //triggered with the default queue (concurrency of 1) const handle = await generatePullRequest.trigger(data); return Response.json(handle); }}concurrencyKey. It creates a copy of the queue for each unique value of the key.Your backend code:import {generatePullRequest }from "~/trigger/override-concurrency";export async function POST(request: Request) { const data = await request.json(); if (data.isFreeUser) { //the "free-users" queue has a concurrency limit of 1 const handle = await generatePullRequest.trigger(data, { queue: "free-users", //this creates a free-users queue for each user concurrencyKey: data.userId, }); //return a success response with the handle return Response.json(handle); }else { //the "paid-users" queue has a concurrency limit of 10 const handle = await generatePullRequest.trigger(data, { queue: "paid-users", //this creates a paid-users queue for each user concurrencyKey: data.userId, }); //return a success response with the handle return Response.json(handle); }}export const parentTask = task({ id: "parent-task", run: async (payload)=> { //trigger a subtask await subtask.triggerAndWait(payload); },});// This subtask will run on its own queueexport const subtask = task({ id: "subtask", run: async (payload)=> { //... },});WAITING state and releases its concurrency slot back to both the queue and the environment, allowing other runs to execute or resume.This means that:WAITING state (checkpointed at waitpoints) do not consume concurrency slotsWAITING state than your queue’s concurrency limitconcurrencyLimit of 1:WAITING state that belong to that queueexport const parentTask = task({ id: "parent-task", queue: { concurrencyLimit: 1, }, run: async (payload)=> { //trigger a subtask and wait for it to complete await subtask.triggerAndWait(payload); // The parent task checkpoints here and releases its concurrency slot // allowing other tasks to execute while waiting },});export const subtask = task({ id: "subtask", run: async (payload)=> { //... },});triggerAndWait call, it checkpoints and transitions to theWAITING state, releasing its concurrency slot back to both its queue and the environment. Once the subtask completes, the parent task will resume and re-acquire a concurrency slot.Was this page helpful?