Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix health check AlertNotification #2571

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
73 changes: 25 additions & 48 deletions frontend/src/components/common/AlertNotification.tsx
Original file line number Diff line number Diff line change
Expand Up @@ -21,46 +21,36 @@ const ROUTES_WITHOUT_ALERT = ['login', 'token', 'settingsCluster'];
export function PureAlertNotification({ checkerFunction }: PureAlertNotificationProps) {
const [networkStatusCheckTimeFactor, setNetworkStatusCheckTimeFactor] = React.useState(0);
const [error, setError] = React.useState<null | string | boolean>(null);
const [intervalID, setIntervalID] = React.useState<NodeJS.Timeout | null>(null);
const { t } = useTranslation();
const { pathname } = useLocation();

function registerSetInterval(): NodeJS.Timeout {
return setInterval(() => {
if (!window.navigator.onLine) {
setError(t('translation|Offline') as string);
return;
}
const performHealthCheck = React.useCallback(() => {
if (!window.navigator.onLine) {
setError('Offline');
return;
}

// Don't check for the cluster health if we are not on a cluster route.
if (!getCluster()) {
setError(null);
return;
}
// Don't check for the cluster health if we are not on a cluster route.
if (!getCluster()) {
setError(null);
return;
}

checkerFunction()
.then(() => {
setError(false);
})
.catch(err => {
const error = new Error(err);
setError(error.message);
setNetworkStatusCheckTimeFactor(
(networkStatusCheckTimeFactor: number) => networkStatusCheckTimeFactor + 1
);
});
}, (networkStatusCheckTimeFactor + 1) * NETWORK_STATUS_CHECK_TIME);
}
return checkerFunction()
.then(() => setError(false))
.catch(err => {
setError(err.message);
setNetworkStatusCheckTimeFactor(prev => prev + 1);
});
}, []);

React.useEffect(
() => {
const id = registerSetInterval();
setIntervalID(id);
return () => clearInterval(id);
},
// eslint-disable-next-line
[]
);
React.useEffect(() => {
const interval = setInterval(
performHealthCheck,
(networkStatusCheckTimeFactor + 1) * NETWORK_STATUS_CHECK_TIME
);
return () => clearInterval(interval);
}, [performHealthCheck, networkStatusCheckTimeFactor]);

// Make sure we do not show the alert notification if we are not on a cluster route.
React.useEffect(() => {
Expand All @@ -69,19 +59,6 @@ export function PureAlertNotification({ checkerFunction }: PureAlertNotification
}
}, [pathname]);

React.useEffect(
() => {
if (intervalID) {
clearInterval(intervalID);
}
const id = registerSetInterval();
setIntervalID(id);
return () => clearInterval(id);
},
// eslint-disable-next-line
[networkStatusCheckTimeFactor]
);

const showOnRoute = React.useMemo(() => {
for (const route of ROUTES_WITHOUT_ALERT) {
const routePath = getRoutePath(getRoute(route));
Expand Down Expand Up @@ -130,7 +107,7 @@ export function PureAlertNotification({ checkerFunction }: PureAlertNotification
background: theme.palette.error.dark,
},
})}
onClick={() => setNetworkStatusCheckTimeFactor(0)}
onClick={performHealthCheck}
size="small"
>
{t('translation|Try Again')}
Expand Down
Loading