fix(get-status): return error in case of cache retrieval failure

This commit is contained in:
Nicolas Meienberger 2023-10-15 14:27:09 +02:00 committed by Nicolas Meienberger
parent 2cff0de4ac
commit 27651d53b5
2 changed files with 7 additions and 3 deletions
src
app/api/get-status
client/components/hoc/StatusProvider

View file

@ -1,5 +1,7 @@
import { TipiCache } from '@/server/core/TipiCache';
export const dynamic = 'force-dynamic';
export async function GET() {
try {
const cache = new TipiCache('getStatus');
@ -8,6 +10,6 @@ export async function GET() {
return Response.json({ success: true, status: status || 'RUNNING' });
} catch (error) {
return Response.json({ success: false, status: 'RUNNING' });
return Response.json({ success: false, status: 'ERROR', error });
}
}

View file

@ -10,7 +10,8 @@ interface IProps {
children: React.ReactNode;
}
const fetcher: Fetcher<{ status?: SystemStatus }> = () => fetch('/api/get-status').then((res) => res.json() as Promise<{ status: SystemStatus }>);
const fetcher: Fetcher<{ status?: SystemStatus; success?: boolean }> = () =>
fetch('/api/get-status', { cache: 'no-store', next: { revalidate: 0 } }).then((res) => res.json() as Promise<{ status: SystemStatus }>);
export const StatusProvider: React.FC<IProps> = ({ children }) => {
const { status, setStatus, pollStatus, setPollStatus } = useSystemStore();
@ -22,7 +23,7 @@ export const StatusProvider: React.FC<IProps> = ({ children }) => {
refreshInterval: pollStatus ? 2000 : 0,
isPaused: () => !pollStatus,
onSuccess: (res) => {
if (res.status) {
if (res.success && res.status) {
setStatus(res.status);
}
},
@ -32,6 +33,7 @@ export const StatusProvider: React.FC<IProps> = ({ children }) => {
// If previous was not running and current is running, we need to refresh the page
if (status === 'RUNNING' && s.current !== 'RUNNING') {
setPollStatus(false);
router.push('/');
router.refresh();
}
if (status === 'RUNNING') {