codispatcher.php 5.5 KB

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