diff --git a/CHANGELOG.md b/CHANGELOG.md index 460c1c9..86bd9dd 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -19,6 +19,7 @@ - **system:** can get more system info via class SystemInfoHelper ### Fix +- **Anonymous:** Fix Auth Page 500 after commit `2cd1a499` - **Cookies:** Fix session sep from `%` to `_` - **Database:** Fix table `links` miss - **Env:** Exit when parse env file failed diff --git a/apps/config/httpd.php b/apps/config/httpd.php index 17cc6df..b00ba47 100644 --- a/apps/config/httpd.php +++ b/apps/config/httpd.php @@ -43,6 +43,16 @@ ], + // 用户自定义进程 (用于常驻的任务清理,将会使用Server->addProcess添加到Server上 + 'process' => [ + //'tracker' => [ + // 'class' => \apps\process\EchoProcess::class, + // 'title' => 'Test', + // 'components' => ['log','pdo','redis','config','site'], + // 'sleep' => 5, + //] + ], + // 类库配置 'libraries' => [ diff --git a/apps/process/EchoProcess.php b/apps/process/EchoProcess.php new file mode 100644 index 0000000..33acf09 --- /dev/null +++ b/apps/process/EchoProcess.php @@ -0,0 +1,21 @@ +_config = $config; + + while (true) { + $this->start(); + sleep($this->_config['sleep']); + } + } +} diff --git a/framework/Http/Application.php b/framework/Http/Application.php index 3ab3f13..15d618b 100644 --- a/framework/Http/Application.php +++ b/framework/Http/Application.php @@ -156,9 +156,10 @@ public function __get($name) } // 装载全部组件 - public function loadAllComponents() + public function loadAllComponents($components = null) { - foreach (array_keys($this->components) as $name) { + $components = $components ?? $this->components; + foreach (array_keys($components) as $name) { $this->loadComponent($name); } } diff --git a/framework/Http/HttpServer.php b/framework/Http/HttpServer.php index 851710b..9b2bb20 100644 --- a/framework/Http/HttpServer.php +++ b/framework/Http/HttpServer.php @@ -4,6 +4,7 @@ use Rid\Base\BaseObject; +use Rid\Base\Process; use Rid\Base\Timer; use Rid\Helpers\ProcessHelper; @@ -71,6 +72,9 @@ public function start() $this->_server->on('workerExit', [$this, 'onWorkerExit']); $this->_server->on('request', [$this, 'onRequest']); + // 增加自定义进程 + $this->addCustomProcess(); + // 欢迎信息 $this->welcome(); @@ -136,7 +140,7 @@ public function onWorkerStart(\Swoole\Http\Server $server, int $workerId) // 进程命名 if ($workerId < $server->setting['worker_num']) { - ProcessHelper::setTitle("rid-httpd: worker #{$workerId}"); + ProcessHelper::setTitle("rid-httpd: http worker #{$workerId}"); } else { ProcessHelper::setTitle("rid-httpd: task #{$workerId}"); } @@ -210,6 +214,31 @@ public function onRequest(\Swoole\Http\Request $request, \Swoole\Http\Response $ } } + private function addCustomProcess() + { + foreach (app()->env('process') as $process_name => $process_config) { + $process_class = $process_config['class']; + $custom_process = new $process_class(); + if ($custom_process instanceof Process) { + $process = new \Swoole\Process(function ($process) use ($process_name, $process_config, $custom_process) { + + if ($process_config['title']) ProcessHelper::setTitle($process_config['title']); + + // 实例化App + $config = require $this->virtualHost['configFile']; + + $app = new Application($config); + $app->setServ($this->_server); + $app->loadAllComponents(array_flip($process_config['components'])); + + $custom_process->run($process_config); + }); + + $this->_server->addProcess($process); + } + } + } + // 欢迎信息 protected function welcome() {