爬虫开发
This commit is contained in:
39
app/Console/Commands/CrawlerRetryFailedCommand.php
Normal file
39
app/Console/Commands/CrawlerRetryFailedCommand.php
Normal file
@@ -0,0 +1,39 @@
|
||||
<?php
|
||||
|
||||
declare(strict_types=1);
|
||||
|
||||
namespace App\Console\Commands;
|
||||
|
||||
use App\Enums\CrawlTriggerType;
|
||||
use App\Jobs\RunCrawlRuleJob;
|
||||
use App\Models\CrawlRun;
|
||||
use Illuminate\Console\Command;
|
||||
|
||||
class CrawlerRetryFailedCommand extends Command
|
||||
{
|
||||
protected $signature = 'crawler:retry-failed {runId : 待重试的运行ID} {--sync : 同步执行,不走队列}';
|
||||
|
||||
protected $description = '重试失败的采集运行(按原规则重跑)';
|
||||
|
||||
public function handle(): int
|
||||
{
|
||||
$run = CrawlRun::query()->with('rule')->find((int) $this->argument('runId'));
|
||||
|
||||
if (! $run instanceof CrawlRun || $run->rule === null) {
|
||||
$this->error('运行记录不存在或规则已删除');
|
||||
|
||||
return self::FAILURE;
|
||||
}
|
||||
|
||||
if ((bool) $this->option('sync')) {
|
||||
RunCrawlRuleJob::dispatchSync($run->rule_id, CrawlTriggerType::Retry->value, null, $run->id);
|
||||
} else {
|
||||
RunCrawlRuleJob::dispatch($run->rule_id, CrawlTriggerType::Retry->value, null, $run->id);
|
||||
}
|
||||
|
||||
$this->info(sprintf('已提交重试任务,规则 #%d %s', $run->rule_id, $run->rule->name));
|
||||
|
||||
return self::SUCCESS;
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user