|
@@ -24,7 +24,6 @@ require_once(BASE_CORE_PATH . '/framework/libraries/CoMysqliPool.php');
|
|
|
require_once(BASE_CORE_PATH . '/framework/libraries/CoPool.php');
|
|
|
require_once(BASE_CORE_PATH . '/framework/libraries/CoRedisPool.php');
|
|
|
|
|
|
-//Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL | SWOOLE_HOOK_SLEEP | SWOOLE_HOOK_TCP]);
|
|
|
Co::set(['hook_flags' => SWOOLE_HOOK_ALL | SWOOLE_HOOK_NATIVE_CURL]);
|
|
|
if (empty($_SERVER['argv'][1])) exit('parameter error');
|
|
|
$process_count = intval($_SERVER['argv'][1]);
|
|
@@ -49,16 +48,18 @@ function handle_error($level, $message, $file, $line)
|
|
|
Log::record($trace,Log::ERR);
|
|
|
}
|
|
|
|
|
|
-function subscribe_message(&$quit, &$redis, $channels)
|
|
|
+function subscribe_message(&$quit, &$redis, $channels,$looper)
|
|
|
{
|
|
|
- $redis = new Swoole\Coroutine\Redis();
|
|
|
+ $redis = new Redis();
|
|
|
while (!$quit)
|
|
|
{
|
|
|
try
|
|
|
{
|
|
|
Log::record("subscribe_message start quit=" . strbool($quit),Log::DEBUG);
|
|
|
- if(!$redis->connected) {
|
|
|
+ if(!$redis->isConnected()) {
|
|
|
+ $redis->close();
|
|
|
$ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
|
|
|
+ $redis->setOption(Redis::OPT_READ_TIMEOUT, 3600);
|
|
|
}
|
|
|
else {
|
|
|
$ret = true;
|
|
@@ -66,46 +67,45 @@ function subscribe_message(&$quit, &$redis, $channels)
|
|
|
|
|
|
if(!$ret) {
|
|
|
Log::record("subscribe_message cannot connet redis.",Log::DEBUG);
|
|
|
- $redis->close();
|
|
|
+ sleep(1);
|
|
|
+ continue;
|
|
|
}
|
|
|
- elseif($redis->subscribe($channels))
|
|
|
+
|
|
|
+ $ret = $redis->subscribe($channels,function ($redis,$channel,$msg) use(&$quit,$looper)
|
|
|
{
|
|
|
- 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();
|
|
|
- }
|
|
|
- elseif($type == 'ratio') {
|
|
|
- $ins = Cache::getInstance('cacheredis');
|
|
|
- $val = $ins->get_org('channel_ratios');
|
|
|
-
|
|
|
- if(empty($val)) continue;
|
|
|
- $val = json_decode($val,true);
|
|
|
- if(empty($val)) continue;
|
|
|
- $ratios = $val['ratios'];
|
|
|
- if(empty($ratios)) continue;
|
|
|
-
|
|
|
- refill\RefillFactory::instance()->UpdateRatio($ratios);
|
|
|
- }
|
|
|
- else {
|
|
|
- Log::record("subscribe_message dont not handle mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
|
|
|
- }
|
|
|
+ Log::record("channel={$channel} msg={$msg}",Log::DEBUG);
|
|
|
+ $content = json_decode($msg,true);
|
|
|
+ $type = $content['type'];
|
|
|
+
|
|
|
+ if($channel != 'refill') return;
|
|
|
+ if($quit) return;
|
|
|
+
|
|
|
+ if($type == 'channel' || $type == 'merchant') {
|
|
|
+ $looper->pause();
|
|
|
+ refill\RefillFactory::instance()->load();
|
|
|
+ $looper->resume();
|
|
|
}
|
|
|
- }
|
|
|
- else {
|
|
|
- Log::record("subscribe_message subscribe error",Log::ERR);
|
|
|
- }
|
|
|
+ elseif($type == 'ratio') {
|
|
|
+ $ins = Cache::getInstance('cacheredis');
|
|
|
+ $val = $ins->get_org('channel_ratios');
|
|
|
+
|
|
|
+ if(empty($val)) return;
|
|
|
+ $val = json_decode($val,true);
|
|
|
+ if(empty($val)) return;
|
|
|
+ $ratios = $val['ratios'];
|
|
|
+ if(empty($ratios)) return;
|
|
|
+
|
|
|
+ refill\RefillFactory::instance()->UpdateRatio($ratios);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Log::record("subscribe_message dont not handle mgs:{$channel}-{$type}",Log::DEBUG);
|
|
|
+ }
|
|
|
+ });
|
|
|
+ Log::record("subscribe ret={$ret}",Log::DEBUG);
|
|
|
}
|
|
|
catch (Exception $ex)
|
|
|
{
|
|
|
- Log::record($ex->getMessage(),Log::ERR);
|
|
|
+ Log::record("subscribe_message " . $ex->getMessage(), Log::ERR);
|
|
|
}
|
|
|
}
|
|
|
|
|
@@ -123,11 +123,12 @@ for ($i = 0; $i < $process_count;$i++)
|
|
|
|
|
|
$sub_quit = false;
|
|
|
$sub_redis = null;
|
|
|
- go(function () use (&$sub_quit,&$sub_redis) {
|
|
|
- subscribe_message($sub_quit,$sub_redis,['refill']);
|
|
|
+ $looper = new processor(false);
|
|
|
+
|
|
|
+ go(function () use (&$sub_quit,&$sub_redis,$looper) {
|
|
|
+ subscribe_message($sub_quit,$sub_redis,['refill'],$looper);
|
|
|
});
|
|
|
|
|
|
- $looper = new processor(false);
|
|
|
go(function () use ($looper) {
|
|
|
$looper->run();
|
|
|
});
|
|
@@ -154,6 +155,7 @@ for ($i = 0; $i < $process_count;$i++)
|
|
|
}
|
|
|
Log::record("coroutine_num = {$num}",Log::DEBUG);
|
|
|
} while($num > 1);
|
|
|
+
|
|
|
CoRedisPool::instance()->stop();
|
|
|
CoMysqliPool::instance()->stop();
|
|
|
});
|
|
@@ -173,9 +175,12 @@ while (true)
|
|
|
}
|
|
|
else
|
|
|
{
|
|
|
+ foreach ($workers as $pid => $worker) {
|
|
|
+ Swoole\Process::kill($pid, SIGTERM);
|
|
|
+ }
|
|
|
+
|
|
|
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);
|
|
|
}
|