coall.php 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193
  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_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. $ret = $redis->subscribe($channels,function ($redis,$channel,$msg) use(&$quit,$looper)
  63. {
  64. Log::record("channel={$channel} msg={$msg}",Log::DEBUG);
  65. $content = json_decode($msg,true);
  66. $type = $content['type'];
  67. if($channel != 'refill') return;
  68. if($quit) return;
  69. if($type == 'channel' || $type == 'merchant') {
  70. $looper->pause();
  71. refill\RefillFactory::instance()->load();
  72. $looper->resume();
  73. }
  74. elseif($type == 'ratio') {
  75. $ins = Cache::getInstance('cacheredis');
  76. $val = $ins->get_org('channel_ratios');
  77. if(empty($val)) return;
  78. $val = json_decode($val,true);
  79. if(empty($val)) return;
  80. $ratios = $val['ratios'];
  81. if(empty($ratios)) return;
  82. refill\RefillFactory::instance()->UpdateRatio($ratios);
  83. }
  84. else {
  85. Log::record("subscribe_message dont not handle mgs:{$channel}-{$type}",Log::DEBUG);
  86. }
  87. });
  88. Log::record("subscribe ret={$ret}",Log::DEBUG);
  89. }
  90. catch (Exception $ex)
  91. {
  92. Log::record("subscribe_message " . $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. $looper = new processor(false);
  108. go(function () use (&$sub_quit,&$sub_redis,$looper) {
  109. subscribe_message($sub_quit,$sub_redis,['refill'],$looper);
  110. });
  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. Swoole\Process::kill($pid, SIGTERM);
  151. }
  152. foreach ($workers as $pid => $worker)
  153. {
  154. if($status = Swoole\Process::wait(true)) {
  155. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  156. }
  157. }
  158. break;
  159. }
  160. }
  161. Log::record("Quit all",Log::DEBUG);