diff --git a/components/dashboard/src/start/CreateWorkspace.tsx b/components/dashboard/src/start/CreateWorkspace.tsx index 4221d6bfa0ade2..9f964e8d65554f 100644 --- a/components/dashboard/src/start/CreateWorkspace.tsx +++ b/components/dashboard/src/start/CreateWorkspace.tsx @@ -461,40 +461,29 @@ interface RunningPrebuildViewProps { } function RunningPrebuildView(props: RunningPrebuildViewProps) { - const logsEmitter = new EventEmitter(); - let pollTimeout: NodeJS.Timeout | undefined; - let prebuildDoneTriggered: boolean = false; + const [logsEmitter] = useState(new EventEmitter()); useEffect(() => { - const checkIsPrebuildDone = async (): Promise => { - if (prebuildDoneTriggered) { - console.debug("prebuild done already triggered, doing nothing"); - return true; - } - - const done = await getGitpodService().server.isPrebuildDone(props.runningPrebuild.prebuildID); - if (done) { - // note: this treats "done" as "available" which is not equivalent. - // This works because the backend ignores prebuilds which are not "available", and happily starts a workspace as if there was no prebuild at all. - prebuildDoneTriggered = true; - props.onPrebuildSucceeded(); - return true; - } - return false; - }; - const pollIsPrebuildDone = async () => { - clearTimeout(pollTimeout!); - await checkIsPrebuildDone(); - pollTimeout = setTimeout(pollIsPrebuildDone, 10000); - }; - const disposables = watchHeadlessLogs( props.runningPrebuild.instanceID, (chunk) => logsEmitter.emit("logs", chunk), - checkIsPrebuildDone, + async () => false, ); + + disposables.push( + getGitpodService().registerClient({ + onInstanceUpdate: (update) => { + if (update.workspaceId !== props.runningPrebuild.workspaceID) { + return; + } + if (update.status.phase === "stopped") { + props.onPrebuildSucceeded(); + } + }, + }), + ); + return function cleanup() { - clearTimeout(pollTimeout!); disposables.dispose(); }; }, []); @@ -507,7 +496,6 @@ function RunningPrebuildView(props: RunningPrebuildViewProps) {