65 lines
1.7 KiB
PHP
65 lines
1.7 KiB
PHP
|
|
<?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;
|
|||
|
|
}
|
|||
|
|
}
|
|||
|
|
|