codispatcher.php 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  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. $workers = [];
  80. for ($i = 0; $i < $process_count;$i++)
  81. {
  82. $process = new Swoole\Process(function(Swoole\Process $worker)
  83. {
  84. Base::run_util();
  85. set_error_handler('handle_error');
  86. $waiting_quit = false;
  87. $sub_redis = null;
  88. go(function () use (&$waiting_quit,&$sub_redis) {
  89. sub_message($waiting_quit,$sub_redis,['refill']);
  90. });
  91. $looper = new processor(true);
  92. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$waiting_quit,$sub_redis,$looper)
  93. {
  94. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  95. set_error_handler(null);
  96. try {
  97. $waiting_quit = true;
  98. $sub_redis->close();
  99. }
  100. catch(Exception $ex) {
  101. Log::record($ex->getMessage(),Log::DEBUG);
  102. }
  103. $looper->stop();
  104. do {
  105. $res = Swoole\Coroutine::stats();
  106. $num = $res['coroutine_num'];
  107. if($num > 1) {
  108. Swoole\Coroutine::sleep(0.1);
  109. }
  110. Log::record("coroutine_num = {$num}",Log::DEBUG);
  111. } while($num > 1);
  112. });
  113. $looper->run();
  114. }, false, false, true);
  115. $pid = $process->start();
  116. $workers[$pid] = $process;
  117. }
  118. Log::record("main process start wait sub process....",Log::DEBUG);
  119. while (true)
  120. {
  121. if($status = Swoole\Process::wait(true)) {
  122. Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  123. }
  124. else
  125. {
  126. foreach ($workers as $pid => $worker)
  127. {
  128. Swoole\Process::kill($pid,SIGTERM);
  129. if($status = Swoole\Process::wait(true)) {
  130. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  131. }
  132. }
  133. break;
  134. }
  135. }
  136. Log::record("Quit all",Log::DEBUG);