爬虫开发
Some checks failed
Tests / PHP 8.2 (push) Has been cancelled
Tests / PHP 8.3 (push) Has been cancelled
Tests / PHP 8.4 (push) Has been cancelled

This commit is contained in:
cjd
2026-02-18 12:56:36 +08:00
parent a98bc6f13c
commit 260460df03
45 changed files with 4091 additions and 8 deletions

View File

@@ -0,0 +1,64 @@
<?php
declare(strict_types=1);
namespace App\Console\Commands;
use App\Enums\CrawlTriggerType;
use App\Jobs\RunCrawlRuleJob;
use App\Models\CrawlRule;
use App\Services\Crawler\CrawlRuleScheduleService;
use Illuminate\Console\Command;
class CrawlerRunCommand extends Command
{
protected $signature = 'crawler:run {ruleId? : 指定规则ID} {--all : 忽略cron执行全部启用规则} {--sync : 同步执行,不走队列}';
protected $description = '执行采集规则';
public function handle(CrawlRuleScheduleService $scheduleService): int
{
$ruleId = $this->argument('ruleId');
$query = CrawlRule::query()->where('enabled', true);
if ($ruleId !== null) {
$query->whereKey((int) $ruleId);
}
$rules = $query->orderBy('id')->get();
if ($rules->isEmpty()) {
$this->warn('没有可执行的采集规则');
return self::SUCCESS;
}
$shouldRunAll = (bool) $this->option('all') || $ruleId !== null;
$sync = (bool) $this->option('sync');
$dispatched = 0;
foreach ($rules as $rule) {
if (! $shouldRunAll && ! $scheduleService->isDue($rule)) {
continue;
}
if ($sync) {
RunCrawlRuleJob::dispatchSync($rule->id, CrawlTriggerType::Schedule->value);
} else {
RunCrawlRuleJob::dispatch($rule->id, CrawlTriggerType::Schedule->value);
}
$dispatched++;
$this->info(sprintf('已提交规则 #%d %s', $rule->id, $rule->name));
}
if ($dispatched === 0) {
$this->line('当前无到期规则');
}
return self::SUCCESS;
}
}