coall.php 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211
  1. <?php
  2. declare(strict_types=0);
  3. define('APP_ID', 'coall');
  4. define('MOBILE_SERVER',true);
  5. define('USE_COROUTINE',true);
  6. define('SUPPORT_PTHREAD',false);
  7. define('COROUTINE_HOOK_TCP',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 . '/algorithm.php');
  13. require_once(BASE_HELPER_PATH . '/event_looper.php');
  14. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  15. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  16. require_once(BASE_CORE_PATH . '/framework/libraries/CoRefPool.php');
  17. require_once(BASE_CORE_PATH . '/framework/libraries/CoMysqliPool.php');
  18. require_once(BASE_CORE_PATH . '/framework/libraries/CoPool.php');
  19. require_once(BASE_CORE_PATH . '/framework/libraries/CoRedisPool.php');
  20. require_once(BASE_PATH . '/processor.php');
  21. require_once(BASE_PATH . '/proxy.php');
  22. Co::set(['hook_flags' => SWOOLE_HOOK_ALL | SWOOLE_HOOK_NATIVE_CURL]);
  23. if (empty($_SERVER['argv'][1])) exit('parameter error');
  24. $process_count = intval($_SERVER['argv'][1]);
  25. function all_channels() {
  26. return ['refill'];
  27. }
  28. function strbool($value) {
  29. return $value ? 'true' : 'false';
  30. }
  31. function handle_error($level, $message, $file, $line)
  32. {
  33. if($level == E_NOTICE) return;
  34. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  35. $backtrace = debug_backtrace();
  36. foreach ($backtrace as $item) {
  37. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  38. }
  39. Log::record($trace,Log::ERR);
  40. }
  41. function subscribe_message(&$quit, &$redis, $channels,$looper)
  42. {
  43. $redis = new Redis();
  44. while (!$quit)
  45. {
  46. try
  47. {
  48. Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
  49. if(!$redis->isConnected()) {
  50. $redis->close();
  51. $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  52. $redis->setOption(Redis::OPT_READ_TIMEOUT, 3600);
  53. }
  54. else {
  55. $ret = true;
  56. }
  57. if(!$ret) {
  58. Log::record("subscribe_message cannot connet redis.",Log::DEBUG);
  59. sleep(1);
  60. continue;
  61. }
  62. $fLoading = false;
  63. $ret = $redis->subscribe($channels,function ($redis,$channel,$msg) use(&$quit,$looper,&$fLoading)
  64. {
  65. Log::record("channel={$channel} msg={$msg}",Log::DEBUG);
  66. $content = json_decode($msg,true);
  67. $type = $content['type'];
  68. if($channel != 'refill') return;
  69. if($quit) return;
  70. if($type == 'channel' || $type == 'merchant') {
  71. $fLoading = true;
  72. $looper->pause();
  73. Log::record("start load.....",Log::DEBUG);
  74. refill\RefillFactory::instance()->load();
  75. Log::record("fini load.....",Log::DEBUG);
  76. $looper->resume();
  77. $fLoading = false;
  78. }
  79. elseif($type == 'ratio')
  80. {
  81. if($fLoading == false)
  82. {
  83. $ins = Cache::getInstance('cacheredis');
  84. $val = $ins->get_org('channel_ratios');
  85. if(empty($val)) return;
  86. $val = json_decode($val,true);
  87. if(empty($val)) return;
  88. $ratios = $val['ratios'];
  89. if(empty($ratios)) return;
  90. refill\RefillFactory::instance()->UpdateRatio($ratios);
  91. }
  92. }
  93. else {
  94. Log::record("subscribe_message dont not handle mgs:{$channel}-{$type}",Log::DEBUG);
  95. }
  96. });
  97. Log::record("subscribe ret={$ret}",Log::DEBUG);
  98. }
  99. catch (Exception $ex)
  100. {
  101. Log::record("subscribe_message " . $ex->getMessage(), Log::ERR);
  102. }
  103. }
  104. Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
  105. $quit = true;
  106. }
  107. $workers = [];
  108. for ($i = 0; $i < $process_count;$i++)
  109. {
  110. $process = new Swoole\Process(function(Swoole\Process $worker)
  111. {
  112. Base::run_util();
  113. refill\RefillFactory::instance();
  114. set_error_handler('handle_error');
  115. $sub_quit = false;
  116. $sub_redis = null;
  117. $looper = new processor(false);
  118. go(function () use (&$sub_quit,&$sub_redis,$looper) {
  119. subscribe_message($sub_quit,$sub_redis,['refill'],$looper);
  120. });
  121. go(function () use ($looper) {
  122. $looper->run();
  123. });
  124. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,$looper)
  125. {
  126. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  127. set_error_handler(null);
  128. try {
  129. $sub_quit = true;
  130. $sub_redis->close();
  131. }
  132. catch(Exception $ex) {
  133. Log::record($ex->getMessage(),Log::DEBUG);
  134. }
  135. $looper->stop();
  136. CoRedisPool::instance()->stop();
  137. CoMysqliPool::instance()->stop();
  138. });
  139. }, false, false, true);
  140. $pid = $process->start();
  141. $workers[$pid] = $process;
  142. }
  143. Log::record("main process start wait sub process....",Log::DEBUG);
  144. while (true)
  145. {
  146. if($status = Swoole\Process::wait(true))
  147. {
  148. $quit_pid = $status['pid'];
  149. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  150. foreach ($workers as $pid => $worker)
  151. {
  152. if($pid != $quit_pid) {
  153. Swoole\Process::kill($pid, SIGTERM);
  154. }
  155. }
  156. foreach ($workers as $pid => $worker)
  157. {
  158. if($pid != $quit_pid && ($status = Swoole\Process::wait(true)))
  159. {
  160. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  161. }
  162. }
  163. break;
  164. }
  165. else
  166. {
  167. foreach ($workers as $pid => $worker) {
  168. Swoole\Process::kill($pid, SIGTERM);
  169. }
  170. foreach ($workers as $pid => $worker)
  171. {
  172. if($status = Swoole\Process::wait(true)) {
  173. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  174. }
  175. }
  176. break;
  177. }
  178. }
  179. Log::record("Quit all",Log::DEBUG);