queue.php 2.4 KB

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