Files
ismysiteup.online/app/Console/Commands/MonitorUptimeCommand.php
T

66 lines
1.7 KiB
PHP
Raw Normal View History

2026-01-17 13:46:20 +02:00
<?php
namespace App\Console\Commands;
use App\Jobs\CheckUptime;
use App\Models\Site;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\Bus;
class MonitorUptimeCommand extends Command
{
/**
* The name and signature of the console command.
*
* @var string
*/
protected $signature = 'monitor:uptime';
/**
* The console command description.
*
* @var string
*/
protected $description = 'Check uptime for all enabled sites';
/**
* Execute the console command.
*/
public function handle()
{
$this->info('Starting uptime monitoring...');
// Fetch all sites with monitoring enabled
$sites = Site::where('monitoring_enabled', true)->get();
if ($sites->isEmpty()) {
$this->warn('No enabled sites found for monitoring.');
return Command::SUCCESS;
}
$this->info("Found {$sites->count()} enabled site(s) to monitor.");
// Create a batch of jobs to dispatch in parallel
$jobs = $sites->map(function (Site $site) {
return new CheckUptime($site);
});
// Dispatch jobs using Laravel's bus with concurrency limit
Bus::chain([
function () use ($jobs) {
// Using bus batch for parallel processing
Bus::batch($jobs->toArray())
->name('Uptime Monitoring')
->allowFailures()
->onQueue('monitoring')
->dispatch();
}
])->dispatch();
$this->info("Dispatched {$jobs->count()} uptime check job(s).");
$this->comment('Jobs are being processed in the queue...');
return Command::SUCCESS;
}
}