iqueue.php 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265
  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. private $_comode;
  15. public function __construct($queue_name,$comode = false)
  16. {
  17. if ( !extension_loaded('redis') ) {
  18. throw_exception('redis failed to load');
  19. }
  20. $this->_tb_prefix = $queue_name;
  21. $this->_comode = $comode;
  22. if ($this->_comode) {
  23. $this->_redis = new \Swoole\Coroutine\Redis();
  24. $ret = $this->_redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  25. } else {
  26. $this->_redis = new Redis();
  27. $ret = $this->_redis->connect(C('queue.host'), C('queue.port'), 20);
  28. }
  29. $this->connect();
  30. }
  31. public function connect(): bool
  32. {
  33. if ($this->_comode) {
  34. if ($this->_redis->connected) return true;
  35. $ret = $this->_redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  36. Log::record("Swoole\Coroutine\Redis connect ret = {$ret}", Log::DEBUG);
  37. return $this->_redis->connected;
  38. } else {
  39. if ($this->_redis->isConnected()) return true;
  40. $ret = $this->_redis->connect(C('queue.host'), C('queue.port'), 20);
  41. Log::record("Redis connect ret = {$ret}", Log::DEBUG);
  42. return $this->_redis->isConnected();
  43. }
  44. }
  45. public function rpush($value)
  46. {
  47. try {
  48. return $this->_redis->rPush($this->_tb_prefix.rand(1,$this->_tb_num),$value);
  49. } catch(Exception $e) {
  50. throw_exception($e->getMessage());
  51. }
  52. }
  53. public function lpush($value)
  54. {
  55. try {
  56. return $this->_redis->lPush($this->_tb_prefix.rand(1,$this->_tb_num),$value);
  57. } catch(Exception $e) {
  58. throw_exception($e->getMessage());
  59. }
  60. }
  61. public function scan()
  62. {
  63. $list_key = [];
  64. for($i=1;$i<=$this->_tb_num;$i++) {
  65. $list_key[] = $this->_tb_prefix.$i;
  66. }
  67. return $list_key;
  68. }
  69. public function rpop($key, $time)
  70. {
  71. $result = $this->_redis->brPop($key, $time);
  72. if ($result) {
  73. return $result[1];
  74. } else {
  75. return null;
  76. }
  77. }
  78. public function lpop($key, $time)
  79. {
  80. $result = $this->_redis->blPop($key, $time);
  81. if ($result) {
  82. return $result[1];
  83. } else {
  84. return null;
  85. }
  86. }
  87. public function clear() {
  88. $this->_redis->flushAll();
  89. }
  90. }
  91. /**
  92. * 队列处理
  93. *
  94. *
  95. * @package
  96. */
  97. class IClient
  98. {
  99. private $mQueuedb;
  100. public function __construct($queueDb)
  101. {
  102. $this->mQueuedb = $queueDb;
  103. }
  104. public function push($key, $value)
  105. {
  106. return $this->mQueuedb->lpush(serialize([$key=>$value]));
  107. }
  108. }
  109. class IServer
  110. {
  111. private $_queuedb;
  112. public function __construct($queueDb) {
  113. $this->_queuedb = $queueDb;
  114. }
  115. public function connect() : bool
  116. {
  117. return $this->_queuedb->connect();
  118. }
  119. public function pop($key,$time)
  120. {
  121. $result = $this->_queuedb->rpop($key,$time);
  122. if($result != null) {
  123. return unserialize($result);
  124. } else {
  125. return false;
  126. }
  127. }
  128. public function lpop($key,$time)
  129. {
  130. $result = $this->_queuedb->lpop($key,$time);
  131. if($result != null) {
  132. return unserialize($result);
  133. } else {
  134. return false;
  135. }
  136. }
  137. public function scan() {
  138. return $this->_queuedb->scan();
  139. }
  140. }
  141. abstract class ILooper
  142. {
  143. private $_stop = false;
  144. private $mServer;
  145. const MAX_COROUTINE = 1000;
  146. protected function __construct($server)
  147. {
  148. $this->mServer = $server;
  149. }
  150. public function prepare()
  151. {
  152. if (ob_get_level()) ob_end_clean();
  153. pcntl_signal(SIGINT, [$this,'sig_handler']);
  154. pcntl_signal(SIGHUP, [$this,'sig_handler']);
  155. pcntl_signal(SIGQUIT, [$this,'sig_handler']);
  156. pcntl_signal(SIGTERM, [$this,'sig_handler']);
  157. }
  158. abstract protected function handle($msg);
  159. public function run()
  160. {
  161. $queues = $this->mServer->scan();
  162. while (true)
  163. {
  164. try
  165. {
  166. if ($this->_stop) break;
  167. if(defined('USE_COROUTINE') && USE_COROUTINE)
  168. {
  169. $res = \Swoole\Coroutine::stats();
  170. $num = $res['coroutine_num'];
  171. if($num < ILooper::MAX_COROUTINE)
  172. {
  173. if($this->mServer->connect() == false) {
  174. \Swoole\Coroutine::sleep(1);
  175. Log::record("Processor redis disconnect.",Log::ERR);
  176. continue;
  177. }
  178. $content = $this->mServer->pop($queues,1);
  179. if(empty($content)) continue;
  180. go(function ()use ($content) {
  181. $this->handle($content);
  182. });
  183. }
  184. else {
  185. \Swoole\Coroutine::sleep(0.1);
  186. }
  187. }
  188. else
  189. {
  190. if($this->mServer->connect() == false) {
  191. sleep(1);
  192. continue;
  193. }
  194. $content = $this->mServer->pop($queues,1);
  195. if(empty($content)) continue;
  196. perfor_clear();
  197. perfor_start();
  198. $this->handle($content);
  199. perfor_end('Handle Request');
  200. $info = perfor_log();
  201. }
  202. Log::record("{$info} \r\n",Log::DEBUG);
  203. }
  204. catch (Exception $e)
  205. {
  206. $err = $e->getMessage();
  207. $code = $e->getCode();
  208. Log::record("QueueDB pop err: code={$code} err={$err}",Log::ERR);
  209. }
  210. }
  211. }
  212. private function sig_handler($signo)
  213. {
  214. Log::record("queue quit at sig_handler.",Log::DEBUG);
  215. switch($signo) {
  216. case SIGINT:
  217. case SIGHUP:
  218. case SIGQUIT:
  219. case SIGTERM:
  220. $this->_stop = true;
  221. break;
  222. default:
  223. break;
  224. }
  225. }
  226. }