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
- {
-
- protected $signature = 'url:check';
-
- protected $description = 'check url';
-
- public function __construct()
- {
- parent::__construct();
- }
-
- 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;
- }
- }
|