iqueue.php 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. <?php
  2. declare(strict_types=1);
  3. namespace queue;
  4. require_once(BASE_ROOT_PATH . '/helper/performance_helper.php');
  5. use Redis;
  6. use Exception;
  7. use Log;
  8. class IQueueDB
  9. {
  10. private $_redis;
  11. private $_tb_prefix;
  12. //存定义存储表的数量,系统会随机分配存储
  13. private $_tb_num = 3;
  14. public function __construct($queue_name)
  15. {
  16. if ( !extension_loaded('redis') ) {
  17. throw_exception('redis failed to load');
  18. }
  19. $this->_tb_prefix = $queue_name;
  20. $this->_redis = new Redis();
  21. $this->_redis->connect(C('queue.host'), C('queue.port'), 20);
  22. $this->_redis->setOption(Redis::OPT_READ_TIMEOUT, 10);
  23. }
  24. public function rpush($value)
  25. {
  26. try {
  27. return $this->_redis->rPush($this->_tb_prefix.rand(1,$this->_tb_num),$value);
  28. } catch(Exception $e) {
  29. throw_exception($e->getMessage());
  30. }
  31. }
  32. public function lpush($value)
  33. {
  34. try {
  35. return $this->_redis->lPush($this->_tb_prefix.rand(1,$this->_tb_num),$value);
  36. } catch(Exception $e) {
  37. throw_exception($e->getMessage());
  38. }
  39. }
  40. public function scan()
  41. {
  42. $list_key = [];
  43. for($i=1;$i<=$this->_tb_num;$i++) {
  44. $list_key[] = $this->_tb_prefix.$i;
  45. }
  46. return $list_key;
  47. }
  48. public function rpop($key, $time)
  49. {
  50. $result = $this->_redis->brPop($key, $time);
  51. if ($result) {
  52. return $result[1];
  53. } else {
  54. return null;
  55. }
  56. }
  57. public function lpop($key, $time)
  58. {
  59. $result = $this->_redis->blPop($key, $time);
  60. if ($result) {
  61. return $result[1];
  62. } else {
  63. return null;
  64. }
  65. }
  66. public function clear() {
  67. $this->_redis->flushAll();
  68. }
  69. }
  70. /**
  71. * 队列处理
  72. *
  73. *
  74. * @package
  75. */
  76. class IClient
  77. {
  78. private $mQueuedb;
  79. public function __construct($queueDb)
  80. {
  81. $this->mQueuedb = $queueDb;
  82. }
  83. public function push($key, $value)
  84. {
  85. return $this->mQueuedb->lpush(serialize([$key=>$value]));
  86. }
  87. }
  88. class IServer
  89. {
  90. private $_queuedb;
  91. public function __construct($queueDb) {
  92. $this->_queuedb = $queueDb;
  93. }
  94. public function pop($key,$time)
  95. {
  96. $result = $this->_queuedb->rpop($key,$time);
  97. if($result != null) {
  98. return unserialize($result);
  99. } else {
  100. return false;
  101. }
  102. }
  103. public function lpop($key,$time)
  104. {
  105. $result = $this->_queuedb->lpop($key,$time);
  106. if($result != null) {
  107. return unserialize($result);
  108. } else {
  109. return false;
  110. }
  111. }
  112. public function scan() {
  113. return $this->_queuedb->scan();
  114. }
  115. }
  116. abstract class ILooper
  117. {
  118. private $_stop = false;
  119. private $mServer;
  120. protected function __construct($server)
  121. {
  122. $this->mServer = $server;
  123. }
  124. public function prepare()
  125. {
  126. if (ob_get_level()) ob_end_clean();
  127. pcntl_signal(SIGINT, [$this,'sig_handler']);
  128. pcntl_signal(SIGHUP, [$this,'sig_handler']);
  129. pcntl_signal(SIGQUIT, [$this,'sig_handler']);
  130. pcntl_signal(SIGTERM, [$this,'sig_handler']);
  131. }
  132. abstract protected function handle($msg);
  133. public function run()
  134. {
  135. $queues = $this->mServer->scan();
  136. while (true)
  137. {
  138. try
  139. {
  140. if ($this->_stop) break;
  141. $content = $this->mServer->pop($queues,1);
  142. perfor_clear();
  143. perfor_start();
  144. $this->handle($content);
  145. perfor_end('Handle Request');
  146. $info = perfor_log();
  147. Log::record("{$info} \r\n\r\n",Log::DEBUG);
  148. }
  149. catch (Exception $e)
  150. {
  151. $err = $e->getMessage();
  152. $code = $e->getCode();
  153. Log::record("QueueDB pop err: code={$code} err={$err}",Log::ERR);
  154. }
  155. }
  156. }
  157. private function sig_handler($signo)
  158. {
  159. Log::record("queue quit at sig_handler.",Log::DEBUG);
  160. switch($signo) {
  161. case SIGINT:
  162. case SIGHUP:
  163. case SIGQUIT:
  164. case SIGTERM:
  165. $this->_stop = true;
  166. break;
  167. default:
  168. break;
  169. }
  170. }
  171. }