| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899 | <?phpnamespace App\Console\Commands;use App\Http\Models\Site;use GuzzleHttp\Client;use GuzzleHttp\Pool;use Illuminate\Console\Command;use GuzzleHttp\Psr7\Response;use Illuminate\Support\Facades\Log;class Beg extends Command{    /**     * The name and signature of the console command.     *     * @var string     */    protected $signature = 'beg:master {type}';    /**     * The console command description.     *     * @var string     */    protected $description = 'Command description';    /**     * Create a new command instance.     *     * @return void     */    public function __construct()    {        parent::__construct();    }    const URL_MAP = [        'email' => 'queue/mail',        'report' => 'queue/report'    ];    /**     * Execute the console command.     *     * @return mixed     */    public function handle()    {        $type = $this->argument('type');        if (!in_array($type, ['email', 'report'])) {            $this->error('参数必须为email 或 report');            return;        }        $taskUrls = Site::query()->whereIn('status', [2, 3, 5, 6, 8, 9])->get()->pluck('api_url')->filter()->toArray();//        $http = new Client([//            'verify' => false,//            'timeout' => 60//        ]);//        $taskUrls->each(function ($item) use ($http, $type) {////            try {//                $http->get(sprintf('%s%s', $item, self::URL_MAP[$type]));//            } catch (\Throwable $throwable) {//                Log::notice(var_export($throwable->getMessage(), 1));//            }//        });        $client = new Client([            'verify' => false,            'timeout' => 25        ]); //并发请求链接地址        $requests = function () use ($client, $taskUrls, $type) {            foreach ($taskUrls as $item) {                if (empty($item))                    continue;                yield new \GuzzleHttp\Psr7\Request('GET', $item . self::URL_MAP[$type]);            }        };        $pool = new Pool($client, $requests(), [            'concurrency' => 10, //同时并发抓取几个            'fulfilled' => function (Response $response, $index) {            },            'rejected' => function (\Throwable $throwable, $index) {//                $this->error($index . var_export($throwable->getMessage(), 1));                Log::error(var_export($throwable->getMessage(), 1));            },        ]);        $promise = $pool->promise();        $promise->wait();        $this->info('done');        return;    }}
 |