pm2 for BOTH, development included. A `bun dev` started in a terminal dies with the terminal, and nobody can tell whether it is running without finding the window it was started in. Under pm2 the two are the same kind of thing — a named process with a status, an uptime and a log, startable from here and inspectable from anywhere. The cost is that development stops printing to a console somebody is watching, which is what `pm2 logs fofos-dev` is for; the process names are in the panel so nobody has to guess them. This plugin owns exactly two names, `fofos-dev` and `fofos-prod`, and every command is built from that pair and from nothing a caller supplies. No request can stop, restart or delete another process on the machine. That matters more than usual here: Officer's own processes share this pm2 daemon, and a plugin able to name any of them would be a way to take the host down through a web form. Start is really a replace — delete, then start. Not `restart`: it fails on a process that has never existed, and on one that has it reuses the environment it was started with, so a port or database changed here would be saved, displayed, and not in effect. That is an hour of somebody's debugging. The environment is assembled here because this plugin is what knows the ports and the database names, and it is handed to pm2 through the invoking command's own environment — pm2 has no flag for it; `--env` names a block in an ecosystem file. The site reads .env files first and the environment last, so this fills what is absent rather than overriding what somebody wrote down. Ports have no default. It is the one setting where a plausible guess is worst: it binds, the site appears to work, and it is quietly answering on a port another service wanted. Co-Authored-By: Claude Opus 5 <noreply@anthropic.com>
220 lines
7.8 KiB
TypeScript
220 lines
7.8 KiB
TypeScript
import { useCallback, useEffect, useRef, useState } from 'react';
|
|
import { Check, CircleDot, Loader2, Play, Server, Square } from 'lucide-react';
|
|
import { useClient } from 'hooks/useClient';
|
|
import { Button } from '@/components/ui/button';
|
|
import { Input } from '@/components/ui/input';
|
|
import { readableError } from './errors';
|
|
import { Block } from './Block';
|
|
import { Console } from './Console';
|
|
import { useFlash } from './useFlash';
|
|
|
|
/**
|
|
* The two servers that serve the site, under pm2.
|
|
*
|
|
* ── Both under pm2, development included ──
|
|
*
|
|
* A `bun dev` started in a terminal dies with the terminal, and nobody can tell whether it is running
|
|
* without finding the window it was started in. Under pm2 the two are the same kind of thing: a named
|
|
* process with a status and an uptime, startable from here and inspectable from anywhere.
|
|
*
|
|
* ── Start is really a replace ──
|
|
*
|
|
* The port and the database come from these settings, and pm2 keeps the environment a process was
|
|
* STARTED with. So starting removes any existing process of that name first — otherwise a port changed
|
|
* here would be saved, shown, and not in effect, which is the kind of thing somebody debugs for an hour.
|
|
*
|
|
* ── The process names are shown ──
|
|
*
|
|
* `fofos-dev` and `fofos-prod`, in the row. Whatever this panel can do, `pm2 logs fofos-dev` does more,
|
|
* and somebody who knows that should not have to guess the name to use it.
|
|
*/
|
|
|
|
type Server = {
|
|
name: 'development' | 'production';
|
|
process: string;
|
|
port: number | null;
|
|
status: string | null;
|
|
uptimeMs: number | null;
|
|
restarts: number | null;
|
|
url: string | null;
|
|
};
|
|
|
|
const uptime = (ms: number) => {
|
|
const minutes = Math.floor(ms / 60000);
|
|
if (minutes < 60) return `${minutes}m`;
|
|
const hours = Math.floor(minutes / 60);
|
|
return hours < 24 ? `${hours}h ${minutes % 60}m` : `${Math.floor(hours / 24)}d ${hours % 24}h`;
|
|
};
|
|
|
|
export const ServersPanel = () => {
|
|
const client = useClient();
|
|
const clientRef = useRef(client);
|
|
clientRef.current = client;
|
|
|
|
const [servers, setServers] = useState<Server[] | null>(null);
|
|
const [dev, setDev] = useState('');
|
|
const [prod, setProd] = useState('');
|
|
const [busy, setBusy] = useState<string | null>(null);
|
|
const [error, setError] = useState<string | null>(null);
|
|
const [output, setOutput] = useState<string | null>(null);
|
|
const [failed, setFailed] = useState(false);
|
|
const [flashing, flash] = useFlash();
|
|
|
|
const load = useCallback(async () => {
|
|
try {
|
|
const current = await clientRef.current.get<Server[]>('/fofos-admin/_config/servers');
|
|
setServers(current);
|
|
setDev((typed) => typed || String(current.find((s) => s.name === 'development')?.port ?? ''));
|
|
setProd((typed) => typed || String(current.find((s) => s.name === 'production')?.port ?? ''));
|
|
} catch {
|
|
setServers(null);
|
|
}
|
|
}, []);
|
|
|
|
useEffect(() => {
|
|
void load();
|
|
}, [load]);
|
|
|
|
const savePorts = async () => {
|
|
setBusy('ports');
|
|
setError(null);
|
|
try {
|
|
setServers(
|
|
await clientRef.current.put<Server[]>('/fofos-admin/_config/servers', {
|
|
development: Number(dev),
|
|
production: Number(prod),
|
|
}),
|
|
);
|
|
flash();
|
|
} catch (failure) {
|
|
setError(readableError(failure));
|
|
} finally {
|
|
setBusy(null);
|
|
}
|
|
};
|
|
|
|
const act = async (which: string, action: 'start' | 'stop') => {
|
|
setBusy(`${which}:${action}`);
|
|
setError(null);
|
|
setFailed(false);
|
|
setOutput(null);
|
|
try {
|
|
const result = await clientRef.current.post<{ ok: boolean; output: string; servers: Server[] }>(
|
|
`/fofos-admin/_config/servers/${which}/${action}`,
|
|
{},
|
|
);
|
|
setServers(result.servers);
|
|
setOutput(result.output);
|
|
setFailed(!result.ok);
|
|
} catch (failure) {
|
|
setOutput(readableError(failure));
|
|
setFailed(true);
|
|
void load();
|
|
} finally {
|
|
setBusy(null);
|
|
}
|
|
};
|
|
|
|
const dirty =
|
|
dev !== String(servers?.find((s) => s.name === 'development')?.port ?? '') ||
|
|
prod !== String(servers?.find((s) => s.name === 'production')?.port ?? '');
|
|
|
|
const row = (server: Server, value: string, onChange: (next: string) => void) => {
|
|
const online = server.status === 'online';
|
|
const label = server.name === 'development' ? 'Development' : 'Production';
|
|
|
|
return (
|
|
<div key={server.name} className="flex flex-wrap items-center gap-3 border-t border-border pt-3 first:border-0 first:pt-0">
|
|
<div className="min-w-0 flex-1">
|
|
<p className="flex items-center gap-1.5 text-xs">
|
|
{/* pm2's own word, not an interpretation of it. `errored` and `stopped` mean different
|
|
things to whoever has to fix it. */}
|
|
<CircleDot className={`size-3 ${online ? 'text-success' : server.status ? 'text-destructive' : 'text-muted-foreground/40'}`} />
|
|
{label}
|
|
<span className="text-muted-foreground/70">
|
|
{server.status ?? 'not started'}
|
|
{online && server.uptimeMs !== null ? ` · up ${uptime(server.uptimeMs)}` : ''}
|
|
{server.restarts ? ` · ${server.restarts} restarts` : ''}
|
|
</span>
|
|
</p>
|
|
<p className="font-mono text-xs text-muted-foreground/60">
|
|
{server.process}
|
|
{online && server.url ? ` · ${server.url}` : ''}
|
|
</p>
|
|
</div>
|
|
|
|
<Input
|
|
value={value}
|
|
onChange={(event) => onChange(event.target.value.replace(/[^0-9]/g, ''))}
|
|
placeholder="3010"
|
|
className="w-24 shrink-0 font-mono text-xs"
|
|
inputMode="numeric"
|
|
autoComplete="off"
|
|
/>
|
|
|
|
<div className="flex shrink-0 gap-2">
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => void act(server.name, 'start')}
|
|
// Dirty ports are not in effect until saved, and starting with the old one silently would
|
|
// be the confusing outcome.
|
|
disabled={busy !== null || dirty || !value}
|
|
title={dirty ? 'Save the ports first' : online ? 'Restart with the current settings' : undefined}
|
|
>
|
|
{busy === `${server.name}:start` ? <Loader2 className="size-3.5 animate-spin" /> : <Play className="size-3.5" />}
|
|
{online ? 'Restart' : 'Start'}
|
|
</Button>
|
|
<Button
|
|
variant="outline"
|
|
size="sm"
|
|
onClick={() => void act(server.name, 'stop')}
|
|
disabled={busy !== null || !server.status}
|
|
>
|
|
{busy === `${server.name}:stop` ? <Loader2 className="size-3.5 animate-spin" /> : <Square className="size-3.5" />}
|
|
Stop
|
|
</Button>
|
|
</div>
|
|
</div>
|
|
);
|
|
};
|
|
|
|
return (
|
|
<div className="flex size-full flex-col gap-4 overflow-auto p-5">
|
|
<Block
|
|
icon={Server}
|
|
title="Servers"
|
|
hint="The site's development and production processes, run under pm2."
|
|
status={
|
|
flashing ? (
|
|
<span className="flex items-center gap-1 text-xs text-success">
|
|
<Check className="size-3.5" />
|
|
Saved
|
|
</span>
|
|
) : undefined
|
|
}
|
|
>
|
|
{!servers ? null : (
|
|
<>
|
|
{servers.map((server) =>
|
|
server.name === 'development' ? row(server, dev, setDev) : row(server, prod, setProd),
|
|
)}
|
|
|
|
{error && <p className="text-xs text-destructive">{error}</p>}
|
|
<Console output={output} failed={failed} />
|
|
|
|
{dirty && (
|
|
<div className="flex justify-end border-t border-border pt-3">
|
|
<Button size="sm" onClick={() => void savePorts()} disabled={busy !== null || !dev || !prod}>
|
|
{busy === 'ports' ? <Loader2 className="size-3.5 animate-spin" /> : <Check className="size-3.5" />}
|
|
Save ports
|
|
</Button>
|
|
</div>
|
|
)}
|
|
</>
|
|
)}
|
|
</Block>
|
|
</div>
|
|
);
|
|
};
|