From 510c8f29a89c56b7a4c29bcfe93d624b7bc90579 Mon Sep 17 00:00:00 2001 From: Jan Keromnes Date: Tue, 13 Sep 2022 11:42:05 +0000 Subject: [PATCH] [ws-manager-bridge] When de-registering a workspace cluster, mark any leftover running instances as stopped/failed --- components/ws-manager-bridge/src/bridge.ts | 30 ++++++++++++++++++++++ 1 file changed, 30 insertions(+) diff --git a/components/ws-manager-bridge/src/bridge.ts b/components/ws-manager-bridge/src/bridge.ts index 5546b2a1678f58..e3ae3cfe1c994b 100644 --- a/components/ws-manager-bridge/src/bridge.ts +++ b/components/ws-manager-bridge/src/bridge.ts @@ -120,6 +120,7 @@ export class WorkspaceManagerBridge implements Disposable { } public stop() { + this.markAllRunningWorkspaceInstancesAsStopped(); this.dispose(); } @@ -651,6 +652,35 @@ export class WorkspaceManagerBridge implements Disposable { } } + protected async markAllRunningWorkspaceInstancesAsStopped(): Promise { + const span = TraceContext.startSpan("markAllRunningWorkspaceInstancesAsStopped"); + try { + const now = new Date(); + const runningInstances = await this.workspaceDB + .trace({ span }) + .findRunningInstancesWithWorkspaces(this.cluster.name, undefined, true); + await Promise.all( + runningInstances.map(async (info) => { + const logContext: LogContext = { + userId: info.workspace.ownerId, + workspaceId: info.workspace.id, + instanceId: info.latestInstance.id, + }; + log.error(logContext, "Marking still-running instance as stopped in database.", { + creationTime: info.workspace.creationTime, + currentPhase: info.latestInstance.status.phase, + }); + await this.markWorkspaceInstanceAsStopped({ span }, info, now); + }), + ); + } catch (err) { + TraceContext.setError({ span }, err); + throw err; + } finally { + span.finish(); + } + } + public dispose() { this.disposables.dispose(); }