Beg.php 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Models\Site;
  4. use GuzzleHttp\Client;
  5. use GuzzleHttp\Pool;
  6. use Illuminate\Console\Command;
  7. use GuzzleHttp\Psr7\Response;
  8. use Illuminate\Support\Facades\Log;
  9. class Beg extends Command
  10. {
  11. /**
  12. * The name and signature of the console command.
  13. *
  14. * @var string
  15. */
  16. protected $signature = 'beg:master {type}';
  17. /**
  18. * The console command description.
  19. *
  20. * @var string
  21. */
  22. protected $description = 'Command description';
  23. /**
  24. * Create a new command instance.
  25. *
  26. * @return void
  27. */
  28. public function __construct()
  29. {
  30. parent::__construct();
  31. }
  32. const URL_MAP = [
  33. 'email' => 'queue/mail',
  34. 'report' => 'queue/report'
  35. ];
  36. /**
  37. * Execute the console command.
  38. *
  39. * @return mixed
  40. */
  41. public function handle()
  42. {
  43. $type = $this->argument('type');
  44. if (!in_array($type, ['email', 'report'])) {
  45. $this->error('参数必须为email 或 report');
  46. return;
  47. }
  48. $taskUrls = Site::query()->whereIn('status', [2, 3, 5, 6, 8, 9])->get()->pluck('api_url')->filter()->toArray();
  49. // $http = new Client([
  50. // 'verify' => false,
  51. // 'timeout' => 60
  52. // ]);
  53. // $taskUrls->each(function ($item) use ($http, $type) {
  54. //
  55. // try {
  56. // $http->get(sprintf('%s%s', $item, self::URL_MAP[$type]));
  57. // } catch (\Throwable $throwable) {
  58. // Log::notice(var_export($throwable->getMessage(), 1));
  59. // }
  60. // });
  61. $client = new Client([
  62. 'verify' => false,
  63. 'timeout' => 25
  64. ]); //并发请求链接地址
  65. $requests = function () use ($client, $taskUrls, $type) {
  66. foreach ($taskUrls as $item) {
  67. if (empty($item))
  68. continue;
  69. yield new \GuzzleHttp\Psr7\Request('GET', $item . self::URL_MAP[$type]);
  70. }
  71. };
  72. $pool = new Pool($client, $requests(), [
  73. 'concurrency' => 10, //同时并发抓取几个
  74. 'fulfilled' => function (Response $response, $index) {
  75. },
  76. 'rejected' => function (\Throwable $throwable, $index) {
  77. // $this->error($index . var_export($throwable->getMessage(), 1));
  78. Log::error(var_export($throwable->getMessage(), 1));
  79. },
  80. ]);
  81. $promise = $pool->promise();
  82. $promise->wait();
  83. $this->info('done');
  84. return;
  85. }
  86. }