Skip to content

Commit 1abd308

Browse files
committed
Deduplicate instance counting logic
1 parent 748421d commit 1abd308

File tree

3 files changed

+9
-4
lines changed

3 files changed

+9
-4
lines changed

components/server/leeway.Dockerfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@
22
# Licensed under the GNU Affero General Public License (AGPL).
33
# See License.AGPL.txt in the project root for license information.
44

5-
FROM node:18.17.1-slim as builder
5+
FROM node:18.17.1-slim AS builder
66

77
# Install Python, make, gcc and g++ for node-gyp
88
RUN apt-get update && \

components/server/src/billing/entitlement-service-ubp.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ export class EntitlementServiceUBP implements EntitlementService {
6161
? Math.min(planAllowance, maxParallelRunningWorkspaces)
6262
: planAllowance;
6363

64-
const current = (await runningInstances).filter((i) => i.status.phase !== "preparing").length;
64+
const current = await getRunningInstancesCount(runningInstances);
6565
if (current >= max) {
6666
return {
6767
current,
@@ -143,3 +143,8 @@ export class EntitlementServiceUBP implements EntitlementService {
143143
return hasPaidPlan ? "paid" : "free";
144144
}
145145
}
146+
147+
export const getRunningInstancesCount = async (instancesPromise: Promise<WorkspaceInstance[]>): Promise<number> => {
148+
const instances = await instancesPromise;
149+
return instances.filter((i) => i.status.phase !== "preparing").length;
150+
};

components/server/src/billing/entitlement-service.ts

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import { AttributionId } from "@gitpod/gitpod-protocol/lib/attribution";
1717
import { BillingTier } from "@gitpod/gitpod-protocol/lib/protocol";
1818
import { inject, injectable } from "inversify";
1919
import { BillingModes } from "./billing-mode";
20-
import { EntitlementServiceUBP, LazyOrganizationService } from "./entitlement-service-ubp";
20+
import { EntitlementServiceUBP, getRunningInstancesCount, LazyOrganizationService } from "./entitlement-service-ubp";
2121
import { log } from "@gitpod/gitpod-protocol/lib/util/logging";
2222

2323
export interface MayStartWorkspaceResult {
@@ -123,7 +123,7 @@ export class EntitlementServiceImpl implements EntitlementService {
123123
// we use || here because the default value is 0 and we want to use the default limit if the organization limit is not set
124124
const maxParallelRunningWorkspaces =
125125
organizationSettings.maxParallelRunningWorkspaces || MAX_PARALLEL_WORKSPACES_PAID;
126-
const current = (await runningInstances).filter((i) => i.status.phase !== "preparing").length;
126+
const current = await getRunningInstancesCount(runningInstances);
127127
if (current >= maxParallelRunningWorkspaces) {
128128
return {
129129
hitParallelWorkspaceLimit: {

0 commit comments

Comments
 (0)