info('开始执行第三方服务健康检查...');
try {
$serviceId = $this->getServiceId();
$results = MonitorService::performHealthCheck($serviceId);
$this->displayResults($results);
$this->displaySummary($results);
return Command::SUCCESS;
} catch (\Exception $e) {
$this->error("健康检查失败: {$e->getMessage()}");
return Command::FAILURE;
}
}
/**
* 获取要检查的服务ID
*
* @return int|null
*/
protected function getServiceId(): ?int
{
$serviceOption = $this->option('service');
if (!$serviceOption) {
return null;
}
// 如果是数字,直接返回
if (is_numeric($serviceOption)) {
$service = ThirdPartyService::find($serviceOption);
if (!$service) {
throw new \Exception("服务ID {$serviceOption} 不存在");
}
return (int)$serviceOption;
}
// 如果是代码,查找对应的服务
$service = ThirdPartyService::where('code', $serviceOption)->first();
if (!$service) {
throw new \Exception("服务代码 {$serviceOption} 不存在");
}
return $service->id;
}
/**
* 显示检查结果
*
* @param array $results
* @return void
*/
protected function displayResults(array $results): void
{
if (empty($results)) {
$this->warn('没有找到需要检查的服务');
return;
}
$this->info("\n检查结果:");
$this->line(str_repeat('-', 80));
foreach ($results as $result) {
$this->displaySingleResult($result);
}
}
/**
* 显示单个服务的检查结果
*
* @param array $result
* @return void
*/
protected function displaySingleResult(array $result): void
{
$status = $result['status'];
$serviceName = $result['service_name'];
$serviceCode = $result['service_code'];
// 根据状态选择颜色
$color = match ($status) {
'success' => 'green',
'warning' => 'yellow',
'error', 'timeout' => 'red',
default => 'white',
};
$this->line("[{$status}] {$serviceName} ({$serviceCode})");
if ($this->option('verbose')) {
if ($result['response_time']) {
$this->line(" 响应时间: {$result['response_time']}ms");
}
if ($result['status_code']) {
$this->line(" HTTP状态码: {$result['status_code']}");
}
if ($result['error_message']) {
$this->line(" 错误信息: {$result['error_message']}");
}
if (!empty($result['details'])) {
$this->line(" 详细信息:");
foreach ($result['details'] as $key => $value) {
if (is_array($value)) {
$value = json_encode($value, JSON_UNESCAPED_UNICODE);
}
$this->line(" {$key}: {$value}");
}
}
} else {
if ($result['error_message']) {
$this->line(" {$result['error_message']}");
}
}
$this->line('');
}
/**
* 显示检查摘要
*
* @param array $results
* @return void
*/
protected function displaySummary(array $results): void
{
if (empty($results)) {
return;
}
$total = count($results);
$successful = count(array_filter($results, fn($r) => $r['status'] === 'success'));
$warnings = count(array_filter($results, fn($r) => $r['status'] === 'warning'));
$errors = count(array_filter($results, fn($r) => in_array($r['status'], ['error', 'timeout'])));
$this->line(str_repeat('-', 80));
$this->info('检查摘要:');
$this->line("总计: {$total}");
$this->line("成功: {$successful}");
if ($warnings > 0) {
$this->line("警告: {$warnings}");
}
if ($errors > 0) {
$this->line("错误: {$errors}");
}
$successRate = $total > 0 ? round(($successful / $total) * 100, 2) : 0;
$this->line("成功率: {$successRate}%");
// 如果有失败的服务,给出建议
if ($errors > 0) {
$this->line('');
$this->warn('建议:');
$this->line('- 检查失败服务的配置和凭证');
$this->line('- 确认第三方服务是否正常运行');
$this->line('- 查看详细日志了解具体错误原因');
}
}
/**
* 获取要检查的服务列表
*
* @return \Illuminate\Database\Eloquent\Collection
*/
protected function getServicesToCheck()
{
$query = ThirdPartyService::query();
if ($this->option('active-only')) {
$query->where('status', 'ACTIVE');
}
if ($this->option('all')) {
return $query->get();
}
// 默认只检查有健康检查URL的激活服务
return $query->where('status', 'ACTIVE')
->whereNotNull('health_check_url')
->get();
}
}