'use client'; import React, { useState, useEffect } from 'react'; import { Button } from '@/components/ui/button'; import { Card, CardContent, CardDescription, CardHeader, CardTitle } from '@/components/ui/card'; import { Badge } from '@/components/ui/badge'; import { Alert, AlertDescription } from '@/components/ui/alert'; import { Loader2, Database, Power, AlertTriangle, CheckCircle } from 'lucide-react'; interface ShutdownStatus { isShuttingDown: boolean; handlerCount: number; registeredHandlers: string[]; } export default function DatabaseShutdownPage() { const [status, setStatus] = useState(null); const [loading, setLoading] = useState(false); const [message, setMessage] = useState(''); const [error, setError] = useState(''); // 獲取關閉狀態 const fetchStatus = async () => { try { setLoading(true); const response = await fetch('/api/test-shutdown?action=status'); const data = await response.json(); if (data.success) { setStatus(data.data); setMessage('狀態更新成功'); setError(''); } else { setError(data.error || '獲取狀態失敗'); } } catch (err) { setError('網路錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 測試關閉機制 const testShutdown = async () => { try { setLoading(true); const response = await fetch('/api/test-shutdown?action=test'); const data = await response.json(); if (data.success) { setMessage('關閉機制測試成功'); setError(''); await fetchStatus(); // 重新獲取狀態 } else { setError(data.error || '測試失敗'); } } catch (err) { setError('測試錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 強制關閉測試 const forceShutdown = async () => { if (!confirm('確定要執行強制關閉測試嗎?這可能會影響應用程式運行!')) { return; } try { setLoading(true); const response = await fetch('/api/test-shutdown?action=force'); const data = await response.json(); if (data.success) { setMessage('強制關閉測試完成'); setError(''); await fetchStatus(); // 重新獲取狀態 } else { setError(data.error || '強制關閉測試失敗'); } } catch (err) { setError('強制關閉錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 優雅關閉測試 const gracefulShutdown = async () => { if (!confirm('確定要執行優雅關閉測試嗎?這會關閉所有資料庫連線!')) { return; } try { setLoading(true); const response = await fetch('/api/test-shutdown', { method: 'POST', headers: { 'Content-Type': 'application/json', }, body: JSON.stringify({ action: 'graceful' }), }); const data = await response.json(); if (data.success) { setMessage('優雅關閉測試完成'); setError(''); await fetchStatus(); // 重新獲取狀態 } else { setError(data.error || '優雅關閉測試失敗'); } } catch (err) { setError('優雅關閉錯誤: ' + (err instanceof Error ? err.message : '未知錯誤')); } finally { setLoading(false); } }; // 組件載入時獲取狀態 useEffect(() => { fetchStatus(); }, []); return (

資料庫關閉管理

監控和管理資料庫連線的關閉機制

{/* 狀態顯示 */} {status && ( 關閉狀態 當前資料庫關閉管理器的運行狀態
關閉狀態: {status.isShuttingDown ? "關閉中" : "正常"}
處理器數量: {status.handlerCount}
狀態: {status.isShuttingDown ? "異常" : "正常"}
已註冊的處理器:
{status.registeredHandlers.map((handler, index) => ( {handler} ))}
)} {/* 操作按鈕 */} 關閉操作 測試和管理資料庫關閉機制
{/* 訊息顯示 */} {message && ( {message} )} {error && ( {error} )} {/* 說明資訊 */} 使用說明

測試關閉機制: 檢查關閉處理器是否正常註冊,不會實際關閉連線

強制關閉測試: 模擬強制關閉所有資料庫連線(用於測試)

優雅關閉測試: 執行完整的優雅關閉流程(會實際關閉連線)

• 當應用程式收到 SIGINT 或 SIGTERM 信號時,會自動執行優雅關閉

• 關閉管理器會確保所有資料庫連線池都被正確關閉

); }