65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
<?php
|
|||
|
|
|
||
|
|
namespace App\Console\Commands;
|
||
|
|
|
||
|
|
use App\Jobs\CheckBrokenLinks;
|
||
|
|
use App\Models\Site;
|
||
|
|
use Illuminate\Console\Command;
|
||
|
|
use Illuminate\Support\Facades\Bus;
|
||
|
|
|
||
|
|
class MonitorLinksCommand extends Command
|
||
|
|
{
|
||
|
|
/**
|
||
|
|
* The name and signature of the console command.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $signature = 'monitor:links';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* The console command description.
|
||
|
|
*
|
||
|
|
* @var string
|
||
|
|
*/
|
||
|
|
protected $description = 'Check for broken links on all enabled sites';
|
||
|
|
|
||
|
|
/**
|
||
|
|
* Execute the console command.
|
||
|
|
*/
|
||
|
|
public function handle()
|
||
|
|
{
|
||
|
|
$this->info('Starting broken link monitoring...');
|
||
|
|
|
||
|
|
// Fetch all sites with monitoring enabled
|
||
|
|
$sites = Site::where('monitoring_enabled', true)->get();
|
||
|
|
|
||
|
|
if ($sites->isEmpty()) {
|
||
|
|
$this->warn('No enabled sites found for broken link monitoring.');
|
||
|
|
return Command::SUCCESS;
|
||
|
|
}
|
||
|
|
|
||
|
|
$this->info("Found {$sites->count()} enabled site(s) for broken link monitoring.");
|
||
|
|
|
||
|
|
// Create a batch of jobs to dispatch in parallel
|
||
|
|
$jobs = $sites->map(function (Site $site) {
|
||
|
|
return new CheckBrokenLinks($site);
|
||
|
|
});
|
||
|
|
|
||
|
|
// Dispatch jobs using Laravel's bus with batch processing
|
||
|
|
Bus::chain([
|
||
|
|
function () use ($jobs) {
|
||
|
|
Bus::batch($jobs->toArray())
|
||
|
|
->name('Broken Link Monitoring')
|
||
|
|
->allowFailures()
|
||
|
|
->onQueue('monitoring')
|
||
|
|
->dispatch();
|
||
|
|
}
|
||
|
|
])->dispatch();
|
||
|
|
|
||
|
|
$this->info("Dispatched {$jobs->count()} broken link check job(s).");
|
||
|
|
$this->comment('Jobs are being processed in the queue...');
|
||
|
|
|
||
|
|
return Command::SUCCESS;
|
||
|
|
}
|
||
|
|
}
|