UpdateNoticeRule.php 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. <?php
  2. namespace App\Console\Commands;
  3. use App\Repositories\NoticeRuleRepository;
  4. use Illuminate\Console\Command;
  5. use Illuminate\Support\Facades\Log;
  6. use PhpAmqpLib\Connection\AMQPStreamConnection;
  7. class UpdateNoticeRule extends Command
  8. {
  9. /**
  10. * The name and signature of the console command.
  11. *
  12. * @var string
  13. */
  14. protected $signature = 'notice:update_status';
  15. /**
  16. * The console command description.
  17. *
  18. * @var string
  19. */
  20. protected $description = '更新规则通知状态';
  21. protected $channel;
  22. protected $connection;
  23. protected $queue = 'update_status_notice_rule';
  24. /**
  25. * Create a new command instance.
  26. *
  27. * @return void
  28. */
  29. public function __construct(NoticeRuleRepository $noticeRuleRepository)
  30. {
  31. parent::__construct();
  32. $this->noticeRuleRepository = $noticeRuleRepository;
  33. $this->connection = $this->getConnection();
  34. $this->channel = $this->connection->channel();
  35. $this->channel->exchange_declare($this->queue, 'fanout', false, true, false);
  36. }
  37. public function getConnection()
  38. {
  39. $conn = false;
  40. if ($this->connection) {
  41. return $this->connection;
  42. }
  43. for ($i = 0; $i < 3; $i++) {
  44. $connection = $this->createConn();
  45. if ($connection) {
  46. $conn = $connection;
  47. break;
  48. }
  49. Log::info("create amqp conn retry=" . $i);
  50. }
  51. return $conn;
  52. }
  53. public function createConn()
  54. {
  55. try {
  56. $connection = new AMQPStreamConnection(env('MQ_HOST'), env('MQ_PORT'), env('MQ_USERNAME'), env('MQ_PWD'), env('MQ_VHOST'));
  57. } catch (\Exception $exception) {
  58. Log::info("AMQP connection Error" . $exception->getMessage());
  59. $connection = false;
  60. }
  61. return $connection;
  62. }
  63. /**
  64. * Execute the console command.
  65. *
  66. * @return mixed
  67. */
  68. public function handle()
  69. {
  70. Log::info('发送消息结果');
  71. $queue_name = $this->queue;
  72. $this->channel->queue_declare($queue_name, false, true, false, false);
  73. $this->channel->queue_bind($queue_name, $this->queue);
  74. $callback = function ($msg) {
  75. Log::info($msg->body);
  76. $data = \GuzzleHttp\json_decode($msg->body, true);
  77. $this->line('收到数据' . $msg->body);
  78. $res = $this->noticeRuleRepository->updateStatus($data);
  79. if($res){
  80. $msg->delivery_info['channel']->basic_ack($msg->delivery_info['delivery_tag']);
  81. }
  82. };
  83. $this->channel->basic_consume($queue_name, '', false, false, false, false, $callback);
  84. while (count($this->channel->callbacks)) {
  85. $this->channel->wait();
  86. }
  87. $this->channel->close();
  88. $this->connection->close();
  89. }
  90. }