Skip to content

Commit 326e995

Browse files
authored
perf(webapp): scope declarative schedule sync to the current environment (#4577)
## Summary Background worker registration runs on every deploy and every `trigger dev` file save. Its declarative-schedule reconcile loaded every declarative schedule for the whole project across all environments, then re-fetched the deletion candidates it already had in memory. For projects with many scheduled tasks or many environments, that meant reading tens of thousands of rows on each registration. This scopes the load to the environment being registered, drops the redundant re-fetch, and selects only the columns the reconcile needs. It also fixes the schedule-limit count (`getUsedSchedulesCount`), which joined `TaskSchedule` and `RuntimeEnvironment` without a project constraint and could scan those tables in full. Pushing `projectId` onto both joins gives it a project-scoped index path with the same result. Follow-up to [#4522](#4522), which batched the delete side of the same reconcile.
1 parent 26cdedd commit 326e995

3 files changed

Lines changed: 25 additions & 12 deletions

File tree

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
---
2+
area: webapp
3+
type: improvement
4+
---
5+
6+
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.

apps/webapp/app/v3/services/checkSchedule.server.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -131,12 +131,14 @@ export class CheckScheduleService extends BaseService {
131131
projectId,
132132
active: true,
133133
environment: {
134+
projectId,
134135
type: {
135136
not: "DEVELOPMENT",
136137
},
137138
archivedAt: null,
138139
},
139140
taskSchedule: {
141+
projectId,
140142
active: true,
141143
},
142144
},

apps/webapp/app/v3/services/createBackgroundWorker.server.ts

Lines changed: 17 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -655,9 +655,21 @@ export async function syncDeclarativeSchedules(
655655
where: {
656656
type: "DECLARATIVE",
657657
projectId: environment.projectId,
658+
instances: {
659+
some: {
660+
environmentId: environment.id,
661+
},
662+
},
658663
},
659-
include: {
660-
instances: true,
664+
select: {
665+
id: true,
666+
friendlyId: true,
667+
taskIdentifier: true,
668+
instances: {
669+
select: {
670+
environmentId: true,
671+
},
672+
},
661673
},
662674
});
663675

@@ -764,16 +776,9 @@ export async function syncDeclarativeSchedules(
764776

765777
//Delete instances for this environment
766778
//Delete schedules that have no instances left
767-
const potentiallyDeletableSchedules = await prisma.taskSchedule.findMany({
768-
where: {
769-
id: {
770-
in: boundedIn(Array.from(missingSchedules)),
771-
},
772-
},
773-
include: {
774-
instances: true,
775-
},
776-
});
779+
const potentiallyDeletableSchedules = existingDeclarativeSchedules.filter((schedule) =>
780+
missingSchedules.has(schedule.id)
781+
);
777782

778783
const scheduleIdsToDelete: string[] = [];
779784
const scheduleIdsToDetachFromEnvironment: string[] = [];

0 commit comments

Comments
 (0)