Moved the run query to runs/$runId, instead of /tasks

This commit is contained in:
Matt Aitken
2023-07-24 13:28:33 +01:00
parent 5b6539d799
commit eb5195768b
4 changed files with 117 additions and 112 deletions
@@ -1,4 +1,4 @@
import type { ActionArgs, LoaderArgs } from "@remix-run/server-runtime";
import type { ActionArgs } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";
import { TaskStatus } from "@trigger.dev/database";
import {
@@ -6,7 +6,6 @@ import {
RunTaskBodyOutputSchema,
ServerTask,
} from "@trigger.dev/internal";
import { cors } from "remix-utils";
import { z } from "zod";
import { $transaction, PrismaClient, prisma } from "~/db.server";
import { taskWithAttemptsToServerTask } from "~/models/task.server";
@@ -14,7 +13,6 @@ import { authenticateApiRequest } from "~/services/apiAuth.server";
import { logger } from "~/services/logger.server";
import { ulid } from "~/services/ulid.server";
import { workerQueue } from "~/services/worker.server";
import { taskListToTree } from "~/utils/taskListToTree";
const ParamsSchema = z.object({
runId: z.string(),
@@ -24,104 +22,6 @@ const HeadersSchema = z.object({
"idempotency-key": z.string(),
});
const SearchQuerySchema = z.object({
cursor: z.string().optional(),
take: z.coerce.number().default(50),
subtasks: z.coerce.boolean().default(false),
taskdetails: z.coerce.boolean().default(false),
});
export async function loader({ request, params }: LoaderArgs) {
if (request.method.toUpperCase() === "OPTIONS") {
return cors(request, json({}));
}
const authenticatedEnv = await authenticateApiRequest(request, {
allowPublicKey: true,
});
if (!authenticatedEnv) {
return cors(
request,
json({ error: "Invalid or Missing API key" }, { status: 401 })
);
}
const { runId } = ParamsSchema.parse(params);
const url = new URL(request.url);
const query = SearchQuerySchema.parse(Object.fromEntries(url.searchParams));
const jobRun = await prisma.jobRun.findUnique({
where: {
id: runId,
},
select: {
id: true,
status: true,
startedAt: true,
updatedAt: true,
completedAt: true,
environmentId: true,
output: true,
tasks: {
select: {
id: true,
parentId: true,
displayKey: true,
status: true,
name: true,
icon: true,
startedAt: true,
completedAt: true,
params: query.taskdetails,
output: query.taskdetails,
},
where: {
parentId: query.subtasks ? undefined : null,
},
orderBy: {
id: "asc",
},
take: query.take + 1,
cursor: query.cursor
? {
id: query.cursor,
}
: undefined,
},
},
});
if (!jobRun) {
return cors(request, json({ message: "Run not found" }, { status: 404 }));
}
if (jobRun.environmentId !== authenticatedEnv.id) {
return cors(request, json({ message: "Run not found" }, { status: 404 }));
}
const selectedTasks = jobRun.tasks.slice(0, query.take);
const tasks = taskListToTree(selectedTasks, query.subtasks);
const nextTask = jobRun.tasks[query.take];
return cors(
request,
json({
id: jobRun.id,
status: jobRun.status,
startedAt: jobRun.startedAt,
updatedAt: jobRun.updatedAt,
completedAt: jobRun.completedAt,
tasks: tasks.map((task) => {
const { parentId, ...rest } = task;
return { ...rest };
}),
nextCursor: nextTask ? nextTask.id : undefined,
})
);
}
export async function action({ request, params }: ActionArgs) {
// Ensure this is a POST request
if (request.method.toUpperCase() !== "POST") {
@@ -0,0 +1,109 @@
import type { LoaderArgs } from "@remix-run/server-runtime";
import { json } from "@remix-run/server-runtime";
import { cors } from "remix-utils";
import { z } from "zod";
import { prisma } from "~/db.server";
import { authenticateApiRequest } from "~/services/apiAuth.server";
import { taskListToTree } from "~/utils/taskListToTree";
const ParamsSchema = z.object({
runId: z.string(),
});
const SearchQuerySchema = z.object({
cursor: z.string().optional(),
take: z.coerce.number().default(50),
subtasks: z.coerce.boolean().default(false),
taskdetails: z.coerce.boolean().default(false),
});
export async function loader({ request, params }: LoaderArgs) {
if (request.method.toUpperCase() === "OPTIONS") {
return cors(request, json({}));
}
const authenticatedEnv = await authenticateApiRequest(request, {
allowPublicKey: true,
});
if (!authenticatedEnv) {
return cors(
request,
json({ error: "Invalid or Missing API key" }, { status: 401 })
);
}
const { runId } = ParamsSchema.parse(params);
const url = new URL(request.url);
const query = SearchQuerySchema.parse(Object.fromEntries(url.searchParams));
const jobRun = await prisma.jobRun.findUnique({
where: {
id: runId,
},
select: {
id: true,
status: true,
startedAt: true,
updatedAt: true,
completedAt: true,
environmentId: true,
output: true,
tasks: {
select: {
id: true,
parentId: true,
displayKey: true,
status: true,
name: true,
icon: true,
startedAt: true,
completedAt: true,
params: query.taskdetails,
output: query.taskdetails,
},
where: {
parentId: query.subtasks ? undefined : null,
},
orderBy: {
id: "asc",
},
take: query.take + 1,
cursor: query.cursor
? {
id: query.cursor,
}
: undefined,
},
},
});
if (!jobRun) {
return cors(request, json({ message: "Run not found" }, { status: 404 }));
}
if (jobRun.environmentId !== authenticatedEnv.id) {
return cors(request, json({ message: "Run not found" }, { status: 404 }));
}
const selectedTasks = jobRun.tasks.slice(0, query.take);
const tasks = taskListToTree(selectedTasks, query.subtasks);
const nextTask = jobRun.tasks[query.take];
return cors(
request,
json({
id: jobRun.id,
status: jobRun.status,
startedAt: jobRun.startedAt,
updatedAt: jobRun.updatedAt,
completedAt: jobRun.completedAt,
tasks: tasks.map((task) => {
const { parentId, ...rest } = task;
return { ...rest };
}),
nextCursor: nextTask ? nextTask.id : undefined,
})
);
}
+6 -10
View File
@@ -20,16 +20,12 @@ export function useQueryRun(runId: string) {
return useQuery(
[`run-${runId}`],
async () => {
return await zodfetch(
GetRunSchema,
`${apiUrl}/api/v1/runs/${runId}/tasks`,
{
method: "GET",
headers: {
Authorization: `Bearer ${publicApiKey}`,
},
}
);
return await zodfetch(GetRunSchema, `${apiUrl}/api/v1/runs/${runId}`, {
method: "GET",
headers: {
Authorization: `Bearer ${publicApiKey}`,
},
});
},
{
refetchInterval: (data, query) => {
+1 -1
View File
@@ -375,7 +375,7 @@ export class ApiClient {
runId,
});
const url = new URL(`${this.#apiUrl}/api/v1/runs/${runId}/tasks`);
const url = new URL(`${this.#apiUrl}/api/v1/runs/${runId}`);
if (subtasks) {
url.searchParams.set("subtasks", String(subtasks));
}