queue.php 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091
  1. <?php
  2. declare(strict_types=0);
  3. /**
  4. * 队列
  5. *
  6. *
  7. *
  8. *
  9. */
  10. defined('InShopNC') or exit('Access Invalid!');
  11. //此行代码会导致bug
  12. //ini_set('default_socket_timeout', -1);
  13. class queueControl
  14. {
  15. private $_stop = false;
  16. public function indexOp()
  17. {
  18. if (ob_get_level()) ob_end_clean();
  19. pcntl_signal(SIGINT, [$this,'sig_handler']);
  20. pcntl_signal(SIGHUP, [$this,'sig_handler']);
  21. pcntl_signal(SIGQUIT, [$this,'sig_handler']);
  22. pcntl_signal(SIGTERM, [$this,'sig_handler']);
  23. $logic_queue = Logic('queue');
  24. $worker = new QueueServer();
  25. $queues = $worker->scan();
  26. $empty_times = 0;
  27. while (true)
  28. {
  29. pcntl_signal_dispatch();
  30. try
  31. {
  32. if ($this->_stop) break;
  33. $content = $worker->pop($queues, 1);
  34. if(is_array($content))
  35. {
  36. $method = key($content);
  37. $arg = current($content);
  38. $argstr = json_encode($arg,JSON_UNESCAPED_UNICODE);
  39. Log::record("method={$method} args={$argstr}",Log::DEBUG);
  40. $result = $logic_queue->$method($arg);
  41. if (!$result['state']) {
  42. Log::record("{$method} run error: {$result['msg']}",Log::ERR);
  43. }
  44. $empty_times = 0;
  45. }
  46. else
  47. {
  48. $empty_times ++;
  49. if($empty_times > 600) {
  50. $model = Model();
  51. $model->checkActive();
  52. unset($model);
  53. $empty_times = 0;
  54. }
  55. }
  56. }
  57. catch (Exception $e)
  58. {
  59. $err = $e->getMessage();
  60. $code = $e->getCode();
  61. Log::record("QueueDB pop err: code={$code} err={$err}",Log::ERR);
  62. break;
  63. }
  64. }
  65. }
  66. private function sig_handler($signo)
  67. {
  68. Log::record("queue quit at sig_handler.",Log::DEBUG);
  69. switch($signo) {
  70. case SIGINT:
  71. case SIGHUP:
  72. case SIGQUIT:
  73. case SIGTERM:
  74. $this->_stop = true;
  75. break;
  76. default:
  77. break;
  78. }
  79. }
  80. }