FIX - TASKER STARTUP: автовосстановление после запуска backend
This commit is contained in:
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import { useTheme } from "next-themes";
|
||||
import { Button } from "@plane/propel/button";
|
||||
// assets
|
||||
import maintenanceModeDarkModeImage from "@/app/assets/instance/maintenance-mode-dark.svg?url";
|
||||
import maintenanceModeLightModeImage from "@/app/assets/instance/maintenance-mode-light.svg?url";
|
||||
@@ -12,8 +13,18 @@ import maintenanceModeLightModeImage from "@/app/assets/instance/maintenance-mod
|
||||
import DefaultLayout from "@/layouts/default-layout";
|
||||
// components
|
||||
import { MaintenanceMessage } from "@/plane-web/components/instance";
|
||||
import type { TMaintenanceReason } from "@/plane-web/components/instance";
|
||||
|
||||
export function MaintenanceView() {
|
||||
type TMaintenanceViewProps = {
|
||||
autoRetry?: boolean;
|
||||
isRetrying?: boolean;
|
||||
reason?: TMaintenanceReason;
|
||||
statusCode?: number;
|
||||
onRetry?: () => void;
|
||||
};
|
||||
|
||||
export function MaintenanceView(props: TMaintenanceViewProps) {
|
||||
const { autoRetry = false, isRetrying = false, reason = "unavailable", statusCode, onRetry } = props;
|
||||
// hooks
|
||||
const { resolvedTheme } = useTheme();
|
||||
// derived values
|
||||
@@ -31,7 +42,20 @@ export function MaintenanceView() {
|
||||
/>
|
||||
</div>
|
||||
<div className="relative mt-4 flex w-full flex-col gap-4">
|
||||
<MaintenanceMessage />
|
||||
<MaintenanceMessage autoRetry={autoRetry} reason={reason} statusCode={statusCode} />
|
||||
{onRetry && (
|
||||
<div className="flex justify-start">
|
||||
<Button
|
||||
variant="primary"
|
||||
size="lg"
|
||||
className="nodedc-error-primary"
|
||||
loading={isRetrying}
|
||||
onClick={onRetry}
|
||||
>
|
||||
Проверить сейчас
|
||||
</Button>
|
||||
</div>
|
||||
)}
|
||||
</div>
|
||||
</div>
|
||||
</DefaultLayout>
|
||||
|
||||
@@ -5,6 +5,7 @@
|
||||
*/
|
||||
|
||||
import type { ReactNode } from "react";
|
||||
import { isAxiosError } from "axios";
|
||||
import { observer } from "mobx-react";
|
||||
import useSWR from "swr";
|
||||
// components
|
||||
@@ -17,16 +18,44 @@ type TInstanceWrapper = {
|
||||
children: ReactNode;
|
||||
};
|
||||
|
||||
const INSTANCE_RETRY_BASE_INTERVAL = 2_000;
|
||||
const INSTANCE_RETRY_MAX_INTERVAL = 30_000;
|
||||
|
||||
export const getInstanceRetryInterval = (retryCount: number) =>
|
||||
Math.min(INSTANCE_RETRY_MAX_INTERVAL, INSTANCE_RETRY_BASE_INTERVAL * 2 ** Math.min(Math.max(retryCount - 1, 0), 4));
|
||||
|
||||
export const getInstanceErrorStatus = (error: unknown) => (isAxiosError(error) ? error.response?.status : undefined);
|
||||
|
||||
export const shouldRetryInstanceRequest = (error: unknown) => {
|
||||
if (!isAxiosError(error) || !error.response) return true;
|
||||
|
||||
const { status } = error.response;
|
||||
return status === 408 || status === 429 || status >= 500;
|
||||
};
|
||||
|
||||
const InstanceWrapper = observer(function InstanceWrapper(props: TInstanceWrapper) {
|
||||
const { children } = props;
|
||||
// store
|
||||
const { isLoading, instance, error, fetchInstanceInfo } = useInstance();
|
||||
|
||||
const { isLoading: isInstanceSWRLoading, error: instanceSWRError } = useSWR(
|
||||
"INSTANCE_INFORMATION",
|
||||
async () => await fetchInstanceInfo(),
|
||||
{ revalidateOnFocus: false }
|
||||
);
|
||||
const {
|
||||
isLoading: isInstanceSWRLoading,
|
||||
isValidating: isInstanceSWRValidating,
|
||||
error: instanceSWRError,
|
||||
mutate: retryInstanceInfo,
|
||||
} = useSWR("INSTANCE_INFORMATION", async () => await fetchInstanceInfo(), {
|
||||
revalidateOnFocus: true,
|
||||
revalidateOnReconnect: true,
|
||||
shouldRetryOnError: true,
|
||||
onErrorRetry: (requestError, _key, _config, revalidate, { retryCount }) => {
|
||||
if (typeof navigator !== "undefined" && !navigator.onLine) return;
|
||||
if (!shouldRetryInstanceRequest(requestError)) return;
|
||||
|
||||
setTimeout(() => {
|
||||
void revalidate({ retryCount });
|
||||
}, getInstanceRetryInterval(retryCount));
|
||||
},
|
||||
});
|
||||
|
||||
// loading state
|
||||
if ((isLoading || isInstanceSWRLoading) && !instance)
|
||||
@@ -36,7 +65,27 @@ const InstanceWrapper = observer(function InstanceWrapper(props: TInstanceWrappe
|
||||
</div>
|
||||
);
|
||||
|
||||
if (instanceSWRError) return <MaintenanceView />;
|
||||
if (instanceSWRError) {
|
||||
const statusCode = getInstanceErrorStatus(instanceSWRError);
|
||||
const reason =
|
||||
statusCode === 502 || statusCode === 503 || statusCode === 504
|
||||
? "starting"
|
||||
: statusCode === undefined
|
||||
? "offline"
|
||||
: "unavailable";
|
||||
|
||||
return (
|
||||
<MaintenanceView
|
||||
autoRetry={shouldRetryInstanceRequest(instanceSWRError)}
|
||||
isRetrying={isInstanceSWRValidating}
|
||||
reason={reason}
|
||||
statusCode={statusCode}
|
||||
onRetry={() => {
|
||||
void retryInstanceInfo();
|
||||
}}
|
||||
/>
|
||||
);
|
||||
}
|
||||
|
||||
// something went wrong while in the request
|
||||
if (error && error?.status === "error") return <>{children}</>;
|
||||
|
||||
Reference in New Issue
Block a user