From cc96f0b36806083c438ca59a6b5e0c6e29e58b4f Mon Sep 17 00:00:00 2001 From: Gero Posmyk-Leinemann Date: Thu, 20 Oct 2022 15:51:19 +0000 Subject: [PATCH] [server] Restart PVC workspace from latest valid backup --- .../server/src/workspace/workspace-starter.ts | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/components/server/src/workspace/workspace-starter.ts b/components/server/src/workspace/workspace-starter.ts index 5ef76ec160892e..dcb45a20b896e3 100644 --- a/components/server/src/workspace/workspace-starter.ts +++ b/components/server/src/workspace/workspace-starter.ts @@ -336,14 +336,14 @@ export class WorkspaceStarter { // check if there has been an instance before, i.e. if this is a restart const pastInstances = await this.workspaceDb.trace({ span }).findInstances(workspace.id); - const hasValidBackup = pastInstances.some( - (i) => !!i.status && !!i.status.conditions && !i.status.conditions.failed, - ); let lastValidWorkspaceInstance: WorkspaceInstance | undefined; - if (hasValidBackup) { - lastValidWorkspaceInstance = pastInstances.reduce((previousValue, currentValue) => - currentValue.creationTime > previousValue.creationTime ? currentValue : previousValue, - ); + // Sorted from latest to oldest + for (const i of pastInstances.sort((a, b) => (a.creationTime > b.creationTime ? -1 : 1))) { + // We're trying to figure out whether there was a successful backup or not, and if yes for which instance + if (!!i.status.conditions && !i.status.conditions.failed) { + lastValidWorkspaceInstance = i; + break; + } } const ideConfig = await this.ideService.getIDEConfig();