codispatcher.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  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. }
  47. else {
  48. $ret = true;
  49. }
  50. if(!$ret) {
  51. Log::record("subscribe_message cannot connet redis.",Log::DEBUG);
  52. $redis->close();
  53. }
  54. elseif($redis->subscribe($channels))
  55. {
  56. while ($msg = $redis->recv())
  57. {
  58. [$sub_type, $channel, $content] = $msg;
  59. $content = json_decode($content,true);
  60. $type = $content['type'];
  61. if($channel != 'refill') continue;
  62. if($quit) break;
  63. if($type == 'channel' || $type == 'merchant') {
  64. refill\RefillFactory::instance()->load();
  65. }
  66. elseif($type == 'ratio') {
  67. $start = microtime(true);
  68. $ins = Cache::getInstance('cacheredis');
  69. $val = $ins->get_org('channel_ratios');
  70. if(empty($val)) continue;
  71. $val = json_decode($val,true);
  72. if(empty($val)) continue;
  73. $ratios = $val['ratios'];
  74. if(empty($ratios)) continue;
  75. refill\RefillFactory::instance()->UpdateRatio($ratios);
  76. $use_time = microtime(true) - $start;
  77. $msg = sprintf("subscribe_message UpdateRatio use_time=%.6f",$use_time);
  78. Log::record($msg,Log::DEBUG);
  79. }
  80. else {
  81. Log::record("subscribe_message dont not handle mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  82. }
  83. }
  84. }
  85. else {
  86. Log::record("subscribe_message subscribe error",Log::ERR);
  87. }
  88. }
  89. catch (Exception $ex)
  90. {
  91. Log::record($ex->getMessage(),Log::ERR);
  92. }
  93. }
  94. Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
  95. $quit = true;
  96. }
  97. $workers = [];
  98. for ($i = 0; $i < $process_count;$i++)
  99. {
  100. $process = new Swoole\Process(function(Swoole\Process $worker)
  101. {
  102. Base::run_util();
  103. set_error_handler('handle_error');
  104. $sub_quit = false;
  105. $sub_redis = null;
  106. go(function () use (&$sub_quit,&$sub_redis) {
  107. subscribe_message($sub_quit,$sub_redis,['refill']);
  108. });
  109. $looper = new processor(true);
  110. go(function () use ($looper) {
  111. $looper->run();
  112. });
  113. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,$looper)
  114. {
  115. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  116. set_error_handler(null);
  117. try {
  118. $sub_quit = true;
  119. $sub_redis->close();
  120. }
  121. catch(Exception $ex) {
  122. Log::record($ex->getMessage(),Log::DEBUG);
  123. }
  124. $looper->stop();
  125. do {
  126. $res = Swoole\Coroutine::stats();
  127. $num = $res['coroutine_num'];
  128. if($num > 1) {
  129. Swoole\Coroutine::sleep(0.1);
  130. }
  131. Log::record("coroutine_num = {$num}",Log::DEBUG);
  132. } while($num > 1);
  133. });
  134. }, false, false, true);
  135. $pid = $process->start();
  136. $workers[$pid] = $process;
  137. }
  138. Log::record("main process start wait sub process....",Log::DEBUG);
  139. while (true)
  140. {
  141. if($status = Swoole\Process::wait(true)) {
  142. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  143. }
  144. else
  145. {
  146. foreach ($workers as $pid => $worker)
  147. {
  148. Swoole\Process::kill($pid,SIGTERM);
  149. if($status = Swoole\Process::wait(true)) {
  150. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  151. }
  152. }
  153. break;
  154. }
  155. }
  156. Log::record("Quit all",Log::DEBUG);