-
-
Notifications
You must be signed in to change notification settings - Fork 1.4k
feat(sdk,core,webapp,run-engine): runtime overrides for total and per-key queue limits #4829
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
matt-aitken
wants to merge
7
commits into
feat/queue-gates-contract
Choose a base branch
from
feat/queue-concurrency-overrides
base: feat/queue-gates-contract
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+1,573
−22
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
900c8ae
feat(run-engine): per-concurrency-key limit override storage and methods
matt-aitken 0e43f6c
feat(run-engine): enforce per-key limit overrides at admit time
matt-aitken 2bafa19
feat(database): total-override bookkeeping and per-key override table
matt-aitken fa6d12e
feat(sdk,core,webapp): runtime overrides for total and per-key limits
matt-aitken 3a7a5c8
fix(run-engine,webapp,sdk): converge override failures and unpin bloc…
matt-aitken e7844da
fix(run-engine,webapp): gate admission honors per-key overrides; hard…
matt-aitken fbd79d2
fix(run-engine,webapp): flag-consistent gate overrides; generation-sa…
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 in total. `queues.overrideConcurrencyLimit` accepts a `concurrencyKey` to raise or lower one key's limit without touching the rest of the queue, and the new `queues.overrideTotalConcurrencyLimit` and `queues.resetTotalConcurrencyLimit` 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.overrideTotalConcurrencyLimit("my-queue", 100); | ||
| await queues.resetTotalConcurrencyLimit("my-queue"); | ||
| ``` | ||
|
|
||
| Overrides survive deploys and reset back to the declared configuration. Enforcement happens server-side on servers with total concurrency limits enabled. |
103 changes: 103 additions & 0 deletions
103
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,103 @@ | ||
| 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; | ||
95 changes: 95 additions & 0 deletions
95
apps/webapp/app/routes/api.v1.queues.$queueParam.concurrency.key.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,95 @@ | ||
| 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), | ||
| }); | ||
|
|
||
| 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 | ||
| .resetConcurrencyKeyLimit(authentication.environment, input, body.concurrencyKey) | ||
| .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: "This concurrency key does not have an override" }, | ||
| { status: 400 } | ||
| ); | ||
| } | ||
| case "queue_update_failed": { | ||
| return json({ error: "Failed to reset the 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 reset the concurrency key limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| default: { | ||
| return json( | ||
| { error: "Failed to reset the concurrency key limit" }, | ||
| { | ||
| status: 500, | ||
| } | ||
| ); | ||
| } | ||
| } | ||
| } | ||
| ); | ||
| } | ||
| ); | ||
|
|
||
| export const { action } = route; |
96 changes: 96 additions & 0 deletions
96
apps/webapp/app/routes/api.v1.queues.$queueParam.concurrency.total.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,96 @@ | ||
| 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; |
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.