Skip to content

Commit 35a40f2

Browse files
authored
[server] Make PrebuildManager use WorkspaceService.startWorkspace (to remove code duplication) (#18653)
1 parent f5c2123 commit 35a40f2

File tree

3 files changed

+25
-30
lines changed

3 files changed

+25
-30
lines changed

components/server/src/prebuilds/prebuild-manager.ts

Lines changed: 10 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,6 @@ import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
2222
import { TraceContext } from "@gitpod/gitpod-protocol/lib/util/tracing";
2323
import { getCommitInfo, HostContextProvider } from "../auth/host-context-provider";
2424
import { ConfigProvider } from "../workspace/config-provider";
25-
import { WorkspaceStarter } from "../workspace/workspace-starter";
2625
import { Config } from "../config";
2726
import { ProjectsService } from "../projects/projects-service";
2827
import { secondsBefore } from "@gitpod/gitpod-protocol/lib/util/timeutil";
@@ -37,7 +36,6 @@ import { ErrorCodes, ApplicationError } from "@gitpod/gitpod-protocol/lib/messag
3736
import { UserAuthentication } from "../user/user-authentication";
3837
import { EntitlementService, MayStartWorkspaceResult } from "../billing/entitlement-service";
3938
import { WorkspaceService } from "../workspace/workspace-service";
40-
import { EnvVarService } from "../user/env-var-service";
4139

4240
export class WorkspaceRunningError extends Error {
4341
constructor(msg: string, public instance: WorkspaceInstance) {
@@ -57,7 +55,6 @@ export interface StartPrebuildParams {
5755
export class PrebuildManager {
5856
@inject(TracedWorkspaceDB) protected readonly workspaceDB: DBWithTracing<WorkspaceDB>;
5957
@inject(WorkspaceService) protected readonly workspaceService: WorkspaceService;
60-
@inject(WorkspaceStarter) protected readonly workspaceStarter: WorkspaceStarter;
6158
@inject(HostContextProvider) protected readonly hostContextProvider: HostContextProvider;
6259
@inject(ConfigProvider) protected readonly configProvider: ConfigProvider;
6360
@inject(Config) protected readonly config: Config;
@@ -66,7 +63,6 @@ export class PrebuildManager {
6663
@inject(UserAuthentication) protected readonly userService: UserAuthentication;
6764
@inject(TeamDB) protected readonly teamDB: TeamDB;
6865
@inject(EntitlementService) protected readonly entitlementService: EntitlementService;
69-
@inject(EnvVarService) private readonly envVarService: EnvVarService;
7066

7167
async abortPrebuildsForBranch(ctx: TraceContext, project: Project, user: User, branch: string): Promise<void> {
7268
const span = TraceContext.startSpan("abortPrebuildsForBranch", ctx);
@@ -230,13 +226,6 @@ export class PrebuildManager {
230226
context.normalizedContextURL!,
231227
);
232228

233-
const envVarsPromise = this.envVarService.resolveEnvVariables(
234-
workspace.ownerId,
235-
workspace.projectId,
236-
workspace.type,
237-
workspace.context,
238-
);
239-
240229
const prebuild = await this.workspaceDB.trace({ span }).findPrebuildByWorkspaceID(workspace.id)!;
241230
if (!prebuild) {
242231
throw new Error(`Failed to create a prebuild for: ${context.normalizedContextURL}`);
@@ -280,10 +269,15 @@ export class PrebuildManager {
280269
await this.workspaceDB.trace({ span }).storePrebuiltWorkspace(prebuild);
281270
} else {
282271
span.setTag("starting", true);
283-
const envVars = await envVarsPromise;
284-
await this.workspaceStarter.startWorkspace({ span }, workspace, user, project, envVars, {
285-
excludeFeatureFlags: ["full_workspace_backup"],
286-
});
272+
await this.workspaceService.startWorkspace(
273+
{ span },
274+
user,
275+
workspace.id,
276+
{
277+
excludeFeatureFlags: ["full_workspace_backup"],
278+
},
279+
false,
280+
);
287281
}
288282

289283
return { prebuildId: prebuild.id, wsid: workspace.id, done: false };
@@ -339,13 +333,7 @@ export class PrebuildManager {
339333
if (!prebuild) {
340334
throw new Error("No prebuild found for workspace " + workspaceId);
341335
}
342-
const envVars = await this.envVarService.resolveEnvVariables(
343-
workspace.ownerId,
344-
workspace.projectId,
345-
workspace.type,
346-
workspace.context,
347-
);
348-
await this.workspaceStarter.startWorkspace({ span }, workspace, user, project, envVars);
336+
await this.workspaceService.startWorkspace({ span }, user, workspaceId, {}, false);
349337
return { prebuildId: prebuild.id, wsid: workspace.id, done: false };
350338
} catch (err) {
351339
TraceContext.setError({ span }, err);

components/server/src/workspace/workspace-service.ts

Lines changed: 15 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ import {
4545
AdmissionLevel,
4646
ControlAdmissionRequest,
4747
} from "@gitpod/ws-manager/lib";
48-
import { WorkspaceStarter } from "./workspace-starter";
48+
import { WorkspaceStarter, StartWorkspaceOptions as StarterStartWorkspaceOptions } from "./workspace-starter";
4949
import { LogContext, log } from "@gitpod/gitpod-protocol/lib/util/logging";
5050
import { EntitlementService, MayStartWorkspaceResult } from "../billing/entitlement-service";
5151
import * as crypto from "crypto";
@@ -62,7 +62,7 @@ import { HeadlessLogEndpoint, HeadlessLogService } from "./headless-log-service"
6262
import { Deferred } from "@gitpod/gitpod-protocol/lib/util/deferred";
6363
import { OrganizationService } from "../orgs/organization-service";
6464

65-
export interface StartWorkspaceOptions extends GitpodServer.StartWorkspaceOptions {
65+
export interface StartWorkspaceOptions extends StarterStartWorkspaceOptions {
6666
/**
6767
* This field is used to guess the workspace location using the RegionService
6868
*/
@@ -437,10 +437,21 @@ export class WorkspaceService {
437437
user: User,
438438
workspaceId: string,
439439
options: StartWorkspaceOptions = {},
440+
restrictToRegular = true,
440441
): Promise<StartWorkspaceResult> {
441442
await this.auth.checkPermissionOnWorkspace(user.id, "start", workspaceId);
442443

443-
const workspace = await this.doGetWorkspace(user.id, workspaceId);
444+
const { workspace, latestInstance } = await this.getWorkspace(user.id, workspaceId);
445+
if (latestInstance) {
446+
if (latestInstance.status.phase !== "stopped") {
447+
// We already have a running workspace instance
448+
return {
449+
instanceID: latestInstance.id,
450+
workspaceURL: latestInstance.ideUrl,
451+
};
452+
}
453+
}
454+
444455
const mayStartPromise = this.mayStartWorkspace(
445456
ctx,
446457
user,
@@ -456,7 +467,7 @@ export class WorkspaceService {
456467
};
457468
}
458469

459-
if (workspace.type !== "regular") {
470+
if (restrictToRegular && workspace.type !== "regular") {
460471
throw new ApplicationError(ErrorCodes.BAD_REQUEST, "Cannot (re-)start irregular workspace.");
461472
}
462473

components/server/src/workspace/workspace-starter.ts

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -140,10 +140,8 @@ export async function getWorkspaceClassForInstance(
140140
ctx: TraceContext,
141141
workspace: Workspace,
142142
previousInstance: WorkspaceInstance | undefined,
143-
user: User,
144143
project: Project | undefined,
145144
workspaceClassOverride: string | undefined,
146-
entitlementService: EntitlementService,
147145
config: WorkspaceClassesConfig,
148146
): Promise<string> {
149147
const span = TraceContext.startSpan("getWorkspaceClassForInstance", ctx);
@@ -900,10 +898,8 @@ export class WorkspaceStarter {
900898
ctx,
901899
workspace,
902900
previousInstance,
903-
user,
904901
project,
905902
workspaceClassOverride,
906-
this.entitlementService,
907903
this.config.workspaceClasses,
908904
);
909905

0 commit comments

Comments
 (0)