feat(sdk,core,webapp,run-engine): runtime overrides for combined and per-key queue limits #4829
新建议题
Have a question about this project? 注册 for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “注册 for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? 登录 to your account
Open
matt-aitken
wants to merge
9
commits into
feat/queue-gates-contract
from
feat/queue-concurrency-overrides
Open
Changes from all commits
提交
Show all changes
9 commits
Select commit
Hold shift + click to select a range
6d03205
feat(run-engine): per-concurrency-key limit override storage and methods
matt-aitken 3b45051
feat(run-engine): enforce per-key limit overrides at admit time
matt-aitken 4c82831
feat(database): total-override bookkeeping and per-key override table
matt-aitken c7aebc9
feat(sdk,core,webapp): runtime overrides for total and per-key limits
matt-aitken d4e9b1f
fix(run-engine,webapp,sdk): converge override failures and unpin bloc…
matt-aitken 09ce303
fix(run-engine,webapp): gate admission honors per-key overrides; hard…
matt-aitken 584d6b5
fix(run-engine,webapp): flag-consistent gate overrides; generation-sa…
matt-aitken f5b4112
fix(webapp): export queue concurrency route handlers by property access
matt-aitken 0bef7b3
refactor(sdk,core,webapp): combined concurrency override API names
matt-aitken File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,18 @@ | ||
| --- | ||
| "@trigger.dev/sdk": patch | ||
| "@trigger.dev/core": patch | ||
| --- | ||
|
|
||
| Adjust queue concurrency at runtime, per key and combined. `queues.overrideConcurrencyLimit` accepts a `concurrencyKey` to raise or lower one key's limit without touching the rest of the queue, and the new `queues.overrideCombinedConcurrencyLimit` and `queues.resetCombinedConcurrencyLimit` adjust the cap across all keys. | ||
|
|
||
| ```ts | ||
| import { queues } from "@trigger.dev/sdk"; | ||
|
|
||
| await queues.overrideConcurrencyLimit("my-queue", 20, { concurrencyKey: "tenant-123" }); | ||
| await queues.resetConcurrencyLimit("my-queue", { concurrencyKey: "tenant-123" }); | ||
|
|
||
| await queues.overrideCombinedConcurrencyLimit("my-queue", 100); | ||
| await queues.resetCombinedConcurrencyLimit("my-queue"); | ||
| ``` | ||
|
|
||
| Overrides survive deploys and reset back to the declared configuration. Enforcement happens server-side on servers with combined concurrency limits enabled. |
98 changes: 98 additions & 0 deletions
98
apps/webapp/app/routes/api.v1.queues.$queueParam.concurrency.combined.override.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,98 @@ | ||
| import { json } from "@remix-run/server-runtime"; | ||
| import { type RetrieveQueueParam, RetrieveQueueType } from "@trigger.dev/core/v3"; | ||
| import { z } from "zod"; | ||
| import { toQueueItem } from "~/presenters/v3/QueueRetrievePresenter.server"; | ||
| import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server"; | ||
| import { concurrencySystem } from "~/v3/services/concurrencySystemInstance.server"; | ||
|
|
||
| const BodySchema = z.object({ | ||
| type: RetrieveQueueType.default("id"), | ||
| concurrencyLimit: z.number().int().min(0).max(100000), | ||
| }); | ||
|
|
||
| const route = createActionApiRoute( | ||
| { | ||
| body: BodySchema, | ||
| params: z.object({ | ||
| queueParam: z.string().transform((val) => val.replace(/%2F/g, "/")), | ||
| }), | ||
| authorization: { | ||
| action: "write", | ||
| resource: () => ({ type: "queues" }), | ||
| }, | ||
| }, | ||
| async ({ params, body, authentication }) => { | ||
| const input: RetrieveQueueParam = | ||
| body.type === "id" | ||
| ? params.queueParam | ||
| : { | ||
| type: body.type, | ||
| name: decodeURIComponent(params.queueParam).replace(/%2F/g, "/"), | ||
| }; | ||
|
|
||
| return concurrencySystem.queues | ||
| .overrideTotalConcurrencyLimit(authentication.environment, input, body.concurrencyLimit) | ||
| .match( | ||
| (queue) => { | ||
| return json( | ||
| toQueueItem({ | ||
| friendlyId: queue.friendlyId, | ||
| name: queue.name, | ||
| type: queue.type, | ||
| running: queue.running, | ||
| queued: queue.queued, | ||
| concurrencyLimit: queue.concurrencyLimit, | ||
| concurrencyLimitBase: queue.concurrencyLimitBase, | ||
| concurrencyLimitOverriddenAt: queue.concurrencyLimitOverriddenAt, | ||
| concurrencyLimitOverriddenBy: null, | ||
| paused: queue.paused, | ||
| }), | ||
| { status: 200 } | ||
| ); | ||
| }, | ||
| (error) => { | ||
| switch (error.type) { | ||
| case "queue_not_found": { | ||
| return json({ error: "Queue not found" }, { status: 404 }); | ||
| } | ||
| case "invalid_override": | ||
| case "concurrency_limit_exceeds_maximum": { | ||
| return json({ error: error.message }, { status: 400 }); | ||
| } | ||
| case "queue_update_failed": { | ||
| return json( | ||
| { error: "Failed to update queue total concurrency limit" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| case "sync_queue_concurrency_to_engine_failed": { | ||
| return json({ error: "Failed to sync the total concurrency limit" }, { status: 500 }); | ||
| } | ||
| case "get_queue_stats_failed": { | ||
| return json({ error: "Failed to read queue stats" }, { status: 500 }); | ||
| } | ||
| case "other": { | ||
| return json( | ||
| { error: "Failed to update queue total concurrency limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| default: { | ||
| return json( | ||
| { error: "Failed to update queue total concurrency limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| export const action = route.action; | ||
| /** The builder's loader answers non-POST methods with a 405. */ | ||
| export const loader = route.loader; |
99 changes: 99 additions & 0 deletions
99
apps/webapp/app/routes/api.v1.queues.$queueParam.concurrency.combined.reset.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,99 @@ | ||
| import { json } from "@remix-run/server-runtime"; | ||
| import { type RetrieveQueueParam, RetrieveQueueType } from "@trigger.dev/core/v3"; | ||
| import { z } from "zod"; | ||
| import { toQueueItem } from "~/presenters/v3/QueueRetrievePresenter.server"; | ||
| import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server"; | ||
| import { concurrencySystem } from "~/v3/services/concurrencySystemInstance.server"; | ||
|
|
||
| const BodySchema = z.object({ | ||
| type: RetrieveQueueType.default("id"), | ||
| }); | ||
|
|
||
| const route = createActionApiRoute( | ||
| { | ||
| body: BodySchema, | ||
| params: z.object({ | ||
| queueParam: z.string().transform((val) => val.replace(/%2F/g, "/")), | ||
| }), | ||
| authorization: { | ||
| action: "write", | ||
| resource: () => ({ type: "queues" }), | ||
| }, | ||
| }, | ||
| async ({ params, body, authentication }) => { | ||
| const input: RetrieveQueueParam = | ||
| body.type === "id" | ||
| ? params.queueParam | ||
| : { | ||
| type: body.type, | ||
| name: decodeURIComponent(params.queueParam).replace(/%2F/g, "/"), | ||
| }; | ||
|
|
||
| return concurrencySystem.queues | ||
| .resetTotalConcurrencyLimit(authentication.environment, input) | ||
| .match( | ||
| (queue) => { | ||
| return json( | ||
| toQueueItem({ | ||
| friendlyId: queue.friendlyId, | ||
| name: queue.name, | ||
| type: queue.type, | ||
| running: queue.running, | ||
| queued: queue.queued, | ||
| concurrencyLimit: queue.concurrencyLimit, | ||
| concurrencyLimitBase: queue.concurrencyLimitBase, | ||
| concurrencyLimitOverriddenAt: queue.concurrencyLimitOverriddenAt, | ||
| concurrencyLimitOverriddenBy: null, | ||
| paused: queue.paused, | ||
| }), | ||
| { status: 200 } | ||
| ); | ||
| }, | ||
| (error) => { | ||
| switch (error.type) { | ||
| case "queue_not_found": { | ||
| return json({ error: "Queue not found" }, { status: 404 }); | ||
| } | ||
| case "queue_not_overridden": { | ||
| return json( | ||
| { error: "The queue total concurrency limit is not overridden" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| case "queue_update_failed": { | ||
| return json( | ||
| { error: "Failed to reset the queue total concurrency limit" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| case "sync_queue_concurrency_to_engine_failed": { | ||
| return json({ error: "Failed to sync the total concurrency limit" }, { status: 500 }); | ||
| } | ||
| case "get_queue_stats_failed": { | ||
| return json({ error: "Failed to read queue stats" }, { status: 500 }); | ||
| } | ||
| case "other": { | ||
| return json( | ||
| { error: "Failed to reset the queue total concurrency limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| default: { | ||
| return json( | ||
| { error: "Failed to reset the queue total concurrency limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| export const action = route.action; | ||
| /** The builder's loader answers non-POST methods with a 405. */ | ||
| export const loader = route.loader; |
105 changes: 105 additions & 0 deletions
105
apps/webapp/app/routes/api.v1.queues.$queueParam.concurrency.key.override.ts
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| import { json } from "@remix-run/server-runtime"; | ||
| import { type RetrieveQueueParam, RetrieveQueueType } from "@trigger.dev/core/v3"; | ||
| import { z } from "zod"; | ||
| import { toQueueItem } from "~/presenters/v3/QueueRetrievePresenter.server"; | ||
| import { createActionApiRoute } from "~/services/routeBuilders/apiBuilder.server"; | ||
| import { concurrencySystem } from "~/v3/services/concurrencySystemInstance.server"; | ||
|
|
||
| const BodySchema = z.object({ | ||
| type: RetrieveQueueType.default("id"), | ||
| concurrencyKey: z.string().min(1).max(128), | ||
| concurrencyLimit: z.number().int().min(0).max(100000), | ||
| }); | ||
|
|
||
| const route = createActionApiRoute( | ||
| { | ||
| body: BodySchema, | ||
| params: z.object({ | ||
| queueParam: z.string().transform((val) => val.replace(/%2F/g, "/")), | ||
| }), | ||
| authorization: { | ||
| action: "write", | ||
| resource: () => ({ type: "queues" }), | ||
| }, | ||
| }, | ||
| async ({ params, body, authentication }) => { | ||
| const input: RetrieveQueueParam = | ||
| body.type === "id" | ||
| ? params.queueParam | ||
| : { | ||
| type: body.type, | ||
| name: decodeURIComponent(params.queueParam).replace(/%2F/g, "/"), | ||
| }; | ||
|
|
||
| return concurrencySystem.queues | ||
| .overrideConcurrencyKeyLimit( | ||
| authentication.environment, | ||
| input, | ||
| body.concurrencyKey, | ||
| body.concurrencyLimit | ||
| ) | ||
| .match( | ||
| (queue) => { | ||
| return json( | ||
| toQueueItem({ | ||
| friendlyId: queue.friendlyId, | ||
| name: queue.name, | ||
| type: queue.type, | ||
| running: queue.running, | ||
| queued: queue.queued, | ||
| concurrencyLimit: queue.concurrencyLimit, | ||
| concurrencyLimitBase: queue.concurrencyLimitBase, | ||
| concurrencyLimitOverriddenAt: queue.concurrencyLimitOverriddenAt, | ||
| concurrencyLimitOverriddenBy: null, | ||
| paused: queue.paused, | ||
| }), | ||
| { status: 200 } | ||
| ); | ||
| }, | ||
| (error) => { | ||
| switch (error.type) { | ||
| case "queue_not_found": { | ||
| return json({ error: "Queue not found" }, { status: 404 }); | ||
| } | ||
| case "invalid_override": | ||
| case "concurrency_limit_exceeds_maximum": | ||
| case "too_many_key_overrides": { | ||
| return json({ error: error.message }, { status: 400 }); | ||
| } | ||
| case "queue_update_failed": { | ||
| return json( | ||
| { error: "Failed to update queue concurrency key limit" }, | ||
| { status: 500 } | ||
| ); | ||
| } | ||
| case "sync_queue_concurrency_to_engine_failed": { | ||
| return json({ error: "Failed to sync the concurrency key limit" }, { status: 500 }); | ||
| } | ||
| case "get_queue_stats_failed": { | ||
| return json({ error: "Failed to read queue stats" }, { status: 500 }); | ||
| } | ||
| case "other": { | ||
| return json( | ||
| { error: "Failed to update queue concurrency key limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| default: { | ||
| return json( | ||
| { error: "Failed to update queue concurrency key limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| export const action = route.action; | ||
| /** The builder's loader answers non-POST methods with a 405. */ | ||
| export const loader = route.loader; | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.