codispatcher.php 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. Co::set(['socket_connect_timeout' => 60,'socket_read_timeout' => 900,'socket_write_timeout' => 900,'socket_timeout' => 900]);
  19. if (empty($_SERVER['argv'][1])) exit('parameter error');
  20. $process_count = intval($_SERVER['argv'][1]);
  21. function all_channels() {
  22. return ['refill'];
  23. }
  24. function strbool($value) {
  25. return $value ? 'true' : 'false';
  26. }
  27. function handle_error($level, $message, $file, $line)
  28. {
  29. if($level == E_NOTICE) return;
  30. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  31. $backtrace = debug_backtrace();
  32. foreach ($backtrace as $item) {
  33. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  34. }
  35. Log::record($trace,Log::ERR);
  36. }
  37. function subscribe_message(&$quit, &$redis, $channels)
  38. {
  39. $redis = new Swoole\Coroutine\Redis();
  40. while (!$quit)
  41. {
  42. try
  43. {
  44. Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
  45. if(!$redis->connected) {
  46. $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  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 = json_decode($content,true);
  61. $type = $content['type'];
  62. if($channel != 'refill') continue;
  63. if($quit) break;
  64. if($type == 'channel' || $type == 'merchant') {
  65. refill\RefillFactory::instance()->load();
  66. }
  67. elseif($type == 'ratio') {
  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. }
  77. elseif($type == 'mch_counts') {
  78. $ins = Cache::getInstance('cacheredis');
  79. $content = $ins->get_org('merchant_refill_counts');
  80. if(empty($content)) continue;
  81. $counts = json_decode($content,true);
  82. if(empty($counts)) continue;
  83. $gross = $counts['gross'];
  84. $detail = $counts['detail'];
  85. refill\RefillFactory::instance()->UpdatMchRatios($gross,$detail);
  86. }
  87. else {
  88. Log::record("subscribe_message dont not handle mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  89. }
  90. }
  91. }
  92. else {
  93. Log::record("subscribe_message subscribe error",Log::ERR);
  94. }
  95. }
  96. catch (Exception $ex)
  97. {
  98. Log::record($ex->getMessage(),Log::ERR);
  99. }
  100. }
  101. Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
  102. $quit = true;
  103. }
  104. $workers = [];
  105. for ($i = 0; $i < $process_count;$i++)
  106. {
  107. $process = new Swoole\Process(function(Swoole\Process $worker)
  108. {
  109. Base::run_util();
  110. refill\RefillFactory::instance();
  111. $sub_quit = false;
  112. $sub_redis = null;
  113. $looper = new processor(false);
  114. set_error_handler('handle_error');
  115. register_shutdown_function(function () use ($looper)
  116. {
  117. $error = error_get_last();
  118. if(!empty($error) && $error['type'] != E_NOTICE) {
  119. $msg = "register_shutdown_function type:{$error['type']}\n message:{$error['message']}\n file={$error['file']} line={$error['line']}";
  120. Log::record("$msg",Log::ERR);
  121. }
  122. });
  123. go(function () use (&$sub_quit,&$sub_redis) {
  124. subscribe_message($sub_quit,$sub_redis,['refill']);
  125. });
  126. go(function () use ($looper) {
  127. $looper->run();
  128. });
  129. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,$looper)
  130. {
  131. Log::record("signal call SIGTERM begin signum={$signal_num}, pid={$worker->pid}",Log::DEBUG);
  132. set_error_handler(null);
  133. try {
  134. $sub_quit = true;
  135. $sub_redis->close();
  136. }
  137. catch(Exception $ex) {
  138. Log::record($ex->getMessage(),Log::DEBUG);
  139. }
  140. $looper->stop();
  141. });
  142. }, false, false, true);
  143. $pid = $process->start();
  144. $workers[$pid] = $process;
  145. }
  146. Log::record("main process start wait sub process....",Log::DEBUG);
  147. while (true)
  148. {
  149. if($status = Swoole\Process::wait(true))
  150. {
  151. $quit_pid = $status['pid'];
  152. Log::record("Sub process pid={$status['pid']} quit, code={$status['code']}, signum={$status['signal']}",Log::DEBUG);
  153. foreach ($workers as $pid => $worker)
  154. {
  155. if($pid != $quit_pid) {
  156. Swoole\Process::kill($pid, SIGTERM);
  157. }
  158. }
  159. foreach ($workers as $pid => $worker)
  160. {
  161. if($pid != $quit_pid && ($status = Swoole\Process::wait(true))) {
  162. Log::record("Graceful Recycled pid={$status['pid']}, code={$status['code']}, signum={$status['signal']}",Log::DEBUG);
  163. }
  164. }
  165. break;
  166. }
  167. else
  168. {
  169. foreach ($workers as $pid => $worker) {
  170. Swoole\Process::kill($pid, SIGTERM);
  171. }
  172. foreach ($workers as $pid => $worker)
  173. {
  174. if($status = Swoole\Process::wait(true)) {
  175. Log::record("Graceful Recycled pid={$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  176. }
  177. }
  178. break;
  179. }
  180. }
  181. Log::record("Quit all",Log::DEBUG);