coall.php 7.2 KB

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