Skip to content

Commit

Permalink
adding retry strategy in KurtosisUpgrader
Browse files Browse the repository at this point in the history
  • Loading branch information
leoporoli committed May 29, 2024
1 parent 3476f68 commit 4fdc346
Showing 1 changed file with 30 additions and 3 deletions.
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
import { Alert, AlertDescription, AlertIcon, Box, Button, Flex } from "@chakra-ui/react";
import { PropsWithChildren, useEffect, useState } from "react";
import { GetCloudInstanceConfigResponse } from "enclave-manager-sdk/build/kurtosis_backend_server_api_pb";
import { PropsWithChildren, useCallback, useEffect, useState } from "react";
import { FiDownloadCloud, FiRotateCcw } from "react-icons/fi";
import { GoBug } from "react-icons/go";
import { useKurtosisClient } from "../../../client/enclaveManager/KurtosisClientContext";
Expand Down Expand Up @@ -58,6 +59,31 @@ export const KurtosisUpgrader = () => {
}
};

const wait = (ms: number) => {
return new Promise((resolve) => setTimeout(resolve, ms));
};

const getCloudInstanceConfigWithRetry = useCallback(
async (tries: number, interval: number): Promise<GetCloudInstanceConfigResponse> => {
let getCloudInstanceConfigResponse: GetCloudInstanceConfigResponse = new GetCloudInstanceConfigResponse();
try {
getCloudInstanceConfigResponse = await kurtosisClient.getCloudInstanceConfig(skipCache);
} catch (e) {
const newTries = tries - 1;

if (newTries === 0) {
throw e;
}

await wait(interval);

return getCloudInstanceConfigWithRetry(newTries, interval);
}
return getCloudInstanceConfigResponse;
},
[],
);

// Check once on load if a new Kurtosis version is available
useEffect(() => {
if (!kurtosisClient.isRunningInCloud()) return;
Expand All @@ -74,7 +100,8 @@ export const KurtosisUpgrader = () => {

const interval = setInterval(async () => {
try {
const getCloudInstanceConfigResponse = await kurtosisClient.getCloudInstanceConfig(skipCache);
// Calling it with retries because when the engine is restarted it won't return a response for a few seconds
const getCloudInstanceConfigResponse = await getCloudInstanceConfigWithRetry(5, 2000);
const instanceStatus = getCloudInstanceConfigResponse.status;
if (instanceStatus === "running") {
setUpgradeStatus(UpgradeStatus.SUCCESS);
Expand All @@ -88,7 +115,7 @@ export const KurtosisUpgrader = () => {
}, 10000);

return () => clearInterval(interval);
}, [upgradeStatus, kurtosisClient]);
}, [upgradeStatus, kurtosisClient, getCloudInstanceConfigWithRetry]);

if (upgradeStatus === UpgradeStatus.NONE) {
return null;
Expand Down

0 comments on commit 4fdc346

Please sign in to comment.