Skip to content

Commit

Permalink
[server] allow user custom global timeout
Browse files Browse the repository at this point in the history
  • Loading branch information
iQQBot committed Feb 24, 2023
1 parent 831189f commit 5737f15
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
17 changes: 13 additions & 4 deletions components/server/ee/src/billing/entitlement-service.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import {
WorkspaceInstance,
WorkspaceTimeoutDuration,
WORKSPACE_TIMEOUT_DEFAULT_LONG,
WORKSPACE_TIMEOUT_DEFAULT_SHORT,
} from "@gitpod/gitpod-protocol";
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
import { inject, injectable } from "inversify";
Expand Down Expand Up @@ -83,20 +84,28 @@ export class EntitlementServiceImpl implements EntitlementService {
}

async getDefaultWorkspaceTimeout(user: User, date: Date = new Date()): Promise<WorkspaceTimeoutDuration> {
let userWorkspaceTimeout: WorkspaceTimeoutDuration | undefined;
try {
userWorkspaceTimeout = WorkspaceTimeoutDuration.validate(user.additionalData?.workspaceTimeout || "");
} catch (e) {}
let defaultWorkspaceTimeout: WorkspaceTimeoutDuration = WORKSPACE_TIMEOUT_DEFAULT_SHORT;
try {
const billingMode = await this.billingModes.getBillingModeForUser(user, date);
switch (billingMode.mode) {
case "none":
return this.license.getDefaultWorkspaceTimeout(user, date);
defaultWorkspaceTimeout = await this.license.getDefaultWorkspaceTimeout(user, date);
case "chargebee":
return this.chargebee.getDefaultWorkspaceTimeout(user, date);
defaultWorkspaceTimeout = await this.chargebee.getDefaultWorkspaceTimeout(user, date);
case "usage-based":
return this.ubp.getDefaultWorkspaceTimeout(user, date);
defaultWorkspaceTimeout = await this.ubp.getDefaultWorkspaceTimeout(user, date);
}
} catch (err) {
log.error({ userId: user.id }, "EntitlementService error: getDefaultWorkspaceTimeout", err);
return WORKSPACE_TIMEOUT_DEFAULT_LONG;
defaultWorkspaceTimeout = WORKSPACE_TIMEOUT_DEFAULT_LONG;
}
return userWorkspaceTimeout && defaultWorkspaceTimeout == WORKSPACE_TIMEOUT_DEFAULT_LONG
? userWorkspaceTimeout
: defaultWorkspaceTimeout;
}

async userGetsMoreResources(user: User, date: Date = new Date()): Promise<boolean> {
Expand Down
4 changes: 4 additions & 0 deletions components/server/src/workspace/workspace-starter.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1476,6 +1476,7 @@ export class WorkspaceStarter {
lastValidWorkspaceInstanceId,
);
const userTimeoutPromise = this.entitlementService.getDefaultWorkspaceTimeout(user, new Date());
const allowSetTimeout = this.entitlementService.maySetTimeout(user, new Date());

let featureFlags = instance.configuration!.featureFlags || [];

Expand Down Expand Up @@ -1507,6 +1508,9 @@ export class WorkspaceStarter {

if (workspace.type === "regular") {
spec.setTimeout(await userTimeoutPromise);
if ((await allowSetTimeout) && user.additionalData?.disabledClosedTimeout === true) {
spec.setClosedTimeout("0");
}
}
spec.setAdmission(admissionLevel);
const sshKeys = await this.userDB.trace(traceCtx).getSSHPublicKeys(user.id);
Expand Down

0 comments on commit 5737f15

Please sign in to comment.