codispatcher.php 8.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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('SUPPORT_UNUSE_PDLOG',true);
  8. define('BASE_ROOT_PATH',str_replace('/rdispatcher','',dirname(__FILE__)));
  9. define('BASE_PATH',BASE_ROOT_PATH . '/rdispatcher');
  10. require_once(BASE_ROOT_PATH . '/global.php');
  11. require_once(BASE_ROOT_PATH . '/fooder.php');
  12. require_once(BASE_HELPER_PATH . '/event_looper.php');
  13. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  14. require_once(BASE_HELPER_PATH . '/algorithm.php');
  15. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  16. require_once(BASE_PATH . '/processor.php');
  17. require_once(BASE_PATH . '/proxy.php');
  18. Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL|SWOOLE_HOOK_SLEEP]);
  19. Co::set(['socket_connect_timeout' => 60,'socket_read_timeout' => 900,'socket_write_timeout' => 900,'socket_timeout' => 900]);
  20. if (empty($_SERVER['argv'][1])) exit('parameter error');
  21. $process_count = intval($_SERVER['argv'][1]);
  22. function all_channels() {
  23. return ['refill'];
  24. }
  25. function strbool($value) {
  26. return $value ? 'true' : 'false';
  27. }
  28. function handle_error($level, $message, $file, $line)
  29. {
  30. if($level == E_NOTICE) return;
  31. $trace = "handle_error: level=$level msg=$message file=$file,line=$line\n";
  32. $backtrace = debug_backtrace();
  33. foreach ($backtrace as $item) {
  34. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  35. }
  36. Log::record($trace,Log::ERR);
  37. }
  38. function subscribe_message(&$quit, &$redis, $channels)
  39. {
  40. $redis = new Swoole\Coroutine\Redis();
  41. while (!$quit)
  42. {
  43. try
  44. {
  45. Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
  46. if(!$redis->connected) {
  47. $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  48. }
  49. else {
  50. $ret = true;
  51. }
  52. if(!$ret) {
  53. Log::record("subscribe_message cannot connect redis.",Log::DEBUG);
  54. $redis->close();
  55. }
  56. elseif($redis->subscribe($channels))
  57. {
  58. while ($msg = $redis->recv())
  59. {
  60. [$sub_type, $channel, $content] = $msg;
  61. $content = json_decode($content,true);
  62. $type = $content['type'];
  63. if($channel != 'refill') continue;
  64. if($quit) break;
  65. if($type == 'channel' || $type == 'merchant') {
  66. refill\RefillFactory::instance()->load();
  67. refill\transfer::instance()->load();
  68. refill\transfer_timeout::instance()->load();
  69. refill\EventManager::instance()->load();
  70. }
  71. elseif($type == 'mch_profit_ratio') {
  72. $ins = Cache::getInstance('cacheredis');
  73. $content = $ins->get_org('refill_merchant_profit_ratio');
  74. if(empty($content)) continue;
  75. $counts = json_decode($content,true);
  76. if(empty($counts)) continue;
  77. $gross = $counts['gross'];
  78. $detail = $counts['detail'];
  79. $types = $counts['types'];
  80. refill\RefillFactory::instance()->UpdateMchRatios($gross,$detail,$types);
  81. }
  82. elseif($type == 'channel_control') {
  83. $ins = Cache::getInstance('cacheredis');
  84. $content = $ins->get_org('refill_channel_control_model');
  85. if(empty($content)) continue;
  86. $params = json_decode($content,true);
  87. if(empty($params)) continue;
  88. refill\RefillFactory::instance()->UpdateChctl($params);
  89. }
  90. elseif($type == 'channels_cur_speed') { //对通道整体限速
  91. $ins = Cache::getInstance('cacheredis');
  92. $content = $ins->get_org('refill_channels_cur_speed');
  93. if(empty($content)) continue;
  94. $params = json_decode($content,true);
  95. if(empty($params)) continue;
  96. refill\RefillFactory::instance()->UpdateChspeed($params);
  97. }
  98. elseif($type == 'channel_speed') { //人工智能分配的最大速度
  99. $ins = Cache::getInstance('cacheredis');
  100. $content = $ins->get_org('refill_channel_control_speed');
  101. if(empty($content)) continue;
  102. $params = json_decode($content,true);
  103. if(empty($params)) continue;
  104. refill\RefillFactory::instance()->UpdateMaxSpeed($params);
  105. }
  106. else {
  107. //Log::record("subscribe_message dont not handle mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  108. }
  109. }
  110. }
  111. else {
  112. Log::record("subscribe_message subscribe error",Log::ERR);
  113. }
  114. }
  115. catch (Exception $ex)
  116. {
  117. Log::record($ex->getMessage(),Log::ERR);
  118. }
  119. }
  120. Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
  121. $quit = true;
  122. }
  123. $workers = [];
  124. for ($i = 0; $i < $process_count;$i++)
  125. {
  126. $process = new Swoole\Process(function(Swoole\Process $worker)
  127. {
  128. Base::run_util();
  129. refill\RefillFactory::instance();
  130. refill\transfer::instance();
  131. $sub_quit = false;
  132. $sub_redis = null;
  133. $looper = new processor(false);
  134. set_error_handler('handle_error');
  135. register_shutdown_function(function () use ($looper)
  136. {
  137. $error = error_get_last();
  138. if(!empty($error) && $error['type'] != E_NOTICE) {
  139. $msg = "register_shutdown_function type:{$error['type']}\n message:{$error['message']}\n file={$error['file']} line={$error['line']}";
  140. Log::record("$msg",Log::ERR);
  141. }
  142. });
  143. go(function () use (&$sub_quit,&$sub_redis) {
  144. subscribe_message($sub_quit,$sub_redis,['refill']);
  145. });
  146. go(function () use ($looper) {
  147. $looper->run();
  148. });
  149. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,$looper)
  150. {
  151. Log::record("signal call SIGTERM begin signum={$signal_num}, pid={$worker->pid}",Log::DEBUG);
  152. set_error_handler(null);
  153. try {
  154. $sub_quit = true;
  155. $sub_redis->close();
  156. }
  157. catch(Exception $ex) {
  158. Log::record($ex->getMessage(),Log::DEBUG);
  159. }
  160. $looper->stop();
  161. });
  162. }, false, false, true);
  163. $pid = $process->start();
  164. $workers[$pid] = $process;
  165. }
  166. Log::record("main process start wait sub process....",Log::DEBUG);
  167. while (true)
  168. {
  169. if($status = Swoole\Process::wait(true))
  170. {
  171. $quit_pid = $status['pid'];
  172. Log::record("Sub process pid={$status['pid']} quit, code={$status['code']}, signum={$status['signal']}",Log::DEBUG);
  173. foreach ($workers as $pid => $worker)
  174. {
  175. if($pid != $quit_pid) {
  176. Swoole\Process::kill($pid, SIGTERM);
  177. }
  178. }
  179. foreach ($workers as $pid => $worker)
  180. {
  181. if($pid != $quit_pid && ($status = Swoole\Process::wait(true))) {
  182. Log::record("Graceful Recycled pid={$status['pid']}, code={$status['code']}, signum={$status['signal']}",Log::DEBUG);
  183. }
  184. }
  185. break;
  186. }
  187. else
  188. {
  189. foreach ($workers as $pid => $worker) {
  190. Swoole\Process::kill($pid, SIGTERM);
  191. }
  192. foreach ($workers as $pid => $worker)
  193. {
  194. if($status = Swoole\Process::wait(true)) {
  195. Log::record("Graceful Recycled pid={$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  196. }
  197. }
  198. break;
  199. }
  200. }
  201. Log::record("Quit all",Log::DEBUG);