Skip to content

Commit

Permalink
get rid of isPrebuildDone polling
Browse files Browse the repository at this point in the history
  • Loading branch information
AlexTugarev committed Jun 8, 2022
1 parent 2e06c1e commit 1887fec
Showing 1 changed file with 15 additions and 27 deletions.
42 changes: 15 additions & 27 deletions components/dashboard/src/start/CreateWorkspace.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -462,39 +462,28 @@ interface RunningPrebuildViewProps {

function RunningPrebuildView(props: RunningPrebuildViewProps) {
const [logsEmitter] = useState(new EventEmitter());
const [pollTimeout, setPollTimeout] = useState<NodeJS.Timeout | undefined>();
const [prebuildDoneTriggered, setPrebuildDoneTriggered] = useState<boolean>(false);

useEffect(() => {
const checkIsPrebuildDone = async (): Promise<boolean> => {
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.
setPrebuildDoneTriggered(true);
props.onPrebuildSucceeded();
return true;
}
return false;
};
const pollIsPrebuildDone = async () => {
clearTimeout(pollTimeout!);
await checkIsPrebuildDone();
setPollTimeout(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();
};
}, []);
Expand All @@ -507,7 +496,6 @@ function RunningPrebuildView(props: RunningPrebuildViewProps) {
<button
className="mt-6 secondary"
onClick={() => {
clearTimeout(pollTimeout!);
props.onIgnorePrebuild();
}}
>
Expand Down

0 comments on commit 1887fec

Please sign in to comment.