diff --git a/.vscode/launch.json b/.vscode/launch.json index 4b97af00c9..120bfe5e16 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -95,6 +95,7 @@ "ACCOUNT_PORT": "3000", "FRONT_URL": "http://localhost:8080", "SES_URL": "", + // "WS_LIVENESS_DAYS": "1", "MINIO_ACCESS_KEY": "minioadmin", "MINIO_SECRET_KEY": "minioadmin", "MINIO_ENDPOINT": "localhost", diff --git a/dev/docker-compose.yaml b/dev/docker-compose.yaml index d000ac9d5a..4dbab23640 100644 --- a/dev/docker-compose.yaml +++ b/dev/docker-compose.yaml @@ -83,6 +83,7 @@ services: - RESERVED_DB_NAMES=telegram,gmail,github - MODEL_ENABLED=* - LAST_NAME_FIRST=true + # - WS_LIVENESS_DAYS=1 - ACCOUNTS_URL=http://host.docker.internal:3000 - BRANDING_PATH=/var/cfg/branding.json # - DISABLE_SIGNUP=true diff --git a/server/account-service/src/index.ts b/server/account-service/src/index.ts index cc95a5142d..f3e92f4922 100644 --- a/server/account-service/src/index.ts +++ b/server/account-service/src/index.ts @@ -65,6 +65,17 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap const productName = process.env.PRODUCT_NAME const lang = process.env.LANGUAGE ?? 'en' + const wsLivenessDaysRaw = process.env.WS_LIVENESS_DAYS + let wsLivenessDays: number | undefined + + if (wsLivenessDaysRaw !== undefined) { + try { + wsLivenessDays = parseInt(wsLivenessDaysRaw) + } catch (err: any) { + // DO NOTHING + } + } + setMetadata(account.metadata.Transactors, transactorUri) setMetadata(platform.metadata.locale, lang) setMetadata(account.metadata.ProductName, productName) @@ -72,6 +83,7 @@ export function serveAccount (measureCtx: MeasureContext, brandings: BrandingMap setMetadata(account.metadata.OtpRetryDelaySec, parseInt(process.env.OTP_RETRY_DELAY ?? '60')) setMetadata(account.metadata.SES_URL, ses) setMetadata(account.metadata.FrontURL, frontURL) + setMetadata(account.metadata.WsLivenessDays, wsLivenessDays) setMetadata(serverToken.metadata.Secret, serverSecret) diff --git a/server/account/src/collections/mongo.ts b/server/account/src/collections/mongo.ts index 1b4d53261f..a3ace061d1 100644 --- a/server/account/src/collections/mongo.ts +++ b/server/account/src/collections/mongo.ts @@ -212,7 +212,8 @@ export class WorkspaceMongoDbCollection extends MongoDbCollection imp region: string, version: Data, operation: WorkspaceOperation, - processingTimeoutMs: number + processingTimeoutMs: number, + wsLivenessMs?: number ): Promise { const pendingCreationQuery: Filter['$or'] = [{ mode: { $in: ['pending-creation', 'creating'] } }] @@ -233,9 +234,13 @@ export class WorkspaceMongoDbCollection extends MongoDbCollection imp $or: [{ mode: 'active' }, { mode: { $exists: false } }] }, versionQuery, - { - lastVisit: { $gt: Date.now() - 24 * 60 * 60 * 1000 } - } + ...(wsLivenessMs !== undefined + ? [ + { + lastVisit: { $gt: Date.now() - wsLivenessMs } + } + ] + : []) ] }, { diff --git a/server/account/src/collections/postgres.ts b/server/account/src/collections/postgres.ts index fbc406e59b..09993fc848 100644 --- a/server/account/src/collections/postgres.ts +++ b/server/account/src/collections/postgres.ts @@ -358,7 +358,8 @@ export class WorkspacePostgresDbCollection extends PostgresDbCollection, operation: WorkspaceOperation, - processingTimeoutMs: number + processingTimeoutMs: number, + wsLivenessMs?: number ): Promise { const sqlChunks: string[] = [`SELECT * FROM ${this.name}`] const whereChunks: string[] = [] @@ -367,7 +368,7 @@ export class WorkspacePostgresDbCollection extends PostgresDbCollection $4) OR ((disabled = FALSE OR disabled IS NULL) AND mode = 'upgrading'))` + const pendingUpgradeSql = `(((disabled = FALSE OR disabled IS NULL) AND (mode = 'active' OR mode IS NULL) AND ${versionSql} ${wsLivenessMs !== undefined ? 'AND "lastVisit" > $4' : ''}) OR ((disabled = FALSE OR disabled IS NULL) AND mode = 'upgrading'))` const operationSql = operation === 'create' ? pendingCreationSql @@ -375,7 +376,11 @@ export class WorkspacePostgresDbCollection extends PostgresDbCollection, Transactors: '' as Metadata, OtpTimeToLiveSec: '' as Metadata, - OtpRetryDelaySec: '' as Metadata + OtpRetryDelaySec: '' as Metadata, + WsLivenessDays: '' as Metadata }, string: { ConfirmationText: '' as IntlString, diff --git a/server/account/src/types.ts b/server/account/src/types.ts index 9338ec0763..dd3a616c23 100644 --- a/server/account/src/types.ts +++ b/server/account/src/types.ts @@ -195,6 +195,7 @@ export interface WorkspaceDbCollection extends DbCollection { region: string, version: Data, operation: WorkspaceOperation, - processingTimeoutMs: number + processingTimeoutMs: number, + wsLivenessMs?: number ) => Promise }