centra_srv.php 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. <?php
  2. define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
  3. require_once (BASE_ROOT_PATH . '/fooder.php');
  4. require_once (BASE_ROOT_PATH . '/helper/search/srv_base.php');
  5. require_once (BASE_ROOT_PATH . '/helper/search/server.php');
  6. require_once (BASE_ROOT_PATH . '/helper/search/processor.php');
  7. require_once (BASE_ROOT_PATH . '/helper/search/event_handler.php');
  8. require_once (BASE_ROOT_PATH . '/helper/search/util.php');
  9. require_once (BASE_ROOT_PATH . '/helper/category_helper.php');
  10. require_once (BASE_ROOT_PATH . '/helper/brand_helper.php');
  11. require_once (BASE_ROOT_PATH . '/helper/goods/commonid_helper.php');
  12. require_once (BASE_ROOT_PATH . '/helper/search/searcher.php');
  13. require_once (BASE_ROOT_PATH . '/helper/special_helper.php');
  14. require_once (BASE_ROOT_PATH . '/helper/search/area_library.php');
  15. require_once (BASE_ROOT_PATH . '/helper/brand_helper.php');
  16. require_once (BASE_ROOT_PATH . '/helper/algorithm.php');
  17. require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
  18. require_once (BASE_ROOT_PATH . '/helper/message/subscriber.php');
  19. function all_channels()
  20. {
  21. return array('searcher');
  22. }
  23. $gMessageStates = null;
  24. $gMessageLock = null;
  25. function search_work($sockfd)
  26. {
  27. global $gMessageStates;
  28. global $gMessageLock;
  29. $gMessageStates = new MsgStates();
  30. $gMessageLock = Mutex::create();
  31. Base::run_util();
  32. StatesHelper::init();
  33. $listener = new message\subscriber($gMessageStates,$gMessageLock);
  34. $listener->start();
  35. if(StatesHelper::fetch_state('init')) {
  36. Log::record("StatesHelper::fetch_state first.",Log::DEBUG);
  37. search\searcher::instance()->init();
  38. search\area_library::instance();
  39. }
  40. search\CenterHelper::instance()->init(new search\processor());
  41. search\CenterHelper::instance()->run_loop($sockfd);
  42. }
  43. function fork_subprocess($count,$listen_fd)
  44. {
  45. if (($pid = pcntl_fork()) === 0)
  46. {
  47. //ob_end_clean(); // Discard the output buffer and close
  48. fclose(STDIN); // Close all of the standard
  49. fclose(STDOUT); // file descriptors as we
  50. fclose(STDERR); // are running as a daemon.
  51. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  52. search_work($listen_fd);
  53. exit();
  54. }
  55. elseif($pid === -1)
  56. {
  57. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  58. die('could not fork');
  59. }
  60. else
  61. {
  62. socket_close($listen_fd);
  63. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  64. $ret = pcntl_waitpid($pid,$status,WNOHANG);
  65. if($ret == 0) {
  66. Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
  67. }
  68. elseif($ret == -1) {
  69. Log::record("spawn-fcgi: ret == 0 PID: {$pid}",Log::DEBUG);
  70. }
  71. else {
  72. Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
  73. }
  74. }
  75. }
  76. global $config;
  77. $host = $config['searcher']['host'];
  78. $port = $config['searcher']['port'];
  79. $listen_fd = socket_create(AF_INET, SOCK_STREAM, SOL_TCP);
  80. if(!socket_set_option($listen_fd, SOL_SOCKET, SO_REUSEADDR, 1)) {
  81. echo "socket_set_option 地址重用失败.\n";
  82. }
  83. if(!socket_set_nonblock($listen_fd)) {
  84. $err = socket_last_error();
  85. Log::record("socket_set_blocking error : {$err}",Log::DEBUG);
  86. }
  87. if(!socket_bind($listen_fd, $host, $port)) {
  88. echo "无法创建socket,请退出之前进程.\n";
  89. return;
  90. }
  91. if(!socket_listen($listen_fd)) {
  92. echo "无法创建socket,请退出之前进程.\n";
  93. return;
  94. }
  95. $count = 1;
  96. while ($count-- > 0) {
  97. fork_subprocess($count,$listen_fd);
  98. }
  99. //search_work($listen_fd);