MqTask.php 1.2 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Http\Api\Mq;
  4. use App\Tools\Tools;
  5. use Illuminate\Console\Command;
  6. class MqTask extends Command
  7. {
  8. /**
  9. * The name and signature of the console command.
  10. *
  11. * @var string
  12. */
  13. protected $signature = 'mq:task';
  14. /**
  15. * The console command description.
  16. *
  17. * @var string
  18. */
  19. protected $description = 'run task';
  20. /**
  21. * Create a new command instance.
  22. *
  23. * @return void
  24. */
  25. public function __construct()
  26. {
  27. parent::__construct();
  28. }
  29. /**
  30. * Execute the console command.
  31. *
  32. * @return int
  33. */
  34. public function handle()
  35. {
  36. if (Tools::isStop()) {
  37. return 0;
  38. }
  39. $this->info('env='.env('RABBITMQ_HOST'));
  40. $this->info('config='.config('queue.connections.rabbitmq.host'));
  41. $queue = 'task';
  42. $this->info(" [*] Waiting for {$queue}. To exit press CTRL+C");
  43. Mq::worker($queue, function ($message) {
  44. $message = json_decode(json_encode($message), true);
  45. $this->info('name=', $message['name']);
  46. return $this->call($message['name'], $message['param']);
  47. });
  48. return 0;
  49. }
  50. }