From 53c394fd73da04a5e08b78b88f53f11ac091710d Mon Sep 17 00:00:00 2001 From: Eric Allam Date: Tue, 11 Aug 2026 23:18:49 +0100 Subject: [PATCH] perf(webapp): scope declarative schedule sync to the current environment syncDeclarativeSchedules loaded every declarative schedule for the whole project across all environments, then re-fetched deletion candidates it already had in memory. Scope the load to the current environment, drop the redundant re-fetch, and select only the columns the reconcile uses. Also push projectId onto the schedule-limit count joins so it uses a project-scoped index instead of scanning the full TaskSchedule and RuntimeEnvironment tables. Follow-up to #4522, which batched the delete side. --- .../scope-declarative-schedule-sync.md | 6 ++++ .../app/v3/services/checkSchedule.server.ts | 2 ++ .../services/createBackgroundWorker.server.ts | 29 +++++++++++-------- 3 files changed, 25 insertions(+), 12 deletions(-) create mode 100644 .server-changes/scope-declarative-schedule-sync.md diff --git a/.server-changes/scope-declarative-schedule-sync.md b/.server-changes/scope-declarative-schedule-sync.md new file mode 100644 index 00000000000..feb4050d3ee --- /dev/null +++ b/.server-changes/scope-declarative-schedule-sync.md @@ -0,0 +1,6 @@ +--- +area: webapp +type: improvement +--- + +Make background worker registration cheaper for projects with many scheduled tasks by scoping declarative schedule reconciliation to the current environment and dropping redundant schedule lookups. diff --git a/apps/webapp/app/v3/services/checkSchedule.server.ts b/apps/webapp/app/v3/services/checkSchedule.server.ts index 0115c742069..bc28ee9c357 100644 --- a/apps/webapp/app/v3/services/checkSchedule.server.ts +++ b/apps/webapp/app/v3/services/checkSchedule.server.ts @@ -131,12 +131,14 @@ export class CheckScheduleService extends BaseService { projectId, active: true, environment: { + projectId, type: { not: "DEVELOPMENT", }, archivedAt: null, }, taskSchedule: { + projectId, active: true, }, }, diff --git a/apps/webapp/app/v3/services/createBackgroundWorker.server.ts b/apps/webapp/app/v3/services/createBackgroundWorker.server.ts index 25913230b26..dc5c79129ac 100644 --- a/apps/webapp/app/v3/services/createBackgroundWorker.server.ts +++ b/apps/webapp/app/v3/services/createBackgroundWorker.server.ts @@ -655,9 +655,21 @@ export async function syncDeclarativeSchedules( where: { type: "DECLARATIVE", projectId: environment.projectId, + instances: { + some: { + environmentId: environment.id, + }, + }, }, - include: { - instances: true, + select: { + id: true, + friendlyId: true, + taskIdentifier: true, + instances: { + select: { + environmentId: true, + }, + }, }, }); @@ -764,16 +776,9 @@ export async function syncDeclarativeSchedules( //Delete instances for this environment //Delete schedules that have no instances left - const potentiallyDeletableSchedules = await prisma.taskSchedule.findMany({ - where: { - id: { - in: boundedIn(Array.from(missingSchedules)), - }, - }, - include: { - instances: true, - }, - }); + const potentiallyDeletableSchedules = existingDeclarativeSchedules.filter((schedule) => + missingSchedules.has(schedule.id) + ); const scheduleIdsToDelete: string[] = []; const scheduleIdsToDetachFromEnvironment: string[] = [];