codispatcher.php 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178
  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. //Co::set(['hook_flags' => 0]);
  19. if (empty($_SERVER['argv'][1])) exit('parameter error');
  20. $process_count = intval($_SERVER['argv'][1]);
  21. function all_channels() {
  22. return ['refill'];
  23. }
  24. function handle_error($level, $message, $file, $line)
  25. {
  26. if($level == E_NOTICE) return;
  27. $trace = "handle_error: level={$level},msg={$message} file={$file},line={$line}\n";
  28. $backtrace = debug_backtrace();
  29. foreach ($backtrace as $item) {
  30. $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
  31. }
  32. Log::record($trace,Log::ERR);
  33. }
  34. function sub_message(&$waiting_quit,&$sub_redis,$channels)
  35. {
  36. $sub_redis = new Swoole\Coroutine\Redis();
  37. while (!$waiting_quit)
  38. {
  39. try
  40. {
  41. if(!$sub_redis->connected) {
  42. $ret = $sub_redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
  43. Log::record("sub_message redis connected = {$sub_redis->connected}",Log::DEBUG);
  44. }
  45. else {
  46. $ret = true;
  47. }
  48. if(!$ret) {
  49. Log::record("sub_message cannot connet redis.",Log::DEBUG);
  50. $sub_redis->close();
  51. Swoole\Coroutine::sleep(1);
  52. }
  53. elseif($sub_redis->subscribe($channels))
  54. {
  55. while ($msg = $sub_redis->recv())
  56. {
  57. [$sub_type, $channel, $content] = $msg;
  58. $content = unserialize($content);
  59. $type = $content['type'];
  60. if($channel != 'refill') continue;
  61. if($type == 'channel' || $type == 'merchant') {
  62. Log::record("sub_message recv mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
  63. refill\RefillFactory::instance()->load();
  64. }
  65. }
  66. Log::record("sub_message redis recv timeout",Log::DEBUG);
  67. }
  68. else {
  69. Log::record("sub_message subscribe error",Log::ERR);
  70. }
  71. }
  72. catch (Exception $ex)
  73. {
  74. Log::record($ex->getMessage(),Log::ERR);
  75. }
  76. }
  77. $waiting_quit = true;
  78. Log::record("sub_message quit",Log::DEBUG);
  79. }
  80. //Co\run(function()
  81. //{
  82. // Swoole\Process::signal(SIGTERM, function($signal_num) {
  83. // Log::record("signal call 1 = $signal_num",Log::DEBUG);
  84. //
  85. // });
  86. //
  87. // Base::run_util();
  88. // set_error_handler('handle_error');
  89. // go(function () {
  90. // sub_message(['refill']);
  91. // });
  92. // $looper = new processor(true);
  93. // $looper->run();
  94. //});
  95. $workers = [];
  96. for ($i = 0; $i < $process_count;$i++)
  97. {
  98. $process = new Swoole\Process(function(Swoole\Process $worker)
  99. {
  100. Base::run_util();
  101. set_error_handler('handle_error');
  102. $waiting_quit = false;
  103. $sub_redis = null;
  104. go(function () use (&$waiting_quit,&$sub_redis) {
  105. sub_message($waiting_quit,$sub_redis,['refill']);
  106. });
  107. $looper = new processor(true);
  108. Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$waiting_quit,$sub_redis,$looper)
  109. {
  110. Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG);
  111. set_error_handler(null);
  112. try {
  113. $waiting_quit = true;
  114. $sub_redis->close();
  115. }
  116. catch(Exception $ex) {
  117. Log::record($ex->getMessage(),Log::DEBUG);
  118. }
  119. $looper->stop();
  120. do {
  121. $res = Swoole\Coroutine::stats();
  122. $num = $res['coroutine_num'];
  123. if($num > 1) {
  124. Swoole\Coroutine::sleep(0.1);
  125. }
  126. Log::record("coroutine_num = {$num}",Log::DEBUG);
  127. } while($num > 1);
  128. });
  129. $looper->run();
  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. {
  144. Swoole\Process::kill($pid,SIGTERM);
  145. if($status = Swoole\Process::wait(true)) {
  146. Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
  147. }
  148. }
  149. break;
  150. }
  151. }
  152. Log::record("Quit all",Log::DEBUG);