coall.php 7.3 KB

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