12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788 |
- <?php
- namespace App\Console\Commands;
- use App\Http\Models\Site;
- use GuzzleHttp\Client;
- use GuzzleHttp\Pool;
- use GuzzleHttp\Psr7\Response;
- use Illuminate\Console\Command;
- use App\Http\Models\UrlCheck as UrlCheckModel;
- use Illuminate\Support\Collection;
- class UrlCheck extends Command
- {
- /**
- * The name and signature of the console command.
- *
- * @var string
- */
- protected $signature = 'url:check';
- /**
- * The console command description.
- *
- * @var string
- */
- protected $description = 'check url';
- /**
- * Create a new command instance.
- *
- * @return void
- */
- public function __construct()
- {
- parent::__construct();
- }
- /**
- * Execute the console command.
- *
- * @return mixed
- */
- public function handle()
- {
- UrlCheckModel::query()->truncate(); //截断表
- $taskUrls = Site::query()->whereIn('status', [2,3,5,8,9])->selectRaw('id as site_id,cn_title,webmaster_domain as domain')->limit(1)->get();
- $client = new Client([
- 'verify' => false,
- 'timeout' => 10
- ]); //并发请求链接地址
- $requests = function () use ($client, $taskUrls) {
- foreach ($taskUrls as $item) {
- if (empty($item->domain))
- continue;
- yield new \GuzzleHttp\Psr7\Request('GET',$item->domain);
- }
- };
- $failed = [];
- $pool = new Pool($client, $requests(), [
- 'concurrency' => 5, //同时并发抓取几个
- 'rejected' => function (\Throwable $throwable, $index) use (&$failed) {
- $failed[] = $index;
- },
- ]);
- $promise = $pool->promise();
- $promise->wait();
- $failedSites = collect([]);
- foreach ($taskUrls as $key => $item) {
- if (in_array($key, $failed)) {
- $item->setCreatedAt(date('Y-m-d H:i:s'));
- $item->setUpdatedAt(date('Y-m-d H:i:s'));
- $failedSites->push($item);
- }
- }
- //分批批量插入
- $failedSites->chunk(50)->each(function (Collection $item, $key) {
- UrlCheckModel::query()->insert($item->toArray());
- });
- $this->info('success');
- return;
- }
- }
|