Skip to content

Fix for Schedules list page slow loading #1992

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 2 commits into from
Apr 29, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 1 addition & 5 deletions apps/webapp/app/components/runs/v3/ScheduleFilters.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -24,10 +24,6 @@ export const ScheduleListFilters = z.object({
.string()
.optional()
.transform((value) => (value ? value.split(",") : undefined)),
environments: z
.string()
.optional()
.transform((value) => (value ? value.split(",") : undefined)),
type: z.union([z.literal("declarative"), z.literal("imperative")]).optional(),
search: z.string().optional(),
});
Expand All @@ -44,7 +40,7 @@ export function ScheduleFilters({ possibleTasks }: ScheduleFiltersProps) {
const navigate = useNavigate();
const location = useOptimisticLocation();
const searchParams = new URLSearchParams(location.search);
const { environments, tasks, page, search, type } = ScheduleListFilters.parse(
const { tasks, page, search, type } = ScheduleListFilters.parse(
Object.fromEntries(searchParams.entries())
);

Expand Down
39 changes: 27 additions & 12 deletions apps/webapp/app/presenters/v3/EditSchedulePresenter.server.ts
Original file line number Diff line number Diff line change
@@ -1,13 +1,16 @@
import { RuntimeEnvironmentType } from "@trigger.dev/database";
import { PrismaClient, prisma } from "~/db.server";
import { displayableEnvironment } from "~/models/runtimeEnvironment.server";
import { type RuntimeEnvironmentType } from "@trigger.dev/database";
import { type PrismaClient, prisma } from "~/db.server";
import { displayableEnvironment, findEnvironmentBySlug } from "~/models/runtimeEnvironment.server";
import { logger } from "~/services/logger.server";
import { filterOrphanedEnvironments } from "~/utils/environmentSort";
import { getTimezones } from "~/utils/timezones.server";
import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server";
import { ServiceValidationError } from "~/v3/services/baseService.server";

type EditScheduleOptions = {
userId: string;
projectSlug: string;
environmentSlug: string;
friendlyId?: string;
};

Expand All @@ -26,7 +29,7 @@ export class EditSchedulePresenter {
this.#prismaClient = prismaClient;
}

public async call({ userId, projectSlug, friendlyId }: EditScheduleOptions) {
public async call({ userId, projectSlug, environmentSlug, friendlyId }: EditScheduleOptions) {
// Find the project scoped to the organization
const project = await this.#prismaClient.project.findFirstOrThrow({
select: {
Expand Down Expand Up @@ -62,13 +65,25 @@ export class EditSchedulePresenter {
},
});

const possibleTasks = await this.#prismaClient.backgroundWorkerTask.findMany({
distinct: ["slug"],
where: {
projectId: project.id,
triggerSource: "SCHEDULED",
},
});
const environment = await findEnvironmentBySlug(project.id, environmentSlug, userId);
if (!environment) {
throw new ServiceValidationError("No matching environment for project", 404);
}

//get the latest BackgroundWorker
const latestWorker = await findCurrentWorkerFromEnvironment(environment, this.#prismaClient);

//get all possible scheduled tasks
const possibleTasks = latestWorker
? await this.#prismaClient.backgroundWorkerTask.findMany({
where: {
workerId: latestWorker.id,
projectId: project.id,
runtimeEnvironmentId: environment.id,
triggerSource: "SCHEDULED",
},
})
: [];

