iqueue.php 11 KB

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