Skip to content

Commit 1267e48

Browse files
committed
refactor(webapp): resolve regions UI default via canonical resolver
RegionsPresenter now resolves the effective default through getDefaultWorkerGroupForProject (existence-checked env -> project -> global), so the UI default always matches where runs route and never points at a deleted region. Removes the id-only resolveEffectiveDefaultWorkerGroupId helper (and its test) now that all four sites share one resolver.
1 parent 4b7f66d commit 1267e48

4 files changed

Lines changed: 25 additions & 121 deletions

File tree

apps/webapp/app/presenters/v3/RegionsPresenter.server.ts

Lines changed: 22 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,8 @@
11
import { type WorkloadType } from "@trigger.dev/database";
22
import { type Project } from "~/models/project.server";
33
import { type User } from "~/models/user.server";
4-
import { FEATURE_FLAG } from "~/v3/featureFlags";
5-
import { makeFlag } from "~/v3/featureFlags.server";
6-
import {
7-
defaultVisibilityFilter,
8-
resolveComputeAccess,
9-
resolveEffectiveDefaultWorkerGroupId,
10-
} from "~/v3/regionAccess.server";
4+
import { defaultVisibilityFilter, resolveComputeAccess } from "~/v3/regionAccess.server";
5+
import { WorkerGroupService } from "~/v3/services/worker/workerGroupService.server";
116
import { BasePresenter } from "./basePresenter.server";
127
import { getCurrentPlan } from "~/services/platform.v3.server";
138

