UrlCheck.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Site;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Pool;
  6. use GuzzleHttp\Psr7\Response;
  7. use Illuminate\Console\Command;
  8. use App\Http\Models\UrlCheck as UrlCheckModel;
  9. use Illuminate\Support\Collection;
  10. class UrlCheck extends Command
  11. {
  12. /**
  13. * The name and signature of the console command.
  14. *
  15. * @var string
  16. */
  17. protected $signature = 'url:check';
  18. /**
  19. * The console command description.
  20. *
  21. * @var string
  22. */
  23. protected $description = 'check url';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct()
  30. {
  31. parent::__construct();
  32. }
  33. /**
  34. * Execute the console command.
  35. *
  36. * @return mixed
  37. */
  38. public function handle()
  39. {
  40. UrlCheckModel::query()->truncate(); //截断表
  41. $taskUrls = Site::query()->whereIn('status', [2,3,5,8,9])->selectRaw('id as site_id,cn_title,webmaster_domain as domain')->limit(1)->get();
  42. $client = new Client([
  43. 'verify' => false,
  44. 'timeout' => 10
  45. ]); //并发请求链接地址
  46. $requests = function () use ($client, $taskUrls) {
  47. foreach ($taskUrls as $item) {
  48. if (empty($item->domain))
  49. continue;
  50. yield new \GuzzleHttp\Psr7\Request('GET',$item->domain);
  51. }
  52. };
  53. $failed = [];
  54. $pool = new Pool($client, $requests(), [
  55. 'concurrency' => 5, //同时并发抓取几个
  56. 'rejected' => function (\Throwable $throwable, $index) use (&$failed) {
  57. $failed[] = $index;
  58. },
  59. ]);
  60. $promise = $pool->promise();
  61. $promise->wait();
  62. $failedSites = collect([]);
  63. foreach ($taskUrls as $key => $item) {
  64. if (in_array($key, $failed)) {
  65. $item->setCreatedAt(date('Y-m-d H:i:s'));
  66. $item->setUpdatedAt(date('Y-m-d H:i:s'));
  67. $failedSites->push($item);
  68. }
  69. }
  70. //分批批量插入
  71. $failedSites->chunk(50)->each(function (Collection $item, $key) {
  72. UrlCheckModel::query()->insert($item->toArray());
  73. });
  74. $this->info('success');
  75. return;
  76. }
  77. }