修正資料庫未關閉問題
This commit is contained in:
85
app/api/admin/connection-monitor/route.ts
Normal file
85
app/api/admin/connection-monitor/route.ts
Normal file
@@ -0,0 +1,85 @@
|
||||
// =====================================================
|
||||
// 連線監控 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { connectionMonitor } from '@/lib/connection-monitor';
|
||||
|
||||
export async function GET(request: NextRequest) {
|
||||
try {
|
||||
// 獲取連線統計
|
||||
const stats = await connectionMonitor.getConnectionStats();
|
||||
|
||||
// 獲取監控狀態
|
||||
const isMonitoring = connectionMonitor.isCurrentlyMonitoring();
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: {
|
||||
stats,
|
||||
isMonitoring,
|
||||
timestamp: new Date().toISOString()
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('獲取連線監控數據失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '獲取連線監控數據失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
const body = await request.json();
|
||||
const { action } = body;
|
||||
|
||||
switch (action) {
|
||||
case 'start_monitoring':
|
||||
const interval = body.interval || 30000;
|
||||
connectionMonitor.startMonitoring(interval);
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: `連線監控已啟動,檢查間隔: ${interval}ms`
|
||||
});
|
||||
|
||||
case 'stop_monitoring':
|
||||
connectionMonitor.stopMonitoring();
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '連線監控已停止'
|
||||
});
|
||||
|
||||
case 'force_cleanup':
|
||||
await connectionMonitor.forceCleanup();
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '強制清理連線完成'
|
||||
});
|
||||
|
||||
case 'check_health':
|
||||
const healthStats = await connectionMonitor.checkConnectionHealth();
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
data: healthStats
|
||||
});
|
||||
|
||||
default:
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '無效的操作'
|
||||
}, { status: 400 });
|
||||
}
|
||||
|
||||
} catch (error) {
|
||||
console.error('執行連線監控操作失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '執行連線監控操作失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
66
app/api/admin/force-cleanup/route.ts
Normal file
66
app/api/admin/force-cleanup/route.ts
Normal file
@@ -0,0 +1,66 @@
|
||||
// =====================================================
|
||||
// 強制清理連線 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/database';
|
||||
import { connectionMonitor } from '@/lib/connection-monitor';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
console.log('🧹 開始強制清理資料庫連線...');
|
||||
|
||||
// 獲取清理前的連線狀態
|
||||
const beforeStats = await connectionMonitor.getConnectionStats();
|
||||
console.log(`清理前連線數: ${beforeStats.activeConnections}`);
|
||||
|
||||
// 強制關閉連線池
|
||||
try {
|
||||
await db.close();
|
||||
console.log('✅ 主要資料庫連線池已關閉');
|
||||
} catch (error) {
|
||||
console.error('❌ 關閉主要連線池失敗:', error);
|
||||
}
|
||||
|
||||
// 等待一段時間讓連線完全關閉
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// 重新初始化連線池
|
||||
try {
|
||||
// 重新創建連線池實例
|
||||
const { Database } = await import('@/lib/database');
|
||||
const newDb = Database.getInstance();
|
||||
console.log('✅ 資料庫連線池已重新初始化');
|
||||
} catch (error) {
|
||||
console.error('❌ 重新初始化連線池失敗:', error);
|
||||
}
|
||||
|
||||
// 獲取清理後的連線狀態
|
||||
const afterStats = await connectionMonitor.getConnectionStats();
|
||||
console.log(`清理後連線數: ${afterStats.activeConnections}`);
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '強制清理完成',
|
||||
data: {
|
||||
before: {
|
||||
activeConnections: beforeStats.activeConnections,
|
||||
usagePercentage: beforeStats.usagePercentage
|
||||
},
|
||||
after: {
|
||||
activeConnections: afterStats.activeConnections,
|
||||
usagePercentage: afterStats.usagePercentage
|
||||
},
|
||||
cleaned: beforeStats.activeConnections - afterStats.activeConnections
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('強制清理失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '強制清理失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
70
app/api/admin/kill-connections/route.ts
Normal file
70
app/api/admin/kill-connections/route.ts
Normal file
@@ -0,0 +1,70 @@
|
||||
// =====================================================
|
||||
// 強制終止連線 API
|
||||
// =====================================================
|
||||
|
||||
import { NextRequest, NextResponse } from 'next/server';
|
||||
import { db } from '@/lib/database';
|
||||
|
||||
export async function POST(request: NextRequest) {
|
||||
try {
|
||||
console.log('💀 開始強制終止所有資料庫連線...');
|
||||
|
||||
// 獲取所有 AI_Platform 的連線
|
||||
const connections = await db.query(`
|
||||
SELECT ID, USER, HOST, DB, COMMAND, TIME, STATE
|
||||
FROM INFORMATION_SCHEMA.PROCESSLIST
|
||||
WHERE USER = 'AI_Platform' AND DB = 'db_AI_Platform'
|
||||
`);
|
||||
|
||||
console.log(`找到 ${connections.length} 個 AI_Platform 連線`);
|
||||
|
||||
const killedConnections = [];
|
||||
|
||||
// 終止每個連線
|
||||
for (const conn of connections) {
|
||||
try {
|
||||
await db.query(`KILL CONNECTION ${conn.ID}`);
|
||||
killedConnections.push({
|
||||
id: conn.ID,
|
||||
host: conn.HOST,
|
||||
time: conn.TIME,
|
||||
command: conn.COMMAND
|
||||
});
|
||||
console.log(`✅ 已終止連線 ${conn.ID} (閒置 ${conn.TIME} 秒)`);
|
||||
} catch (error) {
|
||||
console.error(`❌ 終止連線 ${conn.ID} 失敗:`, error);
|
||||
}
|
||||
}
|
||||
|
||||
// 等待連線完全關閉
|
||||
await new Promise(resolve => setTimeout(resolve, 2000));
|
||||
|
||||
// 檢查剩餘連線
|
||||
const remainingConnections = await db.query(`
|
||||
SELECT COUNT(*) as count
|
||||
FROM INFORMATION_SCHEMA.PROCESSLIST
|
||||
WHERE USER = 'AI_Platform' AND DB = 'db_AI_Platform'
|
||||
`);
|
||||
|
||||
const remainingCount = remainingConnections[0]?.count || 0;
|
||||
|
||||
return NextResponse.json({
|
||||
success: true,
|
||||
message: '強制終止連線完成',
|
||||
data: {
|
||||
totalFound: connections.length,
|
||||
killed: killedConnections.length,
|
||||
remaining: remainingCount,
|
||||
killedConnections: killedConnections
|
||||
}
|
||||
});
|
||||
|
||||
} catch (error) {
|
||||
console.error('強制終止連線失敗:', error);
|
||||
return NextResponse.json({
|
||||
success: false,
|
||||
message: '強制終止連線失敗',
|
||||
error: error instanceof Error ? error.message : '未知錯誤'
|
||||
}, { status: 500 });
|
||||
}
|
||||
}
|
Reference in New Issue
Block a user