123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240 |
- <?php
- declare(strict_types=0);
- define('APP_ID', 'cordispatcher');
- define('MOBILE_SERVER',true);
- define('USE_COROUTINE',true);
- define('SUPPORT_PTHREAD',false);
- define('SUPPORT_UNUSE_PDLOG',true);
- define('BASE_ROOT_PATH',str_replace('/rdispatcher','',dirname(__FILE__)));
- define('BASE_PATH',BASE_ROOT_PATH . '/rdispatcher');
- require_once(BASE_ROOT_PATH . '/global.php');
- require_once(BASE_ROOT_PATH . '/fooder.php');
- require_once(BASE_HELPER_PATH . '/event_looper.php');
- require_once(BASE_HELPER_PATH . '/queue/rdispatcher.php');
- require_once(BASE_HELPER_PATH . '/algorithm.php');
- require_once(BASE_HELPER_PATH . '/refill/RefillFactory.php');
- require_once(BASE_PATH . '/processor.php');
- require_once(BASE_PATH . '/proxy.php');
- Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL|SWOOLE_HOOK_SLEEP]);
- Co::set(['socket_connect_timeout' => 60,'socket_read_timeout' => 900,'socket_write_timeout' => 900,'socket_timeout' => 900]);
- if (empty($_SERVER['argv'][1])) exit('parameter error');
- $process_count = intval($_SERVER['argv'][1]);
- function all_channels() {
- return ['refill'];
- }
- function strbool($value) {
- return $value ? 'true' : 'false';
- }
- function handle_error($level, $message, $file, $line)
- {
- if($level == E_NOTICE) return;
- $trace = "handle_error: level=$level msg=$message file=$file,line=$line\n";
- $backtrace = debug_backtrace();
- foreach ($backtrace as $item) {
- $trace .= "{$item['file']}\t{$item['line']}\t{$item['function']}\n";
- }
- Log::record($trace,Log::ERR);
- }
- function subscribe_message(&$quit, &$redis, $channels)
- {
- $redis = new Swoole\Coroutine\Redis();
- while (!$quit)
- {
- try
- {
- Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
- if(!$redis->connected) {
- $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
- }
- else {
- $ret = true;
- }
- if(!$ret) {
- Log::record("subscribe_message cannot connect redis.",Log::DEBUG);
- $redis->close();
- }
- elseif($redis->subscribe($channels))
- {
- while ($msg = $redis->recv())
- {
- [$sub_type, $channel, $content] = $msg;
- $content = json_decode($content,true);
- $type = $content['type'];
- if($channel != 'refill') continue;
- if($quit) break;
- if($type == 'channel' || $type == 'merchant') {
- refill\RefillFactory::instance()->load();
- refill\transfer::instance()->load();
- refill\transfer_timeout::instance()->load();
- refill\EventManager::instance()->load();
- }
- elseif($type == 'mch_profit_ratio') {
- $ins = Cache::getInstance('cacheredis');
- $content = $ins->get_org('refill_merchant_profit_ratio');
- if(empty($content)) continue;
- $counts = json_decode($content,true);
- if(empty($counts)) continue;
- $gross = $counts['gross'];
- $detail = $counts['detail'];
- $types = $counts['types'];
- refill\RefillFactory::instance()->UpdateMchRatios($gross,$detail,$types);
- }
- elseif($type == 'channel_control') {
- $ins = Cache::getInstance('cacheredis');
- $content = $ins->get_org('refill_channel_control_model');
- if(empty($content)) continue;
- $params = json_decode($content,true);
- if(empty($params)) continue;
- refill\RefillFactory::instance()->UpdateChctl($params);
- }
- elseif($type == 'channels_cur_speed') { //对通道整体限速
- $ins = Cache::getInstance('cacheredis');
- $content = $ins->get_org('refill_channels_cur_speed');
- if(empty($content)) continue;
- $params = json_decode($content,true);
- if(empty($params)) continue;
- refill\RefillFactory::instance()->UpdateChspeed($params);
- }
- elseif($type == 'channel_speed') { //人工智能分配的最大速度
- $ins = Cache::getInstance('cacheredis');
- $content = $ins->get_org('refill_channel_control_speed');
- if(empty($content)) continue;
- $params = json_decode($content,true);
- if(empty($params)) continue;
- refill\RefillFactory::instance()->UpdateMaxSpeed($params);
- }
- else {
- //Log::record("subscribe_message dont not handle mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
- }
- }
- }
- else {
- Log::record("subscribe_message subscribe error",Log::ERR);
- }
- }
- catch (Exception $ex)
- {
- Log::record($ex->getMessage(),Log::ERR);
- }
- }
- Log::record("subscribe_message quit =" . strbool($quit),Log::DEBUG);
- $quit = true;
- }
- $workers = [];
- for ($i = 0; $i < $process_count;$i++)
- {
- $process = new Swoole\Process(function(Swoole\Process $worker)
- {
- Base::run_util();
- refill\RefillFactory::instance();
- refill\transfer::instance();
- $sub_quit = false;
- $sub_redis = null;
- $looper = new processor(false);
- set_error_handler('handle_error');
- register_shutdown_function(function () use ($looper)
- {
- $error = error_get_last();
- if(!empty($error) && $error['type'] != E_NOTICE) {
- $msg = "register_shutdown_function type:{$error['type']}\n message:{$error['message']}\n file={$error['file']} line={$error['line']}";
- Log::record("$msg",Log::ERR);
- }
- });
- go(function () use (&$sub_quit,&$sub_redis) {
- subscribe_message($sub_quit,$sub_redis,['refill']);
- });
- go(function () use ($looper) {
- $looper->run();
- });
- Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,$looper)
- {
- Log::record("signal call SIGTERM begin signum={$signal_num}, pid={$worker->pid}",Log::DEBUG);
- set_error_handler(null);
- try {
- $sub_quit = true;
- $sub_redis->close();
- }
- catch(Exception $ex) {
- Log::record($ex->getMessage(),Log::DEBUG);
- }
- $looper->stop();
- });
- }, false, false, true);
- $pid = $process->start();
- $workers[$pid] = $process;
- }
- Log::record("main process start wait sub process....",Log::DEBUG);
- while (true)
- {
- if($status = Swoole\Process::wait(true))
- {
- $quit_pid = $status['pid'];
- Log::record("Sub process pid={$status['pid']} quit, code={$status['code']}, signum={$status['signal']}",Log::DEBUG);
- foreach ($workers as $pid => $worker)
- {
- if($pid != $quit_pid) {
- Swoole\Process::kill($pid, SIGTERM);
- }
- }
- foreach ($workers as $pid => $worker)
- {
- if($pid != $quit_pid && ($status = Swoole\Process::wait(true))) {
- Log::record("Graceful Recycled pid={$status['pid']}, code={$status['code']}, signum={$status['signal']}",Log::DEBUG);
- }
- }
- break;
- }
- else
- {
- foreach ($workers as $pid => $worker) {
- Swoole\Process::kill($pid, SIGTERM);
- }
- foreach ($workers as $pid => $worker)
- {
- if($status = Swoole\Process::wait(true)) {
- Log::record("Graceful Recycled pid={$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
- }
- }
- break;
- }
- }
- Log::record("Quit all",Log::DEBUG);
|