QueueForm.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146
  1. <?php
  2. namespace backend\forms;
  3. use components\Curl;
  4. use components\Exception;
  5. use Yii;
  6. /**
  7. * Class QueueForm
  8. * @package backend\forms
  9. */
  10. class QueueForm extends BaseForm
  11. {
  12. /**
  13. * @var string 名称
  14. */
  15. public $name;
  16. public $name1;
  17. public $name2;
  18. public function rules()
  19. {
  20. return [
  21. //create_queue delete_queue
  22. [['name'], 'required', 'message' => 1100, 'on' => ['create_queue', 'delete_queue']],
  23. [['name1','name2'], 'required', 'message' => 1100, 'on' => ['update_queue']],
  24. ['name', 'string', 'message' => 1101, 'on' => ['create_queue', 'delete_queue']],
  25. ['name', 'filter', 'filter' => 'trim', 'on' => ['create_queue', 'delete_queue']],
  26. ];
  27. }
  28. /**
  29. * [创建队列]
  30. * @author: libingke
  31. * @return array
  32. * @throws Exception
  33. */
  34. public function createQueue()
  35. {
  36. try {
  37. $connect = $this->getConnect();
  38. $channel = $connect->channel();
  39. list($queue,,) = $channel->queue_declare($this->name,
  40. false, true, false, false, false);
  41. } catch (\Exception $e) {
  42. throw new Exception(1001, $e->getMessage());
  43. }
  44. return $data = ['name' => $queue];
  45. }
  46. /**
  47. * [删除队列]
  48. * @author: libingke
  49. * @return array
  50. * @throws Exception
  51. */
  52. public function deleteQueue()
  53. {
  54. try {
  55. $connect = $this->getConnect();
  56. $channel = $connect->channel();
  57. $channel->queue_delete($this->name,
  58. false, false, false, false
  59. );
  60. return [
  61. 'name' => $this->name,
  62. 'result' => '删除成功'
  63. ];
  64. } catch (\Exception $e) {
  65. throw new Exception(1001, $e->getMessage());
  66. }
  67. }
  68. /**
  69. * [获取消息列表]
  70. * @author: libingke
  71. */
  72. public function getQueueList()
  73. {
  74. $authStr = Yii::$app->Amqp->user . ':' . Yii::$app->Amqp->pass;
  75. $url = Yii::$app->Amqp->host . ':' . Yii::$app->Amqp->api_port . "/api/queues";
  76. $curl = new Curl();
  77. $curl->setOption(CURLOPT_USERPWD, $authStr);
  78. $result = json_decode($curl->get($url), true);
  79. if ($curl->responseCode != 200)
  80. throw new Exception(1002);
  81. if ($curl->errorText)
  82. throw new Exception(1002, $curl->errorText);
  83. if (isset($result['error']) && is_string($result['error']))
  84. throw new Exception(1002, $result['error']);
  85. $rows = [];
  86. foreach ($result as $k => $v) {
  87. //$name = $v['name'];
  88. $name = $v['name'];
  89. $rows[$name]['name'] = $name;
  90. $rows[$name]['messages_count'] = $v['messages'];
  91. $rows[$name]['message_bytes'] = $v['message_bytes'];
  92. $rows[$name]['messages_ready'] = $v['messages_ready'];
  93. //$rows[$name]['message_stats'] = $v['message_stats'];
  94. $rows[$name]['consumers_count'] = $v['consumers'];
  95. $rows[$name]['auto_delete'] = $v['auto_delete'];
  96. $rows[$name]['durable'] = $v['durable'];
  97. //$rows[$name]['arguments'] = $v['arguments'];
  98. $rows[$name]['state'] = $v['state'];
  99. $rows[$name]['idle_since'] = $v['idle_since'];
  100. }
  101. unset($result);
  102. return ['count' => count($rows), 'rows' => $rows];
  103. }
  104. /**
  105. * [修改队列]
  106. * @author: hanguangxu
  107. * @return array
  108. * @throws Exception
  109. */
  110. public function updateQueue()
  111. {
  112. try {
  113. $connect = $this->getConnect();
  114. $channel = $connect->channel();
  115. $channel->queue_delete($this->name1,
  116. false, false, false, false
  117. );
  118. list($queue,,) = $channel->queue_declare($this->name2,
  119. false, true, false, false, false);
  120. } catch (\Exception $e) {
  121. throw new Exception(1001, $e->getMessage());
  122. }
  123. return $data = ['name' => $queue];
  124. }
  125. }