coall.php 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  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 . '/event_looper.php');
  13. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  14. require_once(BASE_HELPER_PATH . '/algorithm.php');
  15. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  16. require_once(BASE_PATH . '/processor.php');
  17. require_once(BASE_PATH . '/proxy.php');
  18. require_once(BASE_CORE_PATH . '/framework/libraries/CoRefPool.php');
  19. require_once(BASE_CORE_PATH . '/framework/libraries/CoMysqliPool.php');
  20. require_once(BASE_CORE_PATH . '/framework/libraries/CoPool.php');
  21. require_once(BASE_CORE_PATH . '/framework/libraries/CoRedisPool.php');
  22. Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL | SWOOLE_HOOK_SLEEP | SWOOLE_HOOK_TCP]);
  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)
  42. {
  43. $redis = new Swoole\Coroutine\Redis();
  44. while (!$quit)
  45. {
  46. try
  47. {
  48. Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
  49. if(!$redis->connected) {
  50. $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  51. }
  52. else {
  53. $ret = true;
  54. }
  55. if(!$ret) {
  56. Log::record("subscribe_message cannot connet redis.",Log::DEBUG);
  57. $redis->close();
  58. }
  59. elseif($redis->subscribe($channels))
  60. {
  61. while ($msg = $redis->recv())
  62. {
  63. [$sub_type, $channel, $content] = $msg;
  64. $content = json_decode($content,true);
  65. $type = $content['type'];
  66. if($channel != 'refill') continue;
  67. if($quit) break;
  68. if($type == 'channel' || $type == 'merchant') {
  69. refill\RefillFactory::instance()->load();
  70. }
  71. elseif($type == 'ratio') {
  72. $ins = Cache::getInstance('cacheredis');
  73. $val = $ins->get_org('channel_ratios');
  74. if(empty($val)) continue;
  75. $val = json_decode($val,true);
  76. if(empty($val)) continue;
  77. $ratios = $val['ratios'];
  78. if(empty($ratios)) continue;
  79. refill\RefillFactory::instance()->UpdateRatio($ratios);
  80. }
  81. else {
  82. Log::record("subscribe_message dont not handle mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  83. }
  84. }
  85. }
  86. else {
  87. Log::record("subscribe_message subscribe error",Log::ERR);
  88. }
  89. }
  90. catch (Exception $ex)
  91. {
  92. Log::record($ex->getMessage(),Log::ERR);
  93. }
  94. }
  95. Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
  96. $quit = true;
  97. }
  98. $workers = [];
  99. for ($i = 0; $i < $process_count;$i++)
  100. {
  101. $process = new Swoole\Process(function(Swoole\Process $worker)
  102. {
  103. Base::run_util();
  104. set_error_handler('handle_error');
  105. $sub_quit = false;
  106. $sub_redis = null;
  107. go(function () use (&$sub_quit,&$sub_redis) {
  108. subscribe_message($sub_quit,$sub_redis,['refill']);
  109. });
  110. $looper = new processor(false);
  111. go(function () use ($looper) {
  112. $looper->run();
  113. });
  114. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,$looper)
  115. {
  116. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  117. set_error_handler(null);
  118. try {
  119. $sub_quit = true;
  120. $sub_redis->close();
  121. }
  122. catch(Exception $ex) {
  123. Log::record($ex->getMessage(),Log::DEBUG);
  124. }
  125. $looper->stop();
  126. do {
  127. $res = Swoole\Coroutine::stats();
  128. $num = $res['coroutine_num'];
  129. if($num > 1) {
  130. Swoole\Coroutine::sleep(0.1);
  131. }
  132. Log::record("coroutine_num = {$num}",Log::DEBUG);
  133. } while($num > 1);
  134. CoRedisPool::instance()->stop();
  135. CoMysqliPool::instance()->stop();
  136. });
  137. }, false, false, true);
  138. $pid = $process->start();
  139. $workers[$pid] = $process;
  140. }
  141. Log::record("main process start wait sub process....",Log::DEBUG);
  142. while (true)
  143. {
  144. if($status = Swoole\Process::wait(true)) {
  145. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  146. }
  147. else
  148. {
  149. foreach ($workers as $pid => $worker)
  150. {
  151. Swoole\Process::kill($pid,SIGTERM);
  152. if($status = Swoole\Process::wait(true)) {
  153. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  154. }
  155. }
  156. break;
  157. }
  158. }
  159. Log::record("Quit all",Log::DEBUG);