@@ -40,7 +35,6 @@ export class RegionsPresenter extends BasePresenter {
4035
select: {
4136
id: true,
4237
organizationId: true,
43-
defaultWorkerGroupId: true,
4438
allowedWorkerQueues: true,
4539
organization: {
4640
select: { featureFlags: true },
@@ -62,28 +56,21 @@ export class RegionsPresenter extends BasePresenter {
6256
throw new Error("Project not found");
6357
}
6458

65-
const getFlag = makeFlag(this._replica);
66-
const defaultWorkerInstanceGroupId = await getFlag({
67-
key: FEATURE_FLAG.defaultWorkerInstanceGroupId,
68-
});
69-
70-
if (!defaultWorkerInstanceGroupId) {
71-
throw new Error("Default worker instance group not found");
72-
}
73-
7459
const environment = environmentId
7560
? await this._replica.runtimeEnvironment.findFirst({
7661
select: { defaultWorkerGroupId: true },
7762
where: { id: environmentId, projectId: project.id, archivedAt: null },
7863
})
7964
: null;
8065

81-
// env default -> project default -> global default
82-
const effectiveDefaultId = resolveEffectiveDefaultWorkerGroupId({
66+
// Resolve via the same path the trigger uses (env -> project -> global, each
67+
// existence-checked) so the UI default always matches where runs route and can
68+
// never point at a deleted region.
69+
const defaultWorkerGroup = await new WorkerGroupService().getDefaultWorkerGroupForProject({
70+
projectId: project.id,
8371
environmentDefaultWorkerGroupId: environment?.defaultWorkerGroupId,
84-
projectDefaultWorkerGroupId: project.defaultWorkerGroupId,
85-
globalDefaultWorkerGroupId: defaultWorkerInstanceGroupId,
8672
});
73+
const effectiveDefaultId = defaultWorkerGroup?.id;
8774

8875
const hasComputeAccess = await resolveComputeAccess(
8976
this._replica,
@@ -128,38 +115,21 @@ export class RegionsPresenter extends BasePresenter {
128115
workloadType: region.workloadType,
129116
}));
130117

131-
// The effective default may not be in the visible list (e.g. a hidden
132-
// region set as the env/project default) — fetch and include it.
133-
if (effectiveDefaultId && !regions.some((region) => region.id === effectiveDefaultId)) {
134-
const defaultWorkerGroup = await this._replica.workerInstanceGroup.findFirst({
135-
select: {
136-
id: true,
137-
name: true,
138-
masterQueue: true,
139-
description: true,
140-
cloudProvider: true,
141-
location: true,
142-
staticIPs: true,
143-
hidden: true,
144-
workloadType: true,
145-
},
146-
where: { id: effectiveDefaultId },
118+
// The default may not be in the visible list (e.g. a hidden region set as the
119+
// env/project default) — include the already-resolved group so it still shows.
120+
if (defaultWorkerGroup && !regions.some((region) => region.id === defaultWorkerGroup.id)) {
121+
regions.push({
122+
id: defaultWorkerGroup.id,
123+
name: defaultWorkerGroup.name,
124+
masterQueue: defaultWorkerGroup.masterQueue,
125+
description: defaultWorkerGroup.description ?? undefined,
126+
cloudProvider: defaultWorkerGroup.cloudProvider ?? undefined,
127+
location: defaultWorkerGroup.location ?? undefined,
128+
staticIPs: defaultWorkerGroup.staticIPs ?? undefined,
129+
isDefault: true,
130+
isHidden: defaultWorkerGroup.hidden,
131+
workloadType: defaultWorkerGroup.workloadType,
147132
});
148-
149-
if (defaultWorkerGroup) {
150-
regions.push({
151-
id: defaultWorkerGroup.id,
152-
name: defaultWorkerGroup.name,
153-
masterQueue: defaultWorkerGroup.masterQueue,
154-
description: defaultWorkerGroup.description ?? undefined,
155-
cloudProvider: defaultWorkerGroup.cloudProvider ?? undefined,
156-
location: defaultWorkerGroup.location ?? undefined,
157-
staticIPs: defaultWorkerGroup.staticIPs ?? undefined,
158-
isDefault: true,
159-
isHidden: defaultWorkerGroup.hidden,
160-
workloadType: defaultWorkerGroup.workloadType,
161-
});
162-
}
163133
}
164134

165135
// Default first

apps/webapp/app/v3/regionAccess.server.ts

Lines changed: 0 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -33,29 +33,6 @@ export function defaultVisibilityFilter(
3333
return { hidden: false, workloadType: { not: "MICROVM" } };
3434
}
3535

36-
/**
37-
* Canonical fallback chain for a run's default region:
38-
* environment default -> project default -> global default.
39-
* Both the trigger path and the regions UI must use this order so the
40-
* displayed default matches what actually runs.
41-
*/
42-
export function resolveEffectiveDefaultWorkerGroupId({
43-
environmentDefaultWorkerGroupId,
44-
projectDefaultWorkerGroupId,
45-
globalDefaultWorkerGroupId,
46-
}: {
47-
environmentDefaultWorkerGroupId?: string | null;
48-
projectDefaultWorkerGroupId?: string | null;
49-
globalDefaultWorkerGroupId?: string | null;
50-
}): string | undefined {
51-
return (
52-
environmentDefaultWorkerGroupId ??
53-
projectDefaultWorkerGroupId ??
54-
globalDefaultWorkerGroupId ??
55-
undefined
56-
);
57-
}
58-
5936
/**
6037
* Whether a region is accessible given compute access.
6138
* MICROVM regions require compute access; all other types pass through.

apps/webapp/app/v3/services/worker/workerGroupService.server.ts

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -284,8 +284,9 @@ export class WorkerGroupService extends WithRunEngine {
284284
return workerGroup;
285285
}
286286

287-
// Resolution order must match resolveEffectiveDefaultWorkerGroupId:
288-
// environment default -> project default -> global default.
287+
// Canonical default-region resolution (reused by the regions UI, workers API
288+
// and compute templates): environment default -> project default -> global
289+
// default, each existence-checked so a deleted region falls through.
289290
if (environmentDefaultWorkerGroupId) {
290291
const envWorkerGroup = await this._prisma.workerInstanceGroup.findFirst({
291292
where: { id: environmentDefaultWorkerGroupId },

apps/webapp/test/regionAccess.test.ts

Lines changed: 0 additions & 44 deletions
This file was deleted.

0 commit comments

Comments
 (0)