bug修复
This commit is contained in:
1 parent
914208aa70
commit
88f7fc7141
8 files changed
+949
-456
No files matched your search
+213
-124
@@ -1,19 +1,12 @@
|
||||
<?php
|
||||
/**
|
||||
* 魔方平台API接口封装
|
||||
* 所有智简魔方的操作API以及相关函数存储在此文件中
|
||||
*/
|
||||
|
||||
require_once __DIR__ . '/app/db_helper.php';
|
||||
require_once __DIR__ . '/app/logger.php';
|
||||
|
||||
// Token缓存文件路径
|
||||
define('TOKEN_CACHE_FILE', __DIR__ . '/app/token.php');
|
||||
define('TOKEN_EXPIRE_TIME', 7200); // Token过期时间(秒),2小时
|
||||
define('TOKEN_EXPIRE_TIME', 7200);
|
||||
|
||||
/**
|
||||
* 获取缓存的Token
|
||||
* @param int $configId 配置ID
|
||||
* @return string|null Token字符串或null
|
||||
*/
|
||||
function getCachedToken($configId) {
|
||||
if (!file_exists(TOKEN_CACHE_FILE)) {
|
||||
@@ -43,8 +36,6 @@ function getCachedToken($configId) {
|
||||
|
||||
/**
|
||||
* 保存Token到缓存文件
|
||||
* @param int $configId 配置ID
|
||||
* @param string $token Token字符串
|
||||
*/
|
||||
function saveToken($configId, $token) {
|
||||
$cached_tokens = [];
|
||||
@@ -77,10 +68,6 @@ function saveTokensFile($cached_tokens) {
|
||||
|
||||
/**
|
||||
* 魔方平台登录获取JWT Token
|
||||
* @param string $siteUrl API基础URL
|
||||
* @param string $account 账户
|
||||
* @param string $apiKey API密钥
|
||||
* @return string|null JWT Token或null
|
||||
*/
|
||||
function mofangLogin($siteUrl, $account, $apiKey) {
|
||||
try {
|
||||
@@ -104,7 +91,7 @@ function mofangLogin($siteUrl, $account, $apiKey) {
|
||||
curl_close($ch);
|
||||
|
||||
if ($httpCode !== 200) {
|
||||
error_log("魔方登录请求失败,HTTP状态码: {$httpCode}");
|
||||
Logger::error("魔方登录请求失败,HTTP状态码: {$httpCode}", 'mofangLogin');
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -113,20 +100,18 @@ function mofangLogin($siteUrl, $account, $apiKey) {
|
||||
if (isset($result['status']) && $result['status'] === 200 && isset($result['jwt'])) {
|
||||
return $result['jwt'];
|
||||
} else {
|
||||
error_log("魔方登录失败: " . ($result['msg'] ?? '未知错误'));
|
||||
Logger::error("魔方登录失败: " . ($result['msg'] ?? '未知错误'), 'mofangLogin');
|
||||
return null;
|
||||
}
|
||||
|
||||
} catch (Exception $e) {
|
||||
error_log("魔方登录异常: " . $e->getMessage());
|
||||
Logger::error("魔方登录异常: " . $e->getMessage(), 'mofangLogin');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
/**
|
||||
* 获取有效的Token(先查缓存,没有则重新登录)
|
||||
* @param int $configId 配置ID
|
||||
* @return string|null Token或null
|
||||
*/
|
||||
function getValidToken($configId) {
|
||||
// 先尝试从缓存获取
|
||||
@@ -140,7 +125,7 @@ function getValidToken($configId) {
|
||||
$config = $db->queryOne('SELECT * FROM configs WHERE id = ?', [$configId]);
|
||||
|
||||
if (!$config) {
|
||||
error_log("配置ID {$configId} 不存在");
|
||||
Logger::error("配置ID {$configId} 不存在", 'getValidToken');
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -155,12 +140,6 @@ function getValidToken($configId) {
|
||||
|
||||
/**
|
||||
* 发送魔方API请求
|
||||
* @param string $siteUrl API基础URL
|
||||
* @param string $endpoint API端点
|
||||
* @param string $method HTTP方法 (GET/POST/PUT)
|
||||
* @param array $data 请求数据
|
||||
* @param int $configId 配置ID
|
||||
* @return array|null 响应数据或null
|
||||
*/
|
||||
function mofangApiRequest($siteUrl, $endpoint, $method = 'GET', $data = [], $configId = null) {
|
||||
// 获取Token
|
||||
@@ -233,10 +212,6 @@ function mofangApiRequest($siteUrl, $endpoint, $method = 'GET', $data = [], $con
|
||||
|
||||
/**
|
||||
* 获取VPS列表
|
||||
* @param int $configId 配置ID
|
||||
* @param int $page 页码
|
||||
* @param int $limit 每页数量
|
||||
* @return array|null VPS列表数据或null
|
||||
*/
|
||||
function mofangGetVpsList($configId, $page = 1, $limit = 100) {
|
||||
$db = getVpsDB();
|
||||
@@ -252,10 +227,6 @@ function mofangGetVpsList($configId, $page = 1, $limit = 100) {
|
||||
|
||||
/**
|
||||
* 获取VPS状态
|
||||
* @param int $configId 配置ID
|
||||
* @param int $vpsId VPS ID
|
||||
* @param bool $updateDb 是否更新数据库,默认true
|
||||
* @return array|null 状态数据或null
|
||||
*/
|
||||
function mofangGetVpsStatus($configId, $vpsId, $updateDb = true) {
|
||||
$db = getVpsDB();
|
||||
@@ -271,32 +242,57 @@ function mofangGetVpsStatus($configId, $vpsId, $updateDb = true) {
|
||||
// 如果获取成功且需要更新数据库
|
||||
if ($updateDb && $result && isset($result['status']) && $result['status'] === 200 && isset($result['data'])) {
|
||||
$statusData = $result['data'];
|
||||
$status = $statusData['status'] ?? 'unknown';
|
||||
$rawStatus = $statusData['status'] ?? 'unknown';
|
||||
|
||||
// 状态映射:将魔方平台的原始状态转换为标准状态
|
||||
$statusMap = [
|
||||
'on' => 'on', // 开机
|
||||
'off' => 'off', // 关机
|
||||
'running' => 'on', // 运行中
|
||||
'stopped' => 'off', // 已停止
|
||||
'process' => 'process', // 处理中(开机/关机/重启过程中)
|
||||
'pending' => 'process', // 等待中
|
||||
'installing' => 'process', // 安装中
|
||||
'rebooting' => 'process', // 重启中
|
||||
'unknown' => 'unknown' // 未知
|
||||
];
|
||||
|
||||
$status = $statusMap[$rawStatus] ?? $rawStatus;
|
||||
|
||||
// 只更新status字段,不影响其他字段
|
||||
$listDb = getVpsListDB();
|
||||
|
||||
// 先检查记录是否存在
|
||||
$existing = $listDb->queryOne(
|
||||
'SELECT id FROM vps_list WHERE config_id = ? AND vps_id = ?',
|
||||
// 先获取该VPS的IP地址,用于精确匹配
|
||||
$vpsInfo = $listDb->queryOne(
|
||||
'SELECT id, ip_address FROM vps_list WHERE config_id = ? AND vps_id = ?',
|
||||
[$configId, $vpsId]
|
||||
);
|
||||
|
||||
if ($existing) {
|
||||
if ($vpsInfo) {
|
||||
// 记录存在,执行UPDATE
|
||||
$listDb->execute(
|
||||
'UPDATE vps_list SET status = ?, last_check = CURRENT_TIMESTAMP WHERE config_id = ? AND vps_id = ?',
|
||||
[$status, $configId, $vpsId]
|
||||
$success = $listDb->execute(
|
||||
'UPDATE vps_list SET status = ?, last_check = CURRENT_TIMESTAMP WHERE id = ?',
|
||||
[$status, $vpsInfo['id']]
|
||||
);
|
||||
error_log("[mofangGetVpsStatus] VPS {$vpsId} 状态已更新为: {$status}");
|
||||
if ($success) {
|
||||
Logger::info("[mofangGetVpsStatus] VPS {$vpsId} (IP: {$vpsInfo['ip_address']}) 状态已更新为: {$status}", 'mofangGetVpsStatus');
|
||||
} else {
|
||||
Logger::error("[mofangGetVpsStatus] VPS {$vpsId} 状态更新失败", 'mofangGetVpsStatus');
|
||||
}
|
||||
} else {
|
||||
// 记录不存在,插入新记录
|
||||
$listDb->execute(
|
||||
$success = $listDb->execute(
|
||||
'INSERT INTO vps_list (config_id, vps_id, status, last_check) VALUES (?, ?, ?, CURRENT_TIMESTAMP)',
|
||||
[$configId, $vpsId, $status]
|
||||
);
|
||||
error_log("[mofangGetVpsStatus] VPS {$vpsId} 新记录已插入,状态: {$status}");
|
||||
if ($success) {
|
||||
Logger::info("[mofangGetVpsStatus] VPS {$vpsId} 新记录已插入,状态: {$status}", 'mofangGetVpsStatus');
|
||||
} else {
|
||||
Logger::error("[mofangGetVpsStatus] VPS {$vpsId} 新记录插入失败", 'mofangGetVpsStatus');
|
||||
}
|
||||
}
|
||||
} elseif ($updateDb && (!$result || !isset($result['status']) || $result['status'] !== 200)) {
|
||||
// API调用失败,不更新数据库,保留原有状态
|
||||
Logger::warning("[mofangGetVpsStatus] VPS {$vpsId} API调用失败,保留原有状态", 'mofangGetVpsStatus');
|
||||
}
|
||||
|
||||
return $result;
|
||||
@@ -304,10 +300,6 @@ function mofangGetVpsStatus($configId, $vpsId, $updateDb = true) {
|
||||
|
||||
/**
|
||||
* 获取VPS详细信息
|
||||
* @param int $configId 配置ID
|
||||
* @param int $vpsId VPS ID
|
||||
* @param bool $updateDb 是否更新数据库,默认true
|
||||
* @return array|null 详细信息或null
|
||||
*/
|
||||
function mofangGetVpsDetails($configId, $vpsId, $updateDb = true) {
|
||||
$db = getVpsDB();
|
||||
@@ -328,6 +320,28 @@ function mofangGetVpsDetails($configId, $vpsId, $updateDb = true) {
|
||||
$updates = []; // 存储要更新的字段
|
||||
$values = [];
|
||||
|
||||
// 提取开关机状态(如果有)
|
||||
if (isset($host['status'])) {
|
||||
$rawStatus = $host['status'];
|
||||
|
||||
// 状态映射:将魔方平台的原始状态转换为标准状态
|
||||
$statusMap = [
|
||||
'on' => 'on', // 开机
|
||||
'off' => 'off', // 关机
|
||||
'running' => 'on', // 运行中
|
||||
'stopped' => 'off', // 已停止
|
||||
'process' => 'process', // 处理中(开机/关机/重启过程中)
|
||||
'pending' => 'process', // 等待中
|
||||
'installing' => 'process', // 安装中
|
||||
'rebooting' => 'process', // 重启中
|
||||
'unknown' => 'unknown' // 未知
|
||||
];
|
||||
|
||||
$status = $statusMap[$rawStatus] ?? $rawStatus;
|
||||
$updates[] = 'status = ?';
|
||||
$values[] = $status;
|
||||
}
|
||||
|
||||
if (isset($host['config_option']) && is_array($host['config_option'])) {
|
||||
foreach ($host['config_option'] as $option) {
|
||||
switch ($option['key']) {
|
||||
@@ -392,7 +406,7 @@ function mofangGetVpsDetails($configId, $vpsId, $updateDb = true) {
|
||||
"UPDATE vps_list SET {$setClause} WHERE config_id = ? AND vps_id = ?",
|
||||
$values
|
||||
);
|
||||
error_log("[mofangGetVpsDetails] VPS {$vpsId} 详细信息已更新");
|
||||
Logger::info("[mofangGetVpsDetails] VPS {$vpsId} 详细信息已更新" . (isset($status) ? ",状态: {$status}" : ""), 'mofangGetVpsDetails');
|
||||
} else {
|
||||
// 记录不存在,插入新记录(只插入获取到的字段)
|
||||
$columns = [];
|
||||
@@ -422,7 +436,7 @@ function mofangGetVpsDetails($configId, $vpsId, $updateDb = true) {
|
||||
"INSERT INTO vps_list ({$columnStr}) VALUES ({$placeholderStr})",
|
||||
$insertValues
|
||||
);
|
||||
error_log("[mofangGetVpsDetails] VPS {$vpsId} 新记录已插入");
|
||||
Logger::info("[mofangGetVpsDetails] VPS {$vpsId} 新记录已插入", 'mofangGetVpsDetails');
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -433,9 +447,6 @@ function mofangGetVpsDetails($configId, $vpsId, $updateDb = true) {
|
||||
|
||||
/**
|
||||
* VPS开机
|
||||
* @param int $configId 配置ID
|
||||
* @param int $vpsId VPS ID
|
||||
* @return array|null 响应数据或null
|
||||
*/
|
||||
function mofangPowerOn($configId, $vpsId) {
|
||||
$db = getVpsDB();
|
||||
@@ -446,14 +457,19 @@ function mofangPowerOn($configId, $vpsId) {
|
||||
}
|
||||
|
||||
$endpoint = "/v1/hosts/{$vpsId}/module/on";
|
||||
return mofangApiRequest($config['site_url'], $endpoint, 'PUT', [], $configId);
|
||||
$result = mofangApiRequest($config['site_url'], $endpoint, 'PUT', [], $configId);
|
||||
|
||||
// 开机操作后立即获取状态并更新数据库
|
||||
if ($result && isset($result['status']) && $result['status'] === 200) {
|
||||
sleep(2); // 等待2秒让状态生效
|
||||
mofangGetVpsStatus($configId, $vpsId, true);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* VPS关机
|
||||
* @param int $configId 配置ID
|
||||
* @param int $vpsId VPS ID
|
||||
* @return array|null 响应数据或null
|
||||
*/
|
||||
function mofangPowerOff($configId, $vpsId) {
|
||||
$db = getVpsDB();
|
||||
@@ -464,14 +480,19 @@ function mofangPowerOff($configId, $vpsId) {
|
||||
}
|
||||
|
||||
$endpoint = "/v1/hosts/{$vpsId}/module/off";
|
||||
return mofangApiRequest($config['site_url'], $endpoint, 'PUT', [], $configId);
|
||||
$result = mofangApiRequest($config['site_url'], $endpoint, 'PUT', [], $configId);
|
||||
|
||||
// 关机操作后立即获取状态并更新数据库
|
||||
if ($result && isset($result['status']) && $result['status'] === 200) {
|
||||
sleep(2); // 等待2秒让状态生效
|
||||
mofangGetVpsStatus($configId, $vpsId, true);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* VPS硬重启
|
||||
* @param int $configId 配置ID
|
||||
* @param int $vpsId VPS ID
|
||||
* @return array|null 响应数据或null
|
||||
*/
|
||||
function mofangHardReboot($configId, $vpsId) {
|
||||
$db = getVpsDB();
|
||||
@@ -482,13 +503,19 @@ function mofangHardReboot($configId, $vpsId) {
|
||||
}
|
||||
|
||||
$endpoint = "/v1/hosts/{$vpsId}/module/hard_reboot";
|
||||
return mofangApiRequest($config['site_url'], $endpoint, 'PUT', [], $configId);
|
||||
$result = mofangApiRequest($config['site_url'], $endpoint, 'PUT', [], $configId);
|
||||
|
||||
// 硬重启操作后立即获取状态并更新数据库
|
||||
if ($result && isset($result['status']) && $result['status'] === 200) {
|
||||
sleep(3); // 等待3秒让重启生效
|
||||
mofangGetVpsStatus($configId, $vpsId, true);
|
||||
}
|
||||
|
||||
return $result;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新指定配置的VPS列表并保存到数据库
|
||||
* @param int $configId 配置ID
|
||||
* @return bool 是否成功
|
||||
*/
|
||||
function refreshVpsListForConfig($configId) {
|
||||
$db = getVpsDB();
|
||||
@@ -502,7 +529,7 @@ function refreshVpsListForConfig($configId) {
|
||||
$result = mofangGetVpsList($configId);
|
||||
|
||||
if (!$result || !isset($result['status']) || $result['status'] !== 200) {
|
||||
error_log("获取VPS列表失败: " . ($result['msg'] ?? '未知错误'));
|
||||
Logger::error("获取VPS列表失败: " . ($result['msg'] ?? '未知错误'), 'refreshVpsListForConfig');
|
||||
return false;
|
||||
}
|
||||
|
||||
@@ -559,40 +586,71 @@ function refreshVpsListForConfig($configId) {
|
||||
}
|
||||
}
|
||||
|
||||
// 使用INSERT OR REPLACE防止重复数据
|
||||
// 注意:需要保留原有的status字段,避免被覆盖为NULL
|
||||
// 基于vps_id和ip_address去重
|
||||
$existing = $listDb->queryOne(
|
||||
'SELECT status FROM vps_list WHERE config_id = ? AND vps_id = ?',
|
||||
[$configId, $host['id']]
|
||||
'SELECT id, status FROM vps_list WHERE vps_id = ? AND ip_address = ?',
|
||||
[$host['id'], $host['dedicatedip'] ?? null]
|
||||
);
|
||||
|
||||
$currentStatus = $existing ? $existing['status'] : null;
|
||||
|
||||
$listDb->execute(
|
||||
'INSERT OR REPLACE INTO vps_list (config_id, vps_id, domain, ip_address, product_name, cpu_cores, memory_size, disk_size, bandwidth, os_type, status, section, last_check) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, CURRENT_TIMESTAMP)',
|
||||
[
|
||||
$configId,
|
||||
$host['id'],
|
||||
$host['domain'] ?? null,
|
||||
$host['dedicatedip'] ?? null,
|
||||
$host['product_name'] ?? null,
|
||||
$cpuCores,
|
||||
$memorySize,
|
||||
$diskSize,
|
||||
$bandwidth,
|
||||
$osType,
|
||||
$currentStatus, // 保留原有状态
|
||||
$config['auto_monitor'] ? 1 : 0
|
||||
]
|
||||
);
|
||||
if ($existing) {
|
||||
// 记录已存在,只更新非status字段,保留原有status
|
||||
$listDb->execute(
|
||||
'UPDATE vps_list SET domain = ?, product_name = ?, cpu_cores = ?, memory_size = ?, disk_size = ?, bandwidth = ?, os_type = ?, amount = ?, nextduedate = ?, section = ?, last_check = CURRENT_TIMESTAMP WHERE id = ?',
|
||||
[
|
||||
$host['domain'] ?? null,
|
||||
$host['product_name'] ?? null,
|
||||
$cpuCores,
|
||||
$memorySize,
|
||||
$diskSize,
|
||||
$bandwidth,
|
||||
$osType,
|
||||
$host['amount'] ?? null,
|
||||
$host['nextduedate'] ?? null,
|
||||
$config['auto_monitor'] ? 1 : 0,
|
||||
$existing['id']
|
||||
]
|
||||
);
|
||||
} else {
|
||||
// 新记录,插入所有字段(status设为null,稍后通过状态接口获取)
|
||||
$listDb->execute(
|
||||
'INSERT INTO vps_list (config_id, vps_id, domain, ip_address, product_name, cpu_cores, memory_size, disk_size, bandwidth, os_type, amount, nextduedate, status, section, last_check) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?, NULL, ?, CURRENT_TIMESTAMP)',
|
||||
[
|
||||
$configId,
|
||||
$host['id'],
|
||||
$host['domain'] ?? null,
|
||||
$host['dedicatedip'] ?? null,
|
||||
$host['product_name'] ?? null,
|
||||
$cpuCores,
|
||||
$memorySize,
|
||||
$diskSize,
|
||||
$bandwidth,
|
||||
$osType,
|
||||
$host['amount'] ?? null,
|
||||
$host['nextduedate'] ?? null,
|
||||
$config['auto_monitor'] ? 1 : 0
|
||||
]
|
||||
);
|
||||
}
|
||||
}
|
||||
|
||||
// 刷新列表后,批量获取每个VPS的状态
|
||||
Logger::info("[refreshVpsListForConfig] 开始批量获取 {$configId} 配置下 " . count($hosts) . " 台VPS的状态", 'refreshVpsListForConfig');
|
||||
|
||||
foreach ($hosts as $host) {
|
||||
// 调用专门的状态接口获取开关机状态
|
||||
mofangGetVpsStatus($configId, $host['id'], true);
|
||||
|
||||
// 避免频繁请求,每次请求间隔0.5秒
|
||||
usleep(500000); // 500毫秒
|
||||
}
|
||||
|
||||
Logger::info("[refreshVpsListForConfig] 配置 {$configId} 的VPS列表和状态刷新完成", 'refreshVpsListForConfig');
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
/**
|
||||
* 刷新所有配置的VPS列表
|
||||
* @return int 成功刷新的配置数量
|
||||
*/
|
||||
function refreshAllVpsLists() {
|
||||
$db = getVpsDB();
|
||||
@@ -611,16 +669,13 @@ function refreshAllVpsLists() {
|
||||
|
||||
/**
|
||||
* 获取单个VPS的状态并更新到数据库
|
||||
* @param int $configId 配置ID
|
||||
* @param int $vpsId VPS ID
|
||||
* @return array|null 状态信息或null
|
||||
*/
|
||||
function updateVpsStatusToDb($configId, $vpsId) {
|
||||
$db = getVpsDB();
|
||||
$config = $db->queryOne('SELECT * FROM configs WHERE id = ?', [$configId]);
|
||||
|
||||
if (!$config) {
|
||||
error_log("配置ID {$configId} 不存在");
|
||||
Logger::error("配置ID {$configId} 不存在", 'updateVpsStatusToDb');
|
||||
return null;
|
||||
}
|
||||
|
||||
@@ -628,35 +683,60 @@ function updateVpsStatusToDb($configId, $vpsId) {
|
||||
$result = mofangGetVpsStatus($configId, $vpsId, false);
|
||||
|
||||
if (!$result || !isset($result['status']) || $result['status'] !== 200) {
|
||||
error_log("获取VPS {$vpsId} 状态失败: " . ($result['msg'] ?? '未知错误'));
|
||||
Logger::error("获取VPS {$vpsId} 状态失败,保留原有状态: " . ($result['msg'] ?? '未知错误'), 'updateVpsStatusToDb');
|
||||
return null;
|
||||
}
|
||||
|
||||
$statusData = $result['data'];
|
||||
$status = $statusData['status'] ?? 'unknown';
|
||||
$rawStatus = $statusData['status'] ?? 'unknown';
|
||||
$des = $statusData['des'] ?? '未知';
|
||||
|
||||
// 状态映射:将魔方平台的原始状态转换为标准状态
|
||||
$statusMap = [
|
||||
'on' => 'on', // 开机
|
||||
'off' => 'off', // 关机
|
||||
'running' => 'on', // 运行中
|
||||
'stopped' => 'off', // 已停止
|
||||
'process' => 'process', // 处理中(开机/关机/重启过程中)
|
||||
'pending' => 'process', // 等待中
|
||||
'installing' => 'process', // 安装中
|
||||
'rebooting' => 'process', // 重启中
|
||||
'unknown' => 'unknown' // 未知
|
||||
];
|
||||
|
||||
$status = $statusMap[$rawStatus] ?? $rawStatus;
|
||||
|
||||
// 检查记录是否存在
|
||||
$listDb = getVpsListDB();
|
||||
$existing = $listDb->queryOne(
|
||||
'SELECT id FROM vps_list WHERE config_id = ? AND vps_id = ?',
|
||||
$vpsInfo = $listDb->queryOne(
|
||||
'SELECT id, ip_address FROM vps_list WHERE config_id = ? AND vps_id = ?',
|
||||
[$configId, $vpsId]
|
||||
);
|
||||
|
||||
if ($existing) {
|
||||
if ($vpsInfo) {
|
||||
// 记录存在,执行UPDATE
|
||||
$listDb->execute(
|
||||
'UPDATE vps_list SET status = ?, last_check = CURRENT_TIMESTAMP WHERE config_id = ? AND vps_id = ?',
|
||||
[$status, $configId, $vpsId]
|
||||
$success = $listDb->execute(
|
||||
'UPDATE vps_list SET status = ?, last_check = CURRENT_TIMESTAMP WHERE id = ?',
|
||||
[$status, $vpsInfo['id']]
|
||||
);
|
||||
error_log("[updateVpsStatusToDb] VPS {$vpsId} 状态已更新为: {$status}");
|
||||
if ($success) {
|
||||
Logger::info("[updateVpsStatusToDb] VPS {$vpsId} (IP: {$vpsInfo['ip_address']}) 状态已更新为: {$status}", 'updateVpsStatusToDb');
|
||||
} else {
|
||||
Logger::error("[updateVpsStatusToDb] VPS {$vpsId} 状态更新失败,保留原有状态", 'updateVpsStatusToDb');
|
||||
return null;
|
||||
}
|
||||
} else {
|
||||
// 记录不存在,插入新记录
|
||||
$listDb->execute(
|
||||
$success = $listDb->execute(
|
||||
'INSERT INTO vps_list (config_id, vps_id, status, last_check) VALUES (?, ?, ?, CURRENT_TIMESTAMP)',
|
||||
[$configId, $vpsId, $status]
|
||||
);
|
||||
error_log("[updateVpsStatusToDb] VPS {$vpsId} 新记录已插入,状态: {$status}");
|
||||
if ($success) {
|
||||
Logger::info("[updateVpsStatusToDb] VPS {$vpsId} 新记录已插入,状态: {$status}", 'updateVpsStatusToDb');
|
||||
} else {
|
||||
Logger::error("[updateVpsStatusToDb] VPS {$vpsId} 新记录插入失败", 'updateVpsStatusToDb');
|
||||
return null;
|
||||
}
|
||||
}
|
||||
|
||||
return [
|
||||
@@ -669,9 +749,6 @@ function updateVpsStatusToDb($configId, $vpsId) {
|
||||
|
||||
/**
|
||||
* 批量获取VPS状态并更新到数据库
|
||||
* @param int $configId 配置ID
|
||||
* @param array $vpsIds VPS ID数组,为空则更新该配置下所有VPS
|
||||
* @return array 更新结果统计
|
||||
*/
|
||||
function batchUpdateVpsStatus($configId, $vpsIds = []) {
|
||||
$db = getVpsDB();
|
||||
@@ -696,21 +773,33 @@ function batchUpdateVpsStatus($configId, $vpsIds = []) {
|
||||
$failedCount = 0;
|
||||
$results = [];
|
||||
|
||||
foreach ($vpsIds as $vpsId) {
|
||||
$result = updateVpsStatusToDb($configId, $vpsId);
|
||||
|
||||
if ($result) {
|
||||
$successCount++;
|
||||
$results[] = $result;
|
||||
} else {
|
||||
$failedCount++;
|
||||
$results[] = [
|
||||
'vps_id' => $vpsId,
|
||||
'status' => 'error',
|
||||
'des' => '获取状态失败',
|
||||
'updated' => false
|
||||
];
|
||||
$listDb = getVpsListDB();
|
||||
$listDb->getConnection()->beginTransaction();
|
||||
|
||||
try {
|
||||
foreach ($vpsIds as $vpsId) {
|
||||
$result = updateVpsStatusToDb($configId, $vpsId);
|
||||
|
||||
if ($result) {
|
||||
$successCount++;
|
||||
$results[] = $result;
|
||||
} else {
|
||||
$failedCount++;
|
||||
$results[] = [
|
||||
'vps_id' => $vpsId,
|
||||
'status' => 'error',
|
||||
'des' => '获取状态失败',
|
||||
'updated' => false
|
||||
];
|
||||
}
|
||||
}
|
||||
|
||||
$listDb->getConnection()->commit();
|
||||
Logger::info("[batchUpdateVpsStatus] 批量更新完成: 成功{$successCount}, 失败{$failedCount}", 'batchUpdateVpsStatus');
|
||||
} catch (Exception $e) {
|
||||
$listDb->getConnection()->rollBack();
|
||||
Logger::error("[batchUpdateVpsStatus] 批量更新失败,已回滚: " . $e->getMessage(), 'batchUpdateVpsStatus');
|
||||
return ['success' => 0, 'failed' => count($vpsIds), 'error' => '批量更新失败: ' . $e->getMessage()];
|
||||
}
|
||||
|
||||
return [
|
||||
|
||||
Reference in new issue
Block a user