const possibleEnvironments = filterOrphanedEnvironments(project.environments).map(
(environment) => {
Expand All @@ -77,7 +92,7 @@ export class EditSchedulePresenter {
);

return {
possibleTasks: possibleTasks.map((task) => task.slug),
possibleTasks: possibleTasks.map((task) => task.slug).sort(),
possibleEnvironments,
possibleTimezones: getTimezones(),
schedule: await this.#getExistingSchedule(friendlyId, possibleEnvironments),
Expand Down
58 changes: 41 additions & 17 deletions apps/webapp/app/presenters/v3/ScheduleListPresenter.server.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,9 +6,12 @@ import { getLimit } from "~/services/platform.v3.server";
import { CheckScheduleService } from "~/v3/services/checkSchedule.server";
import { calculateNextScheduledTimestamp } from "~/v3/utils/calculateNextSchedule.server";
import { BasePresenter } from "./basePresenter.server";
import { findCurrentWorkerFromEnvironment } from "~/v3/models/workerDeployment.server";
import { ServiceValidationError } from "~/v3/services/baseService.server";

type ScheduleListOptions = {
projectId: string;
environmentId: string;
userId?: string;
pageSize?: number;
} & ScheduleListFilters;
Expand Down Expand Up @@ -42,8 +45,8 @@ export class ScheduleListPresenter extends BasePresenter {
public async call({
userId,
projectId,
environmentId,
tasks,
environments,
search,
page,
type,
Expand Down Expand Up @@ -84,16 +87,45 @@ export class ScheduleListPresenter extends BasePresenter {
},
});

const environment = project.environments.find((env) => env.id === environmentId);
if (!environment) {
throw new ServiceValidationError("No matching environment for project", 404);
}

const schedulesCount = await CheckScheduleService.getUsedSchedulesCount({
prisma: this._replica,
environments: project.environments,
});

const limit = await getLimit(project.organizationId, "schedules", 100_000_000);

//get the latest BackgroundWorker
const latestWorker = await findCurrentWorkerFromEnvironment(environment, this._replica);
if (!latestWorker) {
return {
currentPage: 1,
totalPages: 1,
totalCount: 0,
schedules: [],
possibleTasks: [],
hasFilters,
limits: {
used: schedulesCount,
limit,
},
filters: {
tasks,
search,
},
};
}

//get all possible scheduled tasks
const possibleTasks = await this._replica.backgroundWorkerTask.findMany({
distinct: ["slug"],
where: {
workerId: latestWorker.id,
projectId: project.id,
runtimeEnvironmentId: environmentId,
triggerSource: "SCHEDULED",
},
});
Expand All @@ -107,7 +139,7 @@ export class ScheduleListPresenter extends BasePresenter {
taskIdentifier: tasks ? { in: tasks } : undefined,
instances: {
some: {
environmentId: environments ? { in: environments } : undefined,
environmentId,
},
},
type: filterType,
Expand Down Expand Up @@ -168,13 +200,11 @@ export class ScheduleListPresenter extends BasePresenter {
where: {
projectId: project.id,
taskIdentifier: tasks ? { in: tasks } : undefined,
instances: environments
? {
some: {
environmentId: environments ? { in: environments } : undefined,
},
}
: undefined,
instances: {
some: {
environmentId,
},
},
type: filterType,
AND: search
? {
Expand Down Expand Up @@ -242,25 +272,19 @@ export class ScheduleListPresenter extends BasePresenter {
};
});

const limit = await getLimit(project.organizationId, "schedules", 100_000_000);

return {
currentPage: page,
totalPages: Math.ceil(totalCount / pageSize),
totalCount: totalCount,
schedules,
possibleTasks: possibleTasks.map((task) => task.slug),
possibleEnvironments: project.environments.map((environment) => {
return displayableEnvironment(environment, userId);
}),
possibleTasks: possibleTasks.map((task) => task.slug).sort((a, b) => a.localeCompare(b)),
hasFilters,
limits: {
used: schedulesCount,
limit,
},
filters: {
tasks,
environments,
search,
},
};
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const result = await presenter.call({
userId,
projectSlug: projectParam,
environmentSlug: envParam,
friendlyId: scheduleParam,
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,18 +2,19 @@ import { type LoaderFunctionArgs } from "@remix-run/server-runtime";
import { typedjson, useTypedLoaderData } from "remix-typedjson";
import { EditSchedulePresenter } from "~/presenters/v3/EditSchedulePresenter.server";
import { requireUserId } from "~/services/session.server";
import { ProjectParamSchema } from "~/utils/pathBuilder";
import { EnvironmentParamSchema } from "~/utils/pathBuilder";
import { humanToCronSupported } from "~/v3/humanToCron.server";
import { UpsertScheduleForm } from "../resources.orgs.$organizationSlug.projects.$projectParam.env.$envParam.schedules.new/route";

export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const userId = await requireUserId(request);
const { projectParam, organizationSlug } = ProjectParamSchema.parse(params);
const { projectParam, envParam, organizationSlug } = EnvironmentParamSchema.parse(params);

const presenter = new EditSchedulePresenter();
const result = await presenter.call({
userId,
projectSlug: projectParam,
environmentSlug: envParam,
});

return typedjson({ ...result, showGenerateField: humanToCronSupported });
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -93,28 +93,21 @@ export const loader = async ({ request, params }: LoaderFunctionArgs) => {
const url = new URL(request.url);
const s = Object.fromEntries(url.searchParams.entries());
const filters = ScheduleListFilters.parse(s);
filters.environments = [environment.id];

const presenter = new ScheduleListPresenter();
const list = await presenter.call({
userId,
projectId: project.id,
environmentId: environment.id,
...filters,
});

return typedjson(list);
};

export default function Page() {
const {
schedules,
possibleTasks,
possibleEnvironments,
hasFilters,
limits,
currentPage,
totalPages,
} = useTypedLoaderData<typeof loader>();
const { schedules, possibleTasks, hasFilters, limits, currentPage, totalPages } =
useTypedLoaderData<typeof loader>();
const location = useLocation();
const organization = useOrganization();
const project = useProject();
Expand Down
2 changes: 1 addition & 1 deletion apps/webapp/app/routes/api.v1.schedules.ts
Original file line number Diff line number Diff line change
Expand Up @@ -100,9 +100,9 @@ export async function loader({ request }: LoaderFunctionArgs) {

const result = await presenter.call({
projectId: authenticationResult.environment.projectId,
environmentId: authenticationResult.environment.id,
page: params.data.page ?? 1,
pageSize: params.data.perPage,
environments: [authenticationResult.environment.id],
});

return {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
-- CreateIndex
CREATE INDEX CONCURRENTLY IF NOT EXISTS "BackgroundWorker_runtimeEnvironmentId_createdAt_idx" ON "BackgroundWorker" ("runtimeEnvironmentId", "createdAt" DESC);
2 changes: 2 additions & 0 deletions internal-packages/database/prisma/schema.prisma
Original file line number Diff line number Diff line change
Expand Up @@ -1627,6 +1627,8 @@ model BackgroundWorker {

@@unique([projectId, runtimeEnvironmentId, version])
@@index([runtimeEnvironmentId])
// Get the latest worker for a given environment
@@index([runtimeEnvironmentId, createdAt(sort: Desc)])
}

model BackgroundWorkerFile {
Expand Down
Loading