-
-
Notifications
You must be signed in to change notification settings - Fork 838
feat(server): add two admin endpoints for queue and environment concurrency debugging and repairing #2559
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
Merged
Merged
feat(server): add two admin endpoints for queue and environment concurrency debugging and repairing #2559
Changes from all commits
Commits
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
95 changes: 95 additions & 0 deletions
95
apps/webapp/app/routes/admin.api.v1.environments.$environmentId.engine.repair-queues.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 { ActionFunctionArgs, json } from "@remix-run/server-runtime"; | ||
import pMap from "p-map"; | ||
import { z } from "zod"; | ||
import { $replica, prisma } from "~/db.server"; | ||
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; | ||
import { determineEngineVersion } from "~/v3/engineVersion.server"; | ||
import { engine } from "~/v3/runEngine.server"; | ||
|
||
const ParamsSchema = z.object({ | ||
environmentId: z.string(), | ||
}); | ||
|
||
const BodySchema = z.object({ | ||
dryRun: z.boolean().default(true), | ||
queues: z.array(z.string()).default([]), | ||
}); | ||
|
||
export async function action({ request, params }: ActionFunctionArgs) { | ||
// Next authenticate the request | ||
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request); | ||
|
||
if (!authenticationResult) { | ||
return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { | ||
id: authenticationResult.userId, | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
} | ||
|
||
if (!user.admin) { | ||
return json({ error: "You must be an admin to perform this action" }, { status: 403 }); | ||
} | ||
|
||
const parsedParams = ParamsSchema.parse(params); | ||
|
||
const environment = await prisma.runtimeEnvironment.findFirst({ | ||
where: { | ||
id: parsedParams.environmentId, | ||
}, | ||
include: { | ||
organization: true, | ||
project: true, | ||
orgMember: true, | ||
}, | ||
}); | ||
|
||
if (!environment) { | ||
return json({ error: "Environment not found" }, { status: 404 }); | ||
} | ||
|
||
const engineVersion = await determineEngineVersion({ environment }); | ||
|
||
if (engineVersion === "V1") { | ||
return json({ error: "Engine version is V1" }, { status: 400 }); | ||
} | ||
|
||
const body = await request.json(); | ||
const parsedBody = BodySchema.parse(body); | ||
|
||
Comment on lines
+63
to
+65
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Guard JSON parsing and validate body with safeParse; avoid 500s on bad/missing JSON.
Apply this diff: - const body = await request.json();
- const parsedBody = BodySchema.parse(body);
+ let body: unknown;
+ try {
+ body = await request.json();
+ } catch {
+ body = {};
+ }
+ const parsedBodyResult = BodySchema.safeParse(body);
+ if (!parsedBodyResult.success) {
+ return json(
+ { error: "Invalid request body", issues: parsedBodyResult.error.flatten() },
+ { status: 400 }
+ );
+ }
+ const parsedBody = parsedBodyResult.data; |
||
const queues = await $replica.taskQueue.findMany({ | ||
where: { | ||
runtimeEnvironmentId: environment.id, | ||
version: "V2", | ||
name: parsedBody.queues.length > 0 ? { in: parsedBody.queues } : undefined, | ||
}, | ||
select: { | ||
friendlyId: true, | ||
name: true, | ||
concurrencyLimit: true, | ||
type: true, | ||
paused: true, | ||
}, | ||
orderBy: { | ||
orderableName: "asc", | ||
}, | ||
}); | ||
|
||
const repairEnvironmentResults = await engine.repairEnvironment(environment, parsedBody.dryRun); | ||
|
||
const repairResults = await pMap( | ||
queues, | ||
async (queue) => { | ||
return engine.repairQueue(environment, queue.name, parsedBody.dryRun); | ||
}, | ||
{ concurrency: 5 } | ||
); | ||
|
||
return json({ environment: repairEnvironmentResults, queues: repairResults }); | ||
} |
95 changes: 95 additions & 0 deletions
95
apps/webapp/app/routes/admin.api.v1.environments.$environmentId.engine.report.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, LoaderFunctionArgs } from "@remix-run/server-runtime"; | ||
import { z } from "zod"; | ||
import { $replica, prisma } from "~/db.server"; | ||
import { authenticateApiRequestWithPersonalAccessToken } from "~/services/personalAccessToken.server"; | ||
import { determineEngineVersion } from "~/v3/engineVersion.server"; | ||
import { engine } from "~/v3/runEngine.server"; | ||
|
||
const ParamsSchema = z.object({ | ||
environmentId: z.string(), | ||
}); | ||
|
||
const SearchParamsSchema = z.object({ | ||
verbose: z.string().default("0"), | ||
page: z.coerce.number().optional(), | ||
per_page: z.coerce.number().optional(), | ||
}); | ||
|
||
export async function loader({ request, params }: LoaderFunctionArgs) { | ||
// Next authenticate the request | ||
const authenticationResult = await authenticateApiRequestWithPersonalAccessToken(request); | ||
|
||
if (!authenticationResult) { | ||
return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
} | ||
|
||
const user = await prisma.user.findUnique({ | ||
where: { | ||
id: authenticationResult.userId, | ||
}, | ||
}); | ||
|
||
if (!user) { | ||
return json({ error: "Invalid or Missing API key" }, { status: 401 }); | ||
} | ||
|
||
if (!user.admin) { | ||
return json({ error: "You must be an admin to perform this action" }, { status: 403 }); | ||
} | ||
|
||
const parsedParams = ParamsSchema.parse(params); | ||
|
||
const environment = await prisma.runtimeEnvironment.findFirst({ | ||
where: { | ||
id: parsedParams.environmentId, | ||
}, | ||
include: { | ||
organization: true, | ||
project: true, | ||
orgMember: true, | ||
}, | ||
}); | ||
|
||
if (!environment) { | ||
return json({ error: "Environment not found" }, { status: 404 }); | ||
} | ||
|
||
const engineVersion = await determineEngineVersion({ environment }); | ||
|
||
if (engineVersion === "V1") { | ||
return json({ error: "Engine version is V1" }, { status: 400 }); | ||
} | ||
|
||
const url = new URL(request.url); | ||
const searchParams = SearchParamsSchema.parse(Object.fromEntries(url.searchParams)); | ||
|
||
const page = searchParams.page ?? 1; | ||
const perPage = searchParams.per_page ?? 50; | ||
|
||
const queues = await $replica.taskQueue.findMany({ | ||
where: { | ||
runtimeEnvironmentId: environment.id, | ||
version: "V2", | ||
}, | ||
select: { | ||
friendlyId: true, | ||
name: true, | ||
concurrencyLimit: true, | ||
type: true, | ||
paused: true, | ||
}, | ||
orderBy: { | ||
orderableName: "asc", | ||
}, | ||
skip: (page - 1) * perPage, | ||
take: perPage, | ||
}); | ||
|
||
const report = await engine.generateEnvironmentReport( | ||
environment, | ||
queues, | ||
searchParams.verbose === "1" | ||
); | ||
|
||
return json(report); | ||
} |
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
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Return 400 on invalid route params instead of throwing (use safeParse).
z.parse
will throw and produce a 500 on bad/missing params. PrefersafeParse
and return a 400 with issues.Apply this diff:
📝 Committable suggestion
🤖 Prompt for AI Agents