coall.php 7.2 KB

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