ugc_srv.php 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. <?php
  2. /**
  3. * Created by PhpStorm.
  4. * User: stanley-king
  5. * Date: 2017/7/19
  6. * Time: 下午2:53
  7. */
  8. define('BASE_ROOT_PATH',str_replace('\\','/',dirname(__FILE__)));
  9. define('UGC_SUBSCRIBER_PROC',true);
  10. require_once(BASE_ROOT_PATH . '/fooder.php');
  11. require_once(BASE_ROOT_PATH . '/helper/algorithm.php');
  12. require_once(BASE_ROOT_PATH . '/helper/message/msgutil.php');
  13. require_once(BASE_ROOT_PATH . '/helper/message/subscriber.php');
  14. require_once(BASE_ROOT_PATH . '/helper/ugc_helper.php');
  15. require_once(BASE_ROOT_PATH . '/helper/search/tcp_client.php');
  16. function all_channels() {
  17. return array('special','follow','inviter');
  18. }
  19. $gMessageStates = null;
  20. $gMessageLock = null;
  21. class UgcHandler
  22. {
  23. static public function onSpecial($msg)
  24. {
  25. if(empty($msg)) return false;
  26. $content = unserialize($msg);
  27. if($content == false || !is_array($content)) {
  28. return false;
  29. }
  30. $type = $content['type'];
  31. $params = $content['params'];
  32. if($type == 'add') {
  33. search\relation_client::instance()->add_special(array('user_id' => $params['user_id'],'specials' => $params['specials']));
  34. }
  35. elseif($type == 'del') {
  36. search\relation_client::instance()->del_special(array('user_id' => $params['user_id'],'specials' => $params['specials']));
  37. }
  38. else {
  39. }
  40. return true;
  41. }
  42. static public function onFellow($msg)
  43. {
  44. if(empty($msg)) return false;
  45. $content = unserialize($msg);
  46. if($content == false || !is_array($content)) {
  47. return false;
  48. }
  49. $type = $content['type'];
  50. $params = $content['params'];
  51. if($type == 'add') {
  52. search\relation_client::instance()->add_follow(['user_id' => $params['user_id'],'friends' => $params['friends']]);
  53. }
  54. elseif($type == 'del') {
  55. search\relation_client::instance()->del_follow(['user_id' => $params['user_id'],'friends' => $params['friends']]);
  56. }
  57. else {
  58. }
  59. return true;
  60. }
  61. static public function onInviter($msg)
  62. {
  63. if(empty($msg)) return false;
  64. $content = unserialize($msg);
  65. if($content == false || !is_array($content)) {
  66. return false;
  67. }
  68. $type = $content['type'];
  69. $params = $content['params'];
  70. if($type == 'add') {
  71. search\relation_client::instance()->add_inviter(['user_id' => $params['user_id'],'inviter_id' => $params['inviter_id']]);
  72. }
  73. else {
  74. }
  75. return true;
  76. }
  77. }
  78. function work_proc()
  79. {
  80. global $gMessageStates;
  81. global $gMessageLock;
  82. $gMessageStates = new MsgStates();
  83. $gMessageLock = Mutex::create();
  84. Base::run_util();
  85. ugc_helper::init_server();
  86. StatesHelper::init();
  87. $listener = new message\subscriber($gMessageStates,$gMessageLock);
  88. $listener->run();
  89. }
  90. function fork_subprocess($count)
  91. {
  92. if (($pid = pcntl_fork()) === 0)
  93. {
  94. fclose(STDIN); // Close all of the standard
  95. fclose(STDOUT); // file descriptors as we
  96. fclose(STDERR); // are running as a daemon.
  97. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  98. work_proc();
  99. exit();
  100. }
  101. elseif($pid === -1)
  102. {
  103. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  104. die('could not fork');
  105. }
  106. else
  107. {
  108. Log::record("pid = {$pid} count = {$count} ",Log::DEBUG);
  109. $ret = pcntl_waitpid($pid,$status,WNOHANG);
  110. if($ret == 0) {
  111. Log::record("spawn-fcgi: successful ret == 0 PID: {$pid}",Log::DEBUG);
  112. }
  113. elseif($ret == -1) {
  114. Log::record("spawn-fcgi: ret == 0 PID: {$pid}",Log::DEBUG);
  115. }
  116. else {
  117. Log::record("spawn-fcgi: ret == 0 child exited PID: {$pid}.",Log::DEBUG);
  118. }
  119. }
  120. }
  121. fork_subprocess(1);
  122. //work_proc();