mirror of
https://github.com/jhd3197/ServerKit.git
synced 2026-12-02 13:15:17 +09:00
feat(ui): one visibility-aware polling convention, and a ratchet on the rest
Plan 76 milestone E2, drift/bug row 4. 45 hand-rolled setInterval sites across 35 files, with `visibilitychange` handled in exactly one of them and an in-flight guard in exactly one of them — the documented poller-stampede shape. Two failures every hand-rolled `setInterval(load, 5000)` shares: - it starts a request every 5s whether or not the previous one returned. A slow endpoint does not slow the poller down, it makes it stack; each extra request makes the server slower, which makes the next overlap more likely. This is what a DevTools capture of /domains showed as a wall of pending rows. - it keeps polling a tab nobody is looking at, where browsers throttle timers unevenly anyway, so it is not even a reliable refresh. utils/pollScheduler.js holds the rules as a plain object so they are testable without a DOM (the repo has no jsdom — this follows formState.js/useForm). hooks/usePolling.js is the React binding, and useServerQuery gains `refetchInterval` built on the same scheduler, so there is one implementation rather than a second one for query users. Two design points worth stating, since both differ from what the sites did: - the next tick is armed by the previous run *finishing*, not by setInterval. With setInterval the gap is measured tick-to-tick regardless of how long the work took, so slow work silently turns a 5s gap into no gap. The in-flight guard therefore protects the out-of-band paths — refresh() and the visibility catch-up — rather than the schedule, and the test says so instead of implying a guard the schedule can no longer exercise. - the callback is held in a ref, so an inline arrow re-created each render does not restart the timer. That is the usual reason a hand-rolled poller runs far faster than its stated interval. Migrated four as reference implementations, per the plan's "migrate a representative flow before broader adoption": DeployPill (which had worked all of this out by hand and loses ~20 lines), MonitorsSummary, appdetail/LogsTab, and ProcessListPanel. MonitorsSummary keeps its `refreshKey` prop as an explicit out-of-band refresh rather than folding it into the schedule. Ratchet: check-frontend-boundaries.mjs gains LEGACY_POLLERS with exact per-file counts, now 41 sites in 31 files, so every future cleanup must shrink it and a new bare setInterval fails lint. Genuine non-fetching timers (clock ticks, countdowns) are listed too rather than pattern-matched away — they are harmless but the exact count is what makes the ratchet trustworthy. Gate: npm test (72 passed, 11 of them new), npm run lint (0 errors; 954 warnings, one fewer than the 955 baseline), npm run build clean, boundary check green. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This commit is contained in:
1 parent
054cc33cdf
commit
f21abe7af5
9 files changed
+628
-33
No files matched your search
@@ -3,6 +3,7 @@ import { Link, useLocation } from 'react-router-dom';
|
||||
import { Loader2 } from 'lucide-react';
|
||||
import api from '../services/api';
|
||||
import socketService from '../services/socket';
|
||||
import { usePolling } from '../hooks/usePolling';
|
||||
|
||||
// A small global "deploy in progress" pill: while any deployment job is
|
||||
// pending/running it appears (bottom-left) so you can navigate away mid-deploy
|
||||
@@ -49,30 +50,19 @@ export default function DeployPill() {
|
||||
}
|
||||
}, []);
|
||||
|
||||
// The interval, the hidden-tab pause, and the catch-up on return are the
|
||||
// shared convention now; this component was where they were first worked
|
||||
// out by hand.
|
||||
usePolling(refresh, POLL_MS, { enabled: !onDeploySurface });
|
||||
|
||||
useEffect(() => {
|
||||
if (onDeploySurface) return undefined;
|
||||
|
||||
// Polling a background tab buys nothing and competes for the browser's
|
||||
// per-origin connections with whatever the visible tab is doing.
|
||||
const tick = () => {
|
||||
if (document.visibilityState !== 'visible') return;
|
||||
refresh();
|
||||
};
|
||||
tick();
|
||||
const t = setInterval(tick, POLL_MS);
|
||||
|
||||
// Catch up once when the tab comes back rather than polling all along.
|
||||
const onVisible = () => { if (document.visibilityState === 'visible') refresh(); };
|
||||
document.addEventListener('visibilitychange', onVisible);
|
||||
|
||||
// React quickly to live status changes when a console elsewhere is
|
||||
// streaming — but a burst of deploy_status events must not become a
|
||||
// burst of requests, so the in-flight guard covers these too.
|
||||
const unsubStatus = socketService.on('deploy_status', refresh);
|
||||
const unsubConnect = socketService.on('connected', refresh);
|
||||
return () => {
|
||||
clearInterval(t);
|
||||
document.removeEventListener('visibilitychange', onVisible);
|
||||
unsubStatus();
|
||||
unsubConnect();
|
||||
};
|
||||
|
||||
@@ -2,6 +2,9 @@ import { useState, useEffect } from 'react';
|
||||
import { logsToText } from '@/utils/logText';
|
||||
import api from '../../services/api';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { usePolling } from '../../hooks/usePolling';
|
||||
|
||||
const LOG_REFRESH_MS = 5000;
|
||||
|
||||
const LogsTab = ({ app }) => {
|
||||
const [logs, setLogs] = useState('');
|
||||
@@ -15,11 +18,9 @@ const LogsTab = ({ app }) => {
|
||||
loadLogs();
|
||||
}, [app.id]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!autoRefresh) return;
|
||||
const interval = setInterval(loadLogs, 5000);
|
||||
return () => clearInterval(interval);
|
||||
}, [autoRefresh, app.id]);
|
||||
// immediate: false — the effect above already loads on mount and on app
|
||||
// change; this adds only the repeat while auto-refresh is on.
|
||||
usePolling(loadLogs, LOG_REFRESH_MS, { enabled: autoRefresh, immediate: false });
|
||||
|
||||
async function loadLogs() {
|
||||
try {
|
||||
|
||||
@@ -3,6 +3,7 @@ import { Ban, Activity, TimerReset } from 'lucide-react';
|
||||
import api from '../../services/api';
|
||||
import { useToast } from '../../contexts/ToastContext';
|
||||
import { useConfirm } from '../../hooks/useConfirm';
|
||||
import { usePolling } from '../../hooks/usePolling';
|
||||
import EmptyState from '../EmptyState';
|
||||
import { DataTable, DataTableFooter, SearchField } from '@/components/ds';
|
||||
import {
|
||||
@@ -114,11 +115,10 @@ export default function ProcessListPanel({ conn, engine, active, isAdmin }) {
|
||||
|
||||
useEffect(() => { load(); }, [load]);
|
||||
|
||||
useEffect(() => {
|
||||
if (!active || !autoRefresh) return undefined;
|
||||
const t = setInterval(() => load(true), REFRESH_MS);
|
||||
return () => clearInterval(t);
|
||||
}, [active, autoRefresh, load]);
|
||||
usePolling(() => load(true), REFRESH_MS, {
|
||||
enabled: active && autoRefresh,
|
||||
immediate: false, // the effect above already loaded once
|
||||
});
|
||||
|
||||
async function killProcess(proc) {
|
||||
const verb = engineKind === 'postgresql' ? 'Terminate' : 'Kill';
|
||||
|
||||
@@ -5,13 +5,14 @@
|
||||
// machines ServerKit runs on, and nothing described the things it is supposed to
|
||||
// be watching. Host health stays below; this sits above it because "is the site
|
||||
// up" is the question the page is actually for.
|
||||
import { useCallback, useEffect, useRef, useState } from 'react';
|
||||
import { useCallback, useEffect, useState } from 'react';
|
||||
import { Link, useNavigate } from 'react-router-dom';
|
||||
import { Activity, Plus, Radar, ShieldAlert, Timer, Zap } from 'lucide-react';
|
||||
import api from '../../services/api';
|
||||
import { AreaChart, KpiBand, MetricCard, Pill } from '@/components/ds';
|
||||
import { Button } from '@/components/ui/button';
|
||||
import { monitorStateOf } from './monitorShared';
|
||||
import { usePolling } from '../../hooks/usePolling';
|
||||
|
||||
const POLL_MS = 15000;
|
||||
const FEED_LIMIT = 9;
|
||||
@@ -32,7 +33,6 @@ export default function MonitorsSummary({ refreshKey = 0 }) {
|
||||
const [stats, setStats] = useState(null);
|
||||
const [feed, setFeed] = useState([]);
|
||||
const [loaded, setLoaded] = useState(false);
|
||||
const pollRef = useRef(null);
|
||||
|
||||
const load = useCallback(async () => {
|
||||
try {
|
||||
@@ -60,11 +60,13 @@ export default function MonitorsSummary({ refreshKey = 0 }) {
|
||||
}
|
||||
}, []);
|
||||
|
||||
const refresh = usePolling(load, POLL_MS);
|
||||
|
||||
// The parent bumps refreshKey to force a reload (e.g. after creating a
|
||||
// monitor); it is not part of the polling schedule.
|
||||
useEffect(() => {
|
||||
load();
|
||||
pollRef.current = setInterval(load, POLL_MS);
|
||||
return () => clearInterval(pollRef.current);
|
||||
}, [load, refreshKey]);
|
||||
if (refreshKey) refresh();
|
||||
}, [refreshKey, refresh]);
|
||||
|
||||
if (!loaded) return null;
|
||||
|
||||
|
||||
Reference in new issue
Block a user