|
@@ -1,7 +1,7 @@
|
|
|
<?php
|
|
|
declare(strict_types=0);
|
|
|
|
|
|
-define('APP_ID', 'rdispatcher');
|
|
|
+define('APP_ID', 'cordispatcher');
|
|
|
define('MOBILE_SERVER',true);
|
|
|
define('USE_COROUTINE',true);
|
|
|
define('SUPPORT_PTHREAD',false);
|
|
@@ -21,29 +21,10 @@ require_once(BASE_PATH . '/processor.php');
|
|
|
require_once(BASE_PATH . '/coprocessor.php');
|
|
|
require_once(BASE_PATH . '/proxy.php');
|
|
|
|
|
|
-//这个可以
|
|
|
-//Co::set(['hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP ^ SWOOLE_HOOK_UDP ^
|
|
|
-// SWOOLE_HOOK_FILE ^ SWOOLE_HOOK_UNIX ^
|
|
|
-// SWOOLE_HOOK_STREAM_FUNCTION ^
|
|
|
-// SWOOLE_HOOK_BLOCKING_FUNCTION ^
|
|
|
-// SWOOLE_HOOK_PROC ^
|
|
|
-// SWOOLE_HOOK_SLEEP ^
|
|
|
-// SWOOLE_HOOK_TLS ^
|
|
|
-// SWOOLE_HOOK_SSL
|
|
|
-//]);
|
|
|
-
|
|
|
-//这样会导致,curl 协程部分不执行,收不到数据
|
|
|
-//Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL | SWOOLE_HOOK_SOCKETS]);
|
|
|
-
|
|
|
-//这样会导致,redis任何阻塞函数不返回
|
|
|
-//Co::set(['hook_flags' => SWOOLE_HOOK_ALL]);
|
|
|
-
|
|
|
-//目前没发现问题
|
|
|
-//Co::set(['hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP]);
|
|
|
Co::set(['hook_flags' => SWOOLE_HOOK_NATIVE_CURL]);
|
|
|
|
|
|
if (empty($_SERVER['argv'][1])) exit('parameter error');
|
|
|
-$count = intval($_SERVER['argv'][1]);
|
|
|
+$process_count = intval($_SERVER['argv'][1]);
|
|
|
|
|
|
function all_channels() {
|
|
|
return ['refill'];
|
|
@@ -61,46 +42,81 @@ function handle_error($level, $message, $file, $line)
|
|
|
Log::record($trace,Log::ERR);
|
|
|
}
|
|
|
|
|
|
+function sub_message($channels)
|
|
|
+{
|
|
|
+ while (true)
|
|
|
+ {
|
|
|
+ try {
|
|
|
+ $redis = new Swoole\Coroutine\Redis();
|
|
|
+ $ret = $redis->connect(C('coroutine.redis_host'), C('coroutine.redis_port'));
|
|
|
+
|
|
|
+ if(!$ret) {
|
|
|
+ Log::record("sub_message cannot connet redis.",Log::DEBUG);
|
|
|
+ $redis->close();
|
|
|
+ Swoole\Coroutine::sleep(1);
|
|
|
+ }
|
|
|
+ elseif($redis->subscribe($channels))
|
|
|
+ {
|
|
|
+ Log::record("sub_message starting recv",Log::DEBUG);
|
|
|
+ while ($msg = $redis->recv())
|
|
|
+ {
|
|
|
+ [$sub_type, $channel, $content] = $msg;
|
|
|
+ Log::record("sub_message recv mgs:{$sub_type}-{$channel}-{$content}",Log::DEBUG);
|
|
|
+
|
|
|
+ $content = unserialize($content);
|
|
|
+ $type = $content['type'];
|
|
|
+ Log::record("sub_message recv mgs:{$sub_type}-{$channel}-{$type}",Log::DEBUG);
|
|
|
+
|
|
|
+ if($channel == 'refill' && $type == 'channel') {
|
|
|
+ Log::record("recv publish message reload refill",Log::DEBUG);
|
|
|
+ refill\RefillFactory::instance()->load();
|
|
|
+ }
|
|
|
+ }
|
|
|
+ $redis->close();
|
|
|
+ Log::record("sub_message redis close",Log::DEBUG);
|
|
|
+ }
|
|
|
+ else {
|
|
|
+ Log::record("subscribe publish message error",Log::ERR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+ catch (Exception $ex)
|
|
|
+ {
|
|
|
+ Log::record($ex->getMessage(),Log::ERR);
|
|
|
+ }
|
|
|
+ }
|
|
|
+}
|
|
|
|
|
|
-Co\run(function() {
|
|
|
- Base::run_util();
|
|
|
- set_error_handler('handle_error');
|
|
|
- $looper = new coprocessor();
|
|
|
- $looper->run();
|
|
|
-});
|
|
|
|
|
|
-//function work_proc()
|
|
|
+//Co\run(function()
|
|
|
//{
|
|
|
// Base::run_util();
|
|
|
-//
|
|
|
-//// go(function () {
|
|
|
-//// $redis = new Swoole\Coroutine\Redis;
|
|
|
-//// $ret = $redis->connect('host.docker.internal', 6379);
|
|
|
-//// $key = 'test_cache_data';
|
|
|
-//// $ret = wkcache($key, ["hello world"]);
|
|
|
-//// $data = rkcache($key);
|
|
|
-//// });
|
|
|
-//
|
|
|
// set_error_handler('handle_error');
|
|
|
-// $looper = new processor();
|
|
|
+// go(function () {
|
|
|
+// sub_message(['refill']);
|
|
|
+// });
|
|
|
+// $looper = new processor(true);
|
|
|
// $looper->run();
|
|
|
-//}
|
|
|
-//
|
|
|
-//Co\run(function() {
|
|
|
-// Log::record("start run process",Log::DEBUG);
|
|
|
-// work_proc();
|
|
|
//});
|
|
|
|
|
|
-//swoole 和 pthreads 冲突
|
|
|
-//function corun()
|
|
|
-//{
|
|
|
-// Co::set(['hook_flags' => SWOOLE_HOOK_ALL ^ SWOOLE_HOOK_TCP]);
|
|
|
-// Co\run(function() {
|
|
|
-// Log::record("start run process",Log::DEBUG);
|
|
|
-// work_proc();
|
|
|
-// });
|
|
|
-//}
|
|
|
-////corun();
|
|
|
-//work_proc();
|
|
|
+for ($i = 0; $i < $process_count;$i++)
|
|
|
+{
|
|
|
+ $process = new Swoole\Process(function(Swoole\Process $worker)
|
|
|
+ {
|
|
|
+ Base::run_util();
|
|
|
+ set_error_handler('handle_error');
|
|
|
+ go(function () {
|
|
|
+ sub_message(['refill']);
|
|
|
+ });
|
|
|
+ $looper = new processor(true);
|
|
|
+ $looper->run();
|
|
|
+
|
|
|
+ }, false, false, true);
|
|
|
+
|
|
|
+ $process->start();
|
|
|
+}
|
|
|
|
|
|
-//event\util::fork_workerex('corun',$count);
|
|
|
+for ($i = 0; $i < $process_count;$i++)
|
|
|
+{
|
|
|
+ $status = Swoole\Process::wait(true);
|
|
|
+ Log::record("Recycled #{$status['pid']}, code={$status['code']}, signal={$status['signal']}",Log::DEBUG);
|
|
|
+}
|