Files
2026-01-17 16:42:43 +02:00

91 lines
2.3 KiB
PHP

<?php
namespace App\Console\Commands;
use App\Jobs\CheckSitePing;
use App\Models\Site;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Log;
class MonitorPing extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monitor:ping
{--site= : Specific site ID to ping}
{--sync : Run synchronously instead of dispatching jobs}';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Ping all enabled sites to check their uptime status';
/**
* Execute the console command.
*/
public function handle(): int
{
$this->info('Starting site ping monitoring...');
$query = Site::where('monitoring_enabled', true);
if ($siteId = $this->option('site')) {
$query->where('id', $siteId);
}
$sites = $query->get();
if ($sites->isEmpty()) {
$this->warn('No enabled sites found to monitor.');
return self::SUCCESS;
}
$this->info("Found {$sites->count()} site(s) to ping.");
$progressBar = $this->output->createProgressBar($sites->count());
$progressBar->start();
$dispatched = 0;
$failed = 0;
foreach ($sites as $site) {
try {
if ($this->option('sync')) {
// Run synchronously for testing/debugging
CheckSitePing::dispatchSync($site);
} else {
// Dispatch to queue
CheckSitePing::dispatch($site);
}
$dispatched++;
} catch (\Exception $e) {
$failed++;
Log::error('MonitorPing: Failed to dispatch job', [
'site_id' => $site->id,
'error' => $e->getMessage(),
]);
}
$progressBar->advance();
}
$progressBar->finish();
$this->newLine(2);
$this->info("Successfully dispatched {$dispatched} ping job(s).");
if ($failed > 0) {
$this->error("Failed to dispatch {$failed} job(s).");
return self::FAILURE;
}
return self::SUCCESS;
}
}