Files
ai-web/app/Console/Commands/CrawlerRunCommand.php
cjd 260460df03
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
爬虫开发
2026-02-18 12:56:36 +08:00

65 lines
1.7 KiB
PHP
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
<?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;
}
}