codispatcher.php 5.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. <?php
  2. declare(strict_types=0);
  3. define('APP_ID', 'cordispatcher');
  4. define('MOBILE_SERVER',true);
  5. define('USE_COROUTINE',true);
  6. define('SUPPORT_PTHREAD',false);
  7. define('BASE_ROOT_PATH',str_replace('/rdispatcher','',dirname(__FILE__)));
  8. define('BASE_PATH',BASE_ROOT_PATH . '/rdispatcher');
  9. require_once(BASE_ROOT_PATH . '/global.php');
  10. require_once(BASE_ROOT_PATH . '/fooder.php');
  11. require_once(BASE_HELPER_PATH . '/event_looper.php');
  12. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  13. require_once(BASE_HELPER_PATH . '/algorithm.php');
  14. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  15. require_once(BASE_PATH . '/processor.php');
  16. require_once(BASE_PATH . '/proxy.php');
  17. Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL|SWOOLE_HOOK_SLEEP]);
  18. if (empty($_SERVER['argv'][1])) exit('parameter error');
  19. $process_count = intval($_SERVER['argv'][1]);
  20. function all_channels() {
  21. return ['refill'];
  22. }
  23. function strbool($value) {
  24. return $value ? 'true' : 'false';
  25. }
  26. function handle_error($level, $message, $file, $line)
  27. {
  28. if($level == E_NOTICE) return;
  29. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  30. $backtrace = debug_backtrace();
  31. foreach ($backtrace as $item) {
  32. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  33. }
  34. Log::record($trace,Log::ERR);
  35. }
  36. function subscribe_message(&$quit, &$redis, $channels)
  37. {
  38. $redis = new Swoole\Coroutine\Redis();
  39. while (!$quit)
  40. {
  41. try
  42. {
  43. Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
  44. if(!$redis->connected) {
  45. $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  46. Log::record("subscribe_message redis connected = {$redis->connected}",Log::DEBUG);
  47. }
  48. else {
  49. $ret = true;
  50. }
  51. if(!$ret) {
  52. Log::record("subscribe_message cannot connet redis.",Log::DEBUG);
  53. $redis->close();
  54. }
  55. elseif($redis->subscribe($channels))
  56. {
  57. while ($msg = $redis->recv())
  58. {
  59. [$sub_type, $channel, $content] = $msg;
  60. $content = unserialize($content);
  61. $type = $content['type'];
  62. if($channel != 'refill') continue;
  63. if($quit) break;
  64. if($type == 'channel' || $type == 'merchant') {
  65. Log::record("subscribe_message recv mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  66. refill\RefillFactory::instance()->load();
  67. }
  68. elseif($type == 'ratio') {
  69. Log::record("subscribe_message recv mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  70. refill\RefillFactory::instance()->UpdateRatio();
  71. }
  72. }
  73. Log::record("subscribe_message redis recv timeout",Log::DEBUG);
  74. }
  75. else {
  76. Log::record("subscribe_message subscribe error",Log::ERR);
  77. }
  78. }
  79. catch (Exception $ex)
  80. {
  81. Log::record($ex->getMessage(),Log::ERR);
  82. }
  83. }
  84. Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
  85. $quit = true;
  86. }
  87. $workers = [];
  88. for ($i = 0; $i < $process_count;$i++)
  89. {
  90. $process = new Swoole\Process(function(Swoole\Process $worker)
  91. {
  92. Base::run_util();
  93. set_error_handler('handle_error');
  94. $sub_quit = false;
  95. $sub_redis = null;
  96. go(function () use (&$sub_quit,&$sub_redis) {
  97. subscribe_message($sub_quit,$sub_redis,['refill']);
  98. });
  99. $looper = new processor(true);
  100. go(function () use ($looper) {
  101. $looper->run();
  102. });
  103. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,$looper)
  104. {
  105. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  106. set_error_handler(null);
  107. try {
  108. $sub_quit = true;
  109. $sub_redis->close();
  110. }
  111. catch(Exception $ex) {
  112. Log::record($ex->getMessage(),Log::DEBUG);
  113. }
  114. $looper->stop();
  115. do {
  116. $res = Swoole\Coroutine::stats();
  117. $num = $res['coroutine_num'];
  118. if($num > 1) {
  119. Swoole\Coroutine::sleep(0.1);
  120. }
  121. Log::record("coroutine_num = {$num}",Log::DEBUG);
  122. } while($num > 1);
  123. });
  124. }, false, false, true);
  125. $pid = $process->start();
  126. $workers[$pid] = $process;
  127. }
  128. Log::record("main process start wait sub process....",Log::DEBUG);
  129. while (true)
  130. {
  131. if($status = Swoole\Process::wait(true)) {
  132. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  133. }
  134. else
  135. {
  136. foreach ($workers as $pid => $worker)
  137. {
  138. Swoole\Process::kill($pid,SIGTERM);
  139. if($status = Swoole\Process::wait(true)) {
  140. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  141. }
  142. }
  143. break;
  144. }
  145. }
  146. Log::record("Quit all",Log::DEBUG);