Files
2025-11-30 11:57:22 +01:00

84 lines
2.6 KiB
PHP

<?php
namespace Paymenter\Extensions\Others\UptimeLab\Console\Commands;
use Illuminate\Console\Command;
use Paymenter\Extensions\Others\UptimeLab\Services\UptimeMonitoringService;
class CheckUptimeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'uptime:check {--server= : Check specific server by ID}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check uptime status of all servers or a specific server';
/**
* Execute the console command.
*/
public function handle()
{
$monitoringService = new UptimeMonitoringService();
if ($serverId = $this->option('server')) {
$this->checkSpecificServer($serverId, $monitoringService);
} else {
$this->checkAllServers($monitoringService);
}
}
/**
* Check all active servers
*/
private function checkAllServers(UptimeMonitoringService $monitoringService): void
{
$this->info('Starting uptime check for all active servers...');
try {
$monitoringService->checkAllServers();
$this->info('Uptime check completed successfully!');
} catch (\Exception $e) {
$this->error('Uptime check failed: ' . $e->getMessage());
}
}
/**
* Check a specific server
*/
private function checkSpecificServer(string $serverId, UptimeMonitoringService $monitoringService): void
{
$this->info("Starting uptime check for server ID: {$serverId}...");
try {
$server = \Paymenter\Extensions\Others\UptimeLab\Models\UptimeServer::find($serverId);
if (!$server) {
$this->error("Server with ID {$serverId} not found.");
return;
}
$checkResult = $monitoringService->checkServerStatus($server);
$monitoringService->updateServerStatus($server, $checkResult);
$status = $checkResult['online'] ? 'ONLINE' : 'OFFLINE';
$responseTime = $checkResult['response_time'] ? $checkResult['response_time'] . 'ms' : 'N/A';
$this->info("Server: {$server->name}");
$this->info("Status: {$status}");
$this->info("Response Time: {$responseTime}");
$this->info("Uptime check completed successfully!");
} catch (\Exception $e) {
$this->error('Uptime check failed: ' . $e->getMessage());
}
}
}