fix restart monitor

This commit is contained in:
Proddy
2023-07-02 21:56:31 +02:00
parent 2663bc15f4
commit da3e118444
3 changed files with 32 additions and 23 deletions

View File

@@ -11,14 +11,14 @@ export const EVENT_SOURCE_ROOT = 'http://' + host + '/es/';
export const alovaInstance = createAlova({ export const alovaInstance = createAlova({
statesHook: ReactHook, statesHook: ReactHook,
// timeout: 3000, // timeout not used because of uploading firmware timeout: 2000, // timeout not used because of uploading firmware
// localCache: null, localCache: null,
localCache: { // localCache: {
GET: { // GET: {
mode: 'placeholder', // see https://alova.js.org/learning/response-cache/#cache-replaceholder-mode // mode: 'placeholder', // see https://alova.js.org/learning/response-cache/#cache-replaceholder-mode
expire: 2000 // expire: 2000
} // }
}, // },
requestAdapter: xhrRequestAdapter(), requestAdapter: xhrRequestAdapter(),
beforeRequest(method) { beforeRequest(method) {
if (localStorage.getItem(ACCESS_TOKEN)) { if (localStorage.getItem(ACCESS_TOKEN)) {

View File

@@ -1,7 +1,7 @@
import { alovaInstance, alovaInstanceGH } from './endpoints'; import { alovaInstance, alovaInstanceGH } from './endpoints';
import type { OTASettings, SystemStatus, LogSettings, Version } from 'types'; import type { OTASettings, SystemStatus, LogSettings, Version } from 'types';
// SystemStatus - also used to ping in Restart monitor // SystemStatus - also used to ping in Restart monitor for pinging
export const readSystemStatus = () => alovaInstance.Get<SystemStatus>('/rest/systemStatus'); export const readSystemStatus = () => alovaInstance.Get<SystemStatus>('/rest/systemStatus');
// commands // commands
@@ -45,6 +45,7 @@ export const uploadFile = (file: File) => {
const formData = new FormData(); const formData = new FormData();
formData.append('file', file); formData.append('file', file);
return alovaInstance.Post('/rest/uploadFile', formData, { return alovaInstance.Post('/rest/uploadFile', formData, {
timeout: 60000, // override timeout for uploading firmware - 1 minute
enableUpload: true enableUpload: true
}); });
}; };

View File

@@ -1,5 +1,5 @@
import { useRetriableRequest } from '@alova/scene-react'; import { useRequest } from 'alova';
import { useState } from 'react'; import { useRef, useState, useEffect } from 'react';
import type { FC } from 'react'; import type { FC } from 'react';
import * as SystemApi from 'api/system'; import * as SystemApi from 'api/system';
@@ -7,26 +7,34 @@ import { FormLoader } from 'components';
import { useI18nContext } from 'i18n/i18n-react'; import { useI18nContext } from 'i18n/i18n-react';
const RESTART_TIMEOUT = 2 * 60 * 1000;
const POLL_INTERVAL = 3000;
const RestartMonitor: FC = () => { const RestartMonitor: FC = () => {
const [failed, setFailed] = useState<boolean>(false); const [failed, setFailed] = useState<boolean>(false);
const [timeoutId, setTimeoutId] = useState<NodeJS.Timeout>();
const { LL } = useI18nContext(); const { LL } = useI18nContext();
const { send } = useRequest(SystemApi.readSystemStatus, { force: true });
const timeoutAt = useRef(new Date().getTime() + RESTART_TIMEOUT);
// eslint-disable-next-line @typescript-eslint/unbound-method const poll = useRef(async () => {
const { onFail, onSuccess } = useRetriableRequest(SystemApi.readSystemStatus(), { try {
retry: 10, await send();
backoff: { document.location.href = '/';
delay: 1500 } catch (error) {
if (new Date().getTime() < timeoutAt.current) {
setTimeoutId(setTimeout(poll.current, POLL_INTERVAL));
} else {
setFailed(true);
}
} }
}); });
onFail(() => { useEffect(() => {
setFailed(true); void poll.current();
}); }, []);
onSuccess(() => { useEffect(() => () => timeoutId && clearTimeout(timeoutId), [timeoutId]);
document.location.href = '/fileUpdated';
});
return <FormLoader message={LL.APPLICATION_RESTARTING() + '...'} errorMessage={failed ? 'Timed out' : undefined} />; return <FormLoader message={LL.APPLICATION_RESTARTING() + '...'} errorMessage={failed ? 'Timed out' : undefined} />;
}; };