iqueue.php 9.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379
  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. use Swoole;
  9. class IQueueDB
  10. {
  11. private $_redis;
  12. private $_queue_name;
  13. //存定义存储表的数量,系统会随机分配存储
  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->_queue_name = $queue_name;
  21. $this->_comode = $comode;
  22. if ($this->_comode) {
  23. $this->_redis = new Swoole\Coroutine\Redis();
  24. } else {
  25. $this->_redis = new Redis();
  26. }
  27. $this->connect();
  28. }
  29. public function connect(): bool
  30. {
  31. if ($this->_comode)
  32. {
  33. if ($this->_redis->connected) {
  34. return true;
  35. }
  36. else {
  37. $this->close();
  38. $ret = $this->_redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  39. Log::record("IQueueDB::connect ret = {$ret}", Log::DEBUG);
  40. return $this->_redis->connected;
  41. }
  42. }
  43. elseif ($this->_redis->isConnected()) {
  44. return true;
  45. }
  46. else {
  47. $this->close();
  48. $ret = $this->_redis->connect(C('queue.host'), C('queue.port'));
  49. Log::record("IQueueDB::connect ret = {$ret}", Log::DEBUG);
  50. return $this->_redis->isConnected();
  51. }
  52. }
  53. public function close()
  54. {
  55. if ($this->_comode)
  56. {
  57. if ($this->_redis->connected) {
  58. $this->_redis->close();
  59. }
  60. } elseif ($this->_redis->isConnected()) {
  61. $this->_redis->close();
  62. } else {
  63. Log::record("redis has closed",Log::DEBUG);
  64. }
  65. }
  66. public function rpush($value)
  67. {
  68. try
  69. {
  70. if($this->connect()) {
  71. $ret = $this->_redis->rPush($this->_queue_name, $value);
  72. Log::record("IQueueDB::lpush ret={$ret}", Log::DEBUG);
  73. return $ret;
  74. } else {
  75. Log::record("IQueueDB::rpush connect=false", Log::DEBUG);
  76. return false;
  77. }
  78. }
  79. catch(Exception $e) {
  80. Log::record("IQueueDB::rpush " . $e->getMessage(),Log::ERR);
  81. return false;
  82. }
  83. }
  84. public function lpush($value)
  85. {
  86. try
  87. {
  88. if ($this->connect()) {
  89. $ret = $this->_redis->lPush($this->_queue_name, $value);
  90. Log::record("IQueueDB::lpush ret={$ret}", Log::DEBUG);
  91. return $ret;
  92. } else {
  93. Log::record("IQueueDB::lpush connect=false", Log::DEBUG);
  94. return false;
  95. }
  96. }
  97. catch(Exception $e) {
  98. Log::record("IQueueDB::lpush " . $e->getMessage(),Log::ERR);
  99. return false;
  100. }
  101. }
  102. public function scan()
  103. {
  104. $list_key[] = $this->_queue_name;
  105. return $list_key;
  106. }
  107. public function rpop()
  108. {
  109. $key = $this->_queue_name;
  110. $result = $this->_redis->rPop($key);
  111. return $result;
  112. }
  113. public function lpop()
  114. {
  115. $key = $this->_queue_name;
  116. $result = $this->_redis->lPop($key);
  117. return $result;
  118. }
  119. public function brpop($key, $time)
  120. {
  121. $result = $this->_redis->brPop($key, $time);
  122. $tmp = serialize($result);
  123. Log::record("IQueueDB::brPop={$tmp}",Log::DEBUG);
  124. if ($result) {
  125. return $result[1];
  126. } else {
  127. return null;
  128. }
  129. }
  130. public function blpop($key, $time)
  131. {
  132. $result = $this->_redis->blPop($key, $time);
  133. $tmp = serialize($result);
  134. Log::record("IQueueDB::blPop={$tmp}",Log::DEBUG);
  135. if ($result) {
  136. return $result[1];
  137. } else {
  138. return null;
  139. }
  140. }
  141. public function clear() {
  142. $this->_redis->flushAll();
  143. }
  144. }
  145. /**
  146. * 队列处理
  147. *
  148. *
  149. * @package
  150. */
  151. class IClient
  152. {
  153. private $mQueuedb;
  154. public function __construct($queueDb)
  155. {
  156. $this->mQueuedb = $queueDb;
  157. }
  158. public function push($key, $value)
  159. {
  160. return $this->mQueuedb->lpush(serialize([$key=>$value]));
  161. }
  162. }
  163. class IServer
  164. {
  165. private $_queuedb;
  166. public function __construct($queueDb) {
  167. $this->_queuedb = $queueDb;
  168. }
  169. public function connect() : bool
  170. {
  171. return $this->_queuedb->connect();
  172. }
  173. public function rpop()
  174. {
  175. $result = $this->_queuedb->rpop();
  176. if($result != null) {
  177. return unserialize($result);
  178. } else {
  179. return false;
  180. }
  181. }
  182. public function lpop()
  183. {
  184. $result = $this->_queuedb->lpop();
  185. if($result != null) {
  186. return unserialize($result);
  187. } else {
  188. return false;
  189. }
  190. }
  191. public function brpop($key,$time)
  192. {
  193. $result = $this->_queuedb->brpop($key,$time);
  194. if($result != null) {
  195. return unserialize($result);
  196. } else {
  197. return false;
  198. }
  199. }
  200. public function blpop($key,$time)
  201. {
  202. $result = $this->_queuedb->lpop($key,$time);
  203. if($result != null) {
  204. return unserialize($result);
  205. } else {
  206. return false;
  207. }
  208. }
  209. public function scan() {
  210. return $this->_queuedb->scan();
  211. }
  212. public function stop()
  213. {
  214. $this->_queuedb->close();
  215. }
  216. }
  217. abstract class ILooper
  218. {
  219. private $_stop = false;
  220. private $mServer;
  221. const MAX_COROUTINE = 100;
  222. protected function __construct($server)
  223. {
  224. $this->mServer = $server;
  225. }
  226. public function prepare()
  227. {
  228. if (ob_get_level()) ob_end_clean();
  229. pcntl_signal(SIGINT, [$this,'sig_handler']);
  230. pcntl_signal(SIGHUP, [$this,'sig_handler']);
  231. pcntl_signal(SIGQUIT, [$this,'sig_handler']);
  232. pcntl_signal(SIGTERM, [$this,'sig_handler']);
  233. }
  234. abstract protected function handle($msg);
  235. public function stop()
  236. {
  237. Log::record(__FUNCTION__,Log::DEBUG);
  238. $this->_stop = true;
  239. }
  240. public function run()
  241. {
  242. $queues = $this->mServer->scan();
  243. while (true)
  244. {
  245. try
  246. {
  247. if ($this->_stop) break;
  248. perfor_clear();
  249. if(defined('USE_COROUTINE') && USE_COROUTINE)
  250. {
  251. $res = Swoole\Coroutine::stats();
  252. $num = $res['coroutine_num'];
  253. $mem = memory_get_usage();
  254. if($num < ILooper::MAX_COROUTINE)
  255. {
  256. if($this->mServer->connect() == false) {
  257. Swoole\Coroutine::sleep(1);
  258. Log::record("Processor redis disconnect.",Log::ERR);
  259. continue;
  260. }
  261. $content = $this->mServer->rpop();
  262. if(empty($content)) {
  263. Swoole\Coroutine::sleep(1);
  264. continue;
  265. }
  266. if($this->_stop)
  267. {
  268. foreach ($content as $key => $params) {
  269. DispatcherClient::instance()->push($key, $params);
  270. }
  271. }
  272. else
  273. {
  274. go(function ()use ($content,$num,$mem) {
  275. $start = microtime(true);
  276. Log::record("BeginGoFunction coroutin_num={$num} memory={$mem}",Log::DEBUG);
  277. $method = $this->handle($content);
  278. $use_time = microtime(true) - $start;
  279. $msg = sprintf("EndGoFunction coroutin_num={$num} memory={$mem} request_time=%.6f method={$method}",$use_time);
  280. Log::record($msg,Log::DEBUG);
  281. });
  282. }
  283. }
  284. else {
  285. Swoole\Coroutine::sleep(0.1);
  286. }
  287. }
  288. else
  289. {
  290. if($this->mServer->connect() == false) {
  291. sleep(1);
  292. continue;
  293. }
  294. $content = $this->mServer->pop($queues,1);
  295. if(empty($content)) continue;
  296. if($this->_stop)
  297. {
  298. foreach ($content as $key => $params) {
  299. DispatcherClient::instance()->push($key, $params);
  300. }
  301. }
  302. else
  303. {
  304. perfor_clear();
  305. perfor_start();
  306. $this->handle($content);
  307. perfor_end('Handle Request');
  308. $info = perfor_log();
  309. Log::record("{$info} \r\n",Log::DEBUG);
  310. }
  311. }
  312. }
  313. catch (Exception $e)
  314. {
  315. $err = $e->getMessage();
  316. $code = $e->getCode();
  317. Log::record("QueueDB pop err: code={$code} err={$err}",Log::ERR);
  318. }
  319. }
  320. Log::record("ILooper Run quit.", Log::DEBUG);
  321. }
  322. private function sig_handler($signo)
  323. {
  324. Log::record("queue quit at sig_handler.",Log::DEBUG);
  325. switch($signo) {
  326. case SIGINT:
  327. case SIGHUP:
  328. case SIGQUIT:
  329. case SIGTERM:
  330. $this->_stop = true;
  331. break;
  332. default:
  333. break;
  334. }
  335. }
  336. }