iqueue.php 7.2 KB

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