Skip to content
This repository has been archived by the owner on Nov 2, 2020. It is now read-only.

Commit

Permalink
feat(Process): Add custom Process Support
Browse files Browse the repository at this point in the history
  • Loading branch information
Rhilip committed Jul 28, 2019
1 parent 2c3a2fd commit 7057f26
Show file tree
Hide file tree
Showing 6 changed files with 96 additions and 3 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
10 changes: 10 additions & 0 deletions apps/config/httpd.php
Original file line number Diff line number Diff line change
Expand Up @@ -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' => [

Expand Down
21 changes: 21 additions & 0 deletions apps/process/EchoProcess.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 7/28/2019
* Time: 10:22 PM
*/

namespace apps\process;


use Rid\Base\Process;

class EchoProcess extends Process
{
public function start()
{
println(time() . config('base.site_name'));
sleep(5);
}
}
31 changes: 31 additions & 0 deletions framework/Base/Process.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
<?php
/**
* Created by PhpStorm.
* User: Rhilip
* Date: 7/28/2019
* Time: 10:09 PM
*/

namespace Rid\Base;


class Process implements StaticInstanceInterface
{

use StaticInstanceTrait;

protected $_config;

public function start() {

}

final public function run($config) {
$this->_config = $config;

while (true) {
$this->start();
sleep($this->_config['sleep']);
}
}
}
5 changes: 3 additions & 2 deletions framework/Http/Application.php
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}
Expand Down
31 changes: 30 additions & 1 deletion framework/Http/HttpServer.php
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@

use Rid\Base\BaseObject;

use Rid\Base\Process;
use Rid\Base\Timer;
use Rid\Helpers\ProcessHelper;

Expand Down Expand Up @@ -71,6 +72,9 @@ public function start()
$this->_server->on('workerExit', [$this, 'onWorkerExit']);
$this->_server->on('request', [$this, 'onRequest']);

// 增加自定义进程
$this->addCustomProcess();

// 欢迎信息
$this->welcome();

Expand Down Expand Up @@ -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}");
}
Expand Down Expand Up @@ -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()
{
Expand Down

0 comments on commit 7057f26

Please sign in to comment.