codispatcher.php 5.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188
  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 handle_error($level, $message, $file, $line)
  24. {
  25. if($level == E_NOTICE) return;
  26. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  27. $backtrace = debug_backtrace();
  28. foreach ($backtrace as $item) {
  29. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  30. }
  31. Log::record($trace,Log::ERR);
  32. }
  33. function sub_message(&$waiting_quit,&$sub_redis,$channels)
  34. {
  35. $sub_redis = new Swoole\Coroutine\Redis();
  36. while (!$waiting_quit)
  37. {
  38. try
  39. {
  40. if(!$sub_redis->connected) {
  41. $ret = $sub_redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  42. Log::record("sub_message redis connected = {$sub_redis->connected}",Log::DEBUG);
  43. }
  44. else {
  45. $ret = true;
  46. }
  47. if(!$ret) {
  48. Log::record("sub_message cannot connet redis.",Log::DEBUG);
  49. $sub_redis->close();
  50. Swoole\Coroutine::sleep(1);
  51. }
  52. elseif($sub_redis->subscribe($channels))
  53. {
  54. while ($msg = $sub_redis->recv())
  55. {
  56. [$sub_type, $channel, $content] = $msg;
  57. $content = unserialize($content);
  58. $type = $content['type'];
  59. if($channel != 'refill') continue;
  60. if($type == 'channel' || $type == 'merchant') {
  61. Log::record("sub_message recv mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  62. refill\RefillFactory::instance()->load();
  63. }
  64. }
  65. Log::record("sub_message redis recv timeout",Log::DEBUG);
  66. }
  67. else {
  68. Log::record("sub_message subscribe error",Log::ERR);
  69. }
  70. }
  71. catch (Exception $ex)
  72. {
  73. Log::record($ex->getMessage(),Log::ERR);
  74. }
  75. }
  76. $waiting_quit = true;
  77. Log::record("sub_message quit",Log::DEBUG);
  78. }
  79. function ratio_update(&$waiting_quit)
  80. {
  81. //每分钟的第0秒 将上一分钟的数据,写入HD5File。
  82. //每分钟的第2秒 统计之前的数据
  83. //每分钟的第3秒触发更新 成功率统计数据
  84. while (!$waiting_quit)
  85. {
  86. for ($i = 0; $i < 61; $i++) {
  87. $cur_time = time();
  88. $time_sec = $cur_time;
  89. $next_min = $time_sec - $time_sec % 60;
  90. if(($next_min > $cur_min && $time_sec % 60 == 3) || $waiting_quit) {
  91. $cur_min = $next_min;
  92. break;
  93. } else {
  94. Swoole\Coroutine::sleep(0.1);
  95. }
  96. }
  97. if(!$waiting_quit) {
  98. refill\RefillFactory::instance()->UpdateRatio();
  99. }
  100. }
  101. $waiting_quit = true;
  102. Log::record("ratio_update quit",Log::DEBUG);
  103. }
  104. $workers = [];
  105. for ($i = 0; $i < $process_count;$i++)
  106. {
  107. $process = new Swoole\Process(function(Swoole\Process $worker)
  108. {
  109. Base::run_util();
  110. set_error_handler('handle_error');
  111. $waiting_quit = false;
  112. $sub_redis = null;
  113. go(function () use (&$waiting_quit,&$sub_redis) {
  114. sub_message($waiting_quit,$sub_redis,['refill']);
  115. });
  116. $looper = new processor(true);
  117. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$waiting_quit,$sub_redis,$looper)
  118. {
  119. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  120. set_error_handler(null);
  121. try {
  122. $waiting_quit = true;
  123. $sub_redis->close();
  124. }
  125. catch(Exception $ex) {
  126. Log::record($ex->getMessage(),Log::DEBUG);
  127. }
  128. $looper->stop();
  129. do {
  130. $res = Swoole\Coroutine::stats();
  131. $num = $res['coroutine_num'];
  132. if($num > 1) {
  133. Swoole\Coroutine::sleep(0.1);
  134. }
  135. Log::record("coroutine_num = {$num}",Log::DEBUG);
  136. } while($num > 1);
  137. });
  138. $looper->run();
  139. }, false, false, true);
  140. $pid = $process->start();
  141. $workers[$pid] = $process;
  142. }
  143. Log::record("main process start wait sub process....",Log::DEBUG);
  144. while (true)
  145. {
  146. if($status = Swoole\Process::wait(true)) {
  147. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  148. }
  149. else
  150. {
  151. foreach ($workers as $pid => $worker)
  152. {
  153. Swoole\Process::kill($pid,SIGTERM);
  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);