SWOOLE_HOOK_NATIVE_CURL]); 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')); Log::record("subscribe_message redis connected = {$redis->connected}",Log::DEBUG); } else { $ret = true; } if(!$ret) { Log::record("subscribe_message cannot connet redis.",Log::DEBUG); $redis->close(); Swoole\Coroutine::sleep(1); } elseif($redis->subscribe($channels)) { while ($msg = $redis->recv()) { [$sub_type, $channel, $content] = $msg; $content = unserialize($content); $type = $content['type']; if($channel != 'refill') continue; if($quit) break; if($type == 'channel' || $type == 'merchant') { Log::record("subscribe_message recv mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG); refill\RefillFactory::instance()->load(); } } Log::record("subscribe_message redis recv timeout",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; } function ratio_update(&$quit) { //每分钟的第0秒 将上一分钟的数据,写入HD5File。 //每分钟的第2秒 统计之前的数据 //每分钟的第3秒触发更新 成功率统计数据 $cur_min = 0; while (!$quit) { Log::record("ratio_update start calc quit =" . strbool($quit),Log::DEBUG); for ($i = 0; $i < 61; $i++) { $cur_time = time(); $time_sec = $cur_time; $next_min = $time_sec - $time_sec % 60; if($quit) break; if($next_min > $cur_min && $time_sec % 60 == 3) { $cur_min = $next_min; break; } else { Log::record("ratio_update start sleep",Log::DEBUG); Swoole\Coroutine::sleep(1); Log::record("ratio_update end sleep",Log::DEBUG); } } // if(!$quit) { // refill\RefillFactory::instance()->UpdateRatio(); // } } Log::record("ratio_update 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(); set_error_handler('handle_error'); $sub_quit = false; $sub_redis = null; go(function () use (&$sub_quit,&$sub_redis) { subscribe_message($sub_quit,$sub_redis,['refill']); }); $ratio_quit = false; go(function () use (&$ratio_quit) { ratio_update($ratio_quit); }); $looper = new processor(true); Swoole\Process::signal(SIGTERM, function($signal_num) use ($worker,&$sub_quit,$sub_redis,&$ratio_quit,$looper) { Log::record("signal call SIGTERM begin = $signal_num, #{$worker->pid}",Log::DEBUG); set_error_handler(null); try { $sub_quit = true; $ratio_quit = true; $sub_redis->close(); } catch(Exception $ex) { Log::record($ex->getMessage(),Log::DEBUG); } $looper->stop(); do { $res = Swoole\Coroutine::stats(); $num = $res['coroutine_num']; if($num > 1) { Swoole\Coroutine::sleep(0.1); } Log::record("coroutine_num = {$num}",Log::DEBUG); } while($num > 1); }); $looper->run(); }, 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)) { Log::record("Sub process #{$status['pid']} quit, code={$status['code']}, signal={$status['signal']}",Log::DEBUG); } else { foreach ($workers as $pid => $worker) { Swoole\Process::kill($pid,SIGTERM); if($status = Swoole\Process::wait(true)) { Log::record("Graceful Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG); } } break; } } Log::record("Quit all",Log::DEBUG);