codispatcher.php 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205
  1. <?php
  2. declare(strict_types=0);
  3. define('APP_ID', 'cordispatcher');
  4. define('MOBILE_SERVER',true);
  5. define('USE_COROUTINE',true);
  6. define('SUPPORT_PTHREAD',false);
  7. define('BASE_ROOT_PATH',str_replace('/rdispatcher','',dirname(__FILE__)));
  8. define('BASE_PATH',BASE_ROOT_PATH . '/rdispatcher');
  9. require_once(BASE_ROOT_PATH . '/global.php');
  10. require_once(BASE_ROOT_PATH . '/fooder.php');
  11. require_once(BASE_HELPER_PATH . '/event_looper.php');
  12. require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
  13. require_once(BASE_HELPER_PATH . '/algorithm.php');
  14. require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
  15. require_once(BASE_PATH . '/processor.php');
  16. require_once(BASE_PATH . '/proxy.php');
  17. Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL]);
  18. if (empty($_SERVER['argv'][1])) exit('parameter error');
  19. $process_count = intval($_SERVER['argv'][1]);
  20. function all_channels() {
  21. return ['refill'];
  22. }
  23. function strbool($value) {
  24. return $value ? 'true' : 'false';
  25. }
  26. function handle_error($level, $message, $file, $line)
  27. {
  28. if($level == E_NOTICE) return;
  29. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  30. $backtrace = debug_backtrace();
  31. foreach ($backtrace as $item) {
  32. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  33. }
  34. Log::record($trace,Log::ERR);
  35. }
  36. function subscribe_message(&$quit, &$redis, $channels)
  37. {
  38. $redis = new Swoole\Coroutine\Redis();
  39. while (!$quit)
  40. {
  41. try
  42. {
  43. Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
  44. if(!$redis->connected) {
  45. $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  46. Log::record("subscribe_message redis connected = {$redis->connected}",Log::DEBUG);
  47. }
  48. else {
  49. $ret = true;
  50. }
  51. if(!$ret) {
  52. Log::record("subscribe_message cannot connet redis.",Log::DEBUG);
  53. $redis->close();
  54. Swoole\Coroutine::sleep(1);
  55. }
  56. elseif($redis->subscribe($channels))
  57. {
  58. while ($msg = $redis->recv())
  59. {
  60. [$sub_type, $channel, $content] = $msg;
  61. $content = unserialize($content);
  62. $type = $content['type'];
  63. if($channel != 'refill') continue;
  64. if($quit) break;
  65. if($type == 'channel' || $type == 'merchant') {
  66. Log::record("subscribe_message recv mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  67. refill\RefillFactory::instance()->load();
  68. }
  69. }
  70. Log::record("subscribe_message redis recv timeout",Log::DEBUG);
  71. }
  72. else {
  73. Log::record("subscribe_message subscribe error",Log::ERR);
  74. }
  75. }
  76. catch (Exception $ex)
  77. {
  78. Log::record($ex->getMessage(),Log::ERR);
  79. }
  80. }
  81. Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
  82. $quit = true;
  83. }
  84. function ratio_update(&$quit)
  85. {
  86. //每分钟的第0秒 将上一分钟的数据,写入HD5File。
  87. //每分钟的第2秒 统计之前的数据
  88. //每分钟的第3秒触发更新 成功率统计数据
  89. $cur_min = 0;
  90. while (!$quit)
  91. {
  92. Log::record("ratio_update start calc quit =" . strbool($quit),Log::DEBUG);
  93. for ($i = 0; $i < 61; $i++) {
  94. $cur_time = time();
  95. $time_sec = $cur_time;
  96. $next_min = $time_sec - $time_sec % 60;
  97. if($quit) break;
  98. if($next_min > $cur_min && $time_sec % 60 == 3) {
  99. $cur_min = $next_min;
  100. break;
  101. } else {
  102. Swoole\Coroutine::sleep(1);
  103. }
  104. }
  105. if(!$quit) {
  106. refill\RefillFactory::instance()->UpdateRatio();
  107. }
  108. }
  109. Log::record("ratio_update quit =". strbool($quit),Log::DEBUG);
  110. $quit = true;
  111. }
  112. $workers = [];
  113. for ($i = 0; $i < $process_count;$i++)
  114. {
  115. $process = new Swoole\Process(function(Swoole\Process $worker)
  116. {
  117. Base::run_util();
  118. set_error_handler('handle_error');
  119. $sub_quit = false;
  120. $sub_redis = null;
  121. go(function () use (&$sub_quit,&$sub_redis) {
  122. subscribe_message($sub_quit,$sub_redis,['refill']);
  123. });
  124. $ratio_quit = false;
  125. go(function () use (&$ratio_quit) {
  126. ratio_update($ratio_quit);
  127. });
  128. $looper = new processor(true);
  129. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,&$ratio_quit,$looper)
  130. {
  131. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  132. set_error_handler(null);
  133. try {
  134. $sub_quit = true;
  135. $ratio_quit = true;
  136. $sub_redis->close();
  137. }
  138. catch(Exception $ex) {
  139. Log::record($ex->getMessage(),Log::DEBUG);
  140. }
  141. $looper->stop();
  142. do {
  143. $res = Swoole\Coroutine::stats();
  144. $num = $res['coroutine_num'];
  145. if($num > 1) {
  146. Swoole\Coroutine::sleep(0.1);
  147. }
  148. Log::record("coroutine_num = {$num}",Log::DEBUG);
  149. } while($num > 1);
  150. });
  151. $looper->run();
  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. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  161. }
  162. else
  163. {
  164. foreach ($workers as $pid => $worker)
  165. {
  166. Swoole\Process::kill($pid,SIGTERM);
  167. if($status = Swoole\Process::wait(true)) {
  168. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  169. }
  170. }
  171. break;
  172. }
  173. }
  174. Log::record("Quit all",Log::DEBUG);