feat(server): 添加系统重启功能支持

添加后台任务支持用于处理系统重启请求,实现延迟进程终止机制,
允许系统安全地重启并在重启后自动恢复服务。

feat(frontend): 实现前端系统重启控制面板

在状态页面添加重启对话框组件,提供用户友好的重启确认界面,
包含重启状态监控和错误处理功能,确保用户可以安全重启系统。

docs(deployment): 更新部署指南添加URL协议要求说明

详细说明NANO_AUTHZ_URL和NANO_DEPLOY_URL环境变量必须包含
http://协议前缀的要求,添加常见错误示例和容器重建步骤,
帮助用户避免注册页面502错误问题。
This commit is contained in:
2026-03-19 10:26:43 +08:00
parent 4e45f8b717
commit bfa77204bf
4 changed files with 280 additions and 16 deletions

View File

@ -18,6 +18,7 @@ from urllib.parse import urlsplit, urlunsplit
import httpx
from fastapi import (
BackgroundTasks,
FastAPI,
File,
Form,
@ -112,6 +113,13 @@ async def _reconcile_managed_outlook_mcp(config: Config) -> bool:
return before != after
def _terminate_process_after_delay(delay_seconds: float = 1.0, exit_code: int = 1) -> None:
if delay_seconds > 0:
time.sleep(delay_seconds)
logger.warning("Self-restart requested; exiting backend process with code {}", exit_code)
os._exit(exit_code)
# ============================================================================
# Request/Response models
# ============================================================================
@ -1534,6 +1542,20 @@ def _register_routes(app: FastAPI) -> None:
app.state.auth_tokens.pop(token, None)
return {"ok": True}
@app.post("/api/system/restart", status_code=202)
async def restart_system(
background_tasks: BackgroundTasks,
authorization: str | None = Header(default=None),
):
username = _require_web_user(app, authorization)
logger.warning("Restart requested by user {}", username)
background_tasks.add_task(_terminate_process_after_delay, 1.0, 1)
return {
"ok": True,
"restarting": True,
"detail": "Restart scheduled",
}
# ------ Chat ------
@app.post("/api/chat")

View File

@ -12,17 +12,29 @@ import {
Key,
Loader2,
} from 'lucide-react';
import { getStatus } from '@/lib/api';
import { getStatus, restartSystem } from '@/lib/api';
import {
AlertDialog,
AlertDialogAction,
AlertDialogCancel,
AlertDialogContent,
AlertDialogDescription,
AlertDialogFooter,
AlertDialogHeader,
AlertDialogTitle,
} from '@/components/ui/alert-dialog';
import { Button } from '@/components/ui/button';
import { Card, CardContent, CardHeader, CardTitle } from '@/components/ui/card';
import { Badge } from '@/components/ui/badge';
import { Separator } from '@/components/ui/separator';
import type { SystemStatus } from '@/types';
export default function StatusPage() {
const [status, setStatus] = useState<SystemStatus | null>(null);
const [error, setError] = useState<string | null>(null);
const [loading, setLoading] = useState(true);
const [restartDialogOpen, setRestartDialogOpen] = useState(false);
const [restarting, setRestarting] = useState(false);
const [restartError, setRestartError] = useState<string | null>(null);
const loadStatus = async () => {
setLoading(true);
@ -41,6 +53,36 @@ export default function StatusPage() {
loadStatus();
}, []);
useEffect(() => {
if (!restarting) {
return;
}
const intervalId = window.setInterval(async () => {
try {
await getStatus();
window.location.reload();
} catch {
// Ignore failures until the container is back.
}
}, 3000);
return () => {
window.clearInterval(intervalId);
};
}, [restarting]);
const handleRestart = async () => {
setRestartError(null);
try {
await restartSystem();
setRestartDialogOpen(false);
setRestarting(true);
} catch (err: any) {
setRestartError(err.message || '重启失败');
}
};
if (loading) {
return (
<div className="flex items-center justify-center py-20">
@ -80,7 +122,7 @@ export default function StatusPage() {
<div className="max-w-4xl mx-auto p-6 space-y-6">
<div className="flex items-center justify-between">
<h1 className="text-2xl font-bold"></h1>
<Button onClick={loadStatus} variant="outline" size="sm">
<Button onClick={loadStatus} variant="outline" size="sm" disabled={restarting}>
<RefreshCw className="w-4 h-4 mr-2" />
</Button>
@ -94,17 +136,48 @@ export default function StatusPage() {
</CardTitle>
</CardHeader>
<CardContent className="space-y-3">
<InfoRow
label="配置"
value={status.config_path}
ok={status.config_exists}
/>
<InfoRow
label="工作区"
value={status.workspace}
ok={status.workspace_exists}
/>
<CardContent>
<div className="flex flex-col gap-4 sm:flex-row sm:items-center sm:justify-between">
<div className="space-y-1">
<p className="text-sm font-medium"></p>
<p className="text-sm text-muted-foreground">
{restarting
? '正在重启当前 docker服务恢复后页面会自动刷新。'
: '会重启当前 docker 容器。重启完成后需要重新登录。'}
</p>
{restartError ? (
<p className="text-sm text-destructive">{restartError}</p>
) : null}
</div>
<AlertDialog open={restartDialogOpen} onOpenChange={setRestartDialogOpen}>
<Button
variant="destructive"
onClick={() => setRestartDialogOpen(true)}
disabled={restarting}
>
{restarting ? (
<Loader2 className="w-4 h-4 mr-2 animate-spin" />
) : (
<RefreshCw className="w-4 h-4 mr-2" />
)}
Restart
</Button>
<AlertDialogContent>
<AlertDialogHeader>
<AlertDialogTitle></AlertDialogTitle>
<AlertDialogDescription>
docker
</AlertDialogDescription>
</AlertDialogHeader>
<AlertDialogFooter>
<AlertDialogCancel disabled={restarting}></AlertDialogCancel>
<AlertDialogAction onClick={handleRestart} disabled={restarting}>
{restarting ? '重启中...' : '确认 Restart'}
</AlertDialogAction>
</AlertDialogFooter>
</AlertDialogContent>
</AlertDialog>
</div>
</CardContent>
</Card>

View File

@ -529,6 +529,16 @@ export async function getStatus(): Promise<SystemStatus> {
return fetchJSON('/api/status');
}
export async function restartSystem(): Promise<{
ok: boolean;
restarting: boolean;
detail: string;
}> {
return fetchJSON('/api/system/restart', {
method: 'POST',
});
}
// ---------------------------------------------------------------------------
// Cron (proxied)
// ---------------------------------------------------------------------------