centra_srv.php 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115
  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/server.php');
  5. require_once (BASE_ROOT_PATH . '/helper/search/processor.php');
  6. require_once (BASE_ROOT_PATH . '/helper/search/event_handler.php');
  7. require_once(BASE_ROOT_PATH . '/helper/search/util.php');
  8. require_once(BASE_ROOT_PATH . '/helper/category_helper.php');
  9. require_once(BASE_ROOT_PATH . '/helper/brand_helper.php');
  10. require_once(BASE_ROOT_PATH . '/helper/search/searcher.php');
  11. require_once(BASE_ROOT_PATH . '/helper/search/area_library.php');
  12. require_once(BASE_ROOT_PATH . '/helper/brand_helper.php');
  13. require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
  14. require_once (BASE_ROOT_PATH . '/helper/message/msgutil.php');
  15. require_once (BASE_ROOT_PATH . '/helper/message/subscriber.php');
  16. function all_channels()
  17. {
  18. return array('searcher');
  19. }
  20. $gMessageStates = null;
  21. $gMessageLock = null;
  22. function search_work($sockfd)
  23. {
  24. global $gMessageStates;
  25. global $gMessageLock;
  26. $gMessageStates = new MsgStates();
  27. $gMessageLock = Mutex::create();
  28. echo "start searcher";
  29. Base::run_util();
  30. StatesHelper::init();
  31. $listener = new message\subscriber($gMessageStates,$gMessageLock);
  32. $listener->start();
  33. if(StatesHelper::fetch_state('init')) {
  34. Log::record("StatesHelper::fetch_state first.",Log::DEBUG);
  35. search\searcher::instance()->init();
  36. \search\area_library::instance();
  37. }
  38. search\CentraHelper::instance()->init(new search\processor());
  39. echo "start ending";
  40. search\CentraHelper::instance()->run_loop($sockfd);
  41. echo "end run_loop";
  42. }
  43. function fork_subprocess($count,$listen_fd)
  44. {
  45. if (($child = 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. echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
  52. search_work($listen_fd);
  53. exit();
  54. }
  55. elseif($child === -1)
  56. {
  57. echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
  58. die('could not fork');
  59. }
  60. else
  61. {
  62. echo "pid = {$child} count = {$count} " . __LINE__ ."\r\n";
  63. $ret = pcntl_waitpid($child,$status,WNOHANG);
  64. if($ret == 0) {
  65. echo "spawn-fcgi: successful ret == 0 PID: {$child}\n" ;
  66. }
  67. elseif($ret == -1) {
  68. echo "spawn-fcgi: ret == 0 PID: {$child}\n" ;
  69. }
  70. else {
  71. echo "spawn-fcgi: ret == 0 child exited PID: {$child}.\n";
  72. }
  73. }
  74. }
  75. function remote_addr()
  76. {
  77. global $config;
  78. $host = $config['searcher']['host'];
  79. $port = $config['searcher']['port'];
  80. return "{$host}:{$port}";
  81. }
  82. $listen_fd = stream_socket_server (remote_addr(), $errno, $errstr);
  83. if($listen_fd != false)
  84. {
  85. $count = 1;
  86. while ($count-- > 0) {
  87. fork_subprocess($count,$listen_fd);
  88. }
  89. } else {
  90. echo "无法创建socket,请退出之前进程.\n";
  91. }