Skip to content

Commit

Permalink
up
Browse files Browse the repository at this point in the history
  • Loading branch information
kiss291323003 committed Feb 24, 2020
1 parent 43eda22 commit 8cd55da
Show file tree
Hide file tree
Showing 5 changed files with 147 additions and 33 deletions.
15 changes: 14 additions & 1 deletion src/Core.php
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,8 @@
use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\AbstractInterface\Event;
use EasySwoole\EasySwoole\Crontab\Crontab;
use EasySwoole\EasySwoole\Process\BaseService;
use EasySwoole\EasySwoole\Process\Manager;
use EasySwoole\EasySwoole\Swoole\EventHelper;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use EasySwoole\EasySwoole\Task\TaskManager;
Expand All @@ -28,6 +30,7 @@
use EasySwoole\Trigger\Trigger as DefaultTrigger;
use EasySwoole\Task\Config as TaskConfig;
use Swoole\Timer;
use EasySwoole\Component\Process\Config as ProcessConfig;

////////////////////////////////////////////////////////////////////
// _ooOoo_ //
Expand Down Expand Up @@ -296,16 +299,26 @@ public function loadEnv()

private function extraHandler()
{
$serverName = Config::getInstance()->getConf('SERVER_NAME');
//注册crontab进程
Crontab::getInstance()->__run();
//注册Task进程
$config = Config::getInstance()->getConf('MAIN_SERVER.TASK');
$config = new TaskConfig($config);
$config->setTempDir(EASYSWOOLE_TEMP_DIR);
$config->setServerName(Config::getInstance()->getConf('SERVER_NAME'));
$config->setServerName($serverName);
$config->setOnException(function (\Throwable $throwable){
Trigger::getInstance()->throwable($throwable);
});
TaskManager::getInstance($config)->attachToServer(ServerManager::getInstance()->getSwooleServer());
/*
* 注册基础服务进程
*/
$config = new ProcessConfig();
$config->setProcessName($serverName.'.BaseService');
$config->setProcessGroup('EasySwoole.BaseService');
$process = new BaseService($config);
Manager::getInstance()->addProcess($process);
Manager::getInstance()->attachToServer(ServerManager::getInstance()->getSwooleServer());
}
}
7 changes: 6 additions & 1 deletion src/Crontab/Crontab.php
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
use EasySwoole\EasySwoole\Crontab\Exception\CronTaskRuleInvalid;
use EasySwoole\EasySwoole\ServerManager;
use Swoole\Table;
use EasySwoole\Component\Process\Config as ProcessConfig;

class Crontab
{
Expand Down Expand Up @@ -134,7 +135,11 @@ function __run()
if (!empty($this->tasks)) {
$server = ServerManager::getInstance()->getSwooleServer();
$name = Config::getInstance()->getConf('SERVER_NAME');
$runner = new CronRunner("{$name}.Crontab", $this->tasks);
$config = new ProcessConfig();
$config->setArg($this->tasks);
$config->setProcessName("{$name}.Crontab");
$config->setProcessGroup("EasySwoole.Crontab");
$runner = new CronRunner($config);

// 将当前任务的初始规则全部添加到swTable管理
TableManager::getInstance()->add(self::$__swooleTableName, [
Expand Down
23 changes: 23 additions & 0 deletions src/Process/BaseService.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
<?php


namespace EasySwoole\EasySwoole\Process;


use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Component\Timer;

class BaseService extends AbstractProcess
{
protected function run($arg)
{
/*
* 本进程用于做Easyswoole后续的一些基础附加服务
*/
$dir = EASYSWOOLE_TEMP_DIR.'/process.json';
Timer::getInstance()->loop(1*1000,function ()use($dir){
$list = Manager::getInstance()->info();
file_put_contents($dir,json_encode($list,JSON_UNESCAPED_SLASHES|JSON_UNESCAPED_UNICODE));
});
}
}
96 changes: 96 additions & 0 deletions src/Process/Manager.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
<?php


namespace EasySwoole\EasySwoole\Process;


use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Component\Singleton;
use EasySwoole\Component\TableManager;
use Swoole\Process;
use Swoole\Server;
use Swoole\Table;

class Manager
{
use Singleton;

protected $processList = [];
protected $table;

function __construct()
{
TableManager::getInstance()->add(AbstractProcess::PROCESS_TABLE_NAME,[
'pid'=>[
'type'=>Table::TYPE_INT,
'size'=>10,
],
'name'=>[
'type'=>Table::TYPE_STRING,
'size'=>45,
],
'group'=>[
'type'=>Table::TYPE_STRING,
'size'=>45,
]
]);
$this->table = TableManager::getInstance()->get(AbstractProcess::PROCESS_TABLE_NAME);
}


function kill($pidOrGroupName,$sig = SIGTERM)
{
$list = [];
if(is_numeric($pidOrGroupName)){
$list[] = $pidOrGroupName;
}else{
foreach ($this->table as $key => $value){
if($value['group'] == $pidOrGroupName){
$list[] = $value['pid'];
}
}
}

foreach ($list as $pid){
Process::kill($pid,$sig);
}
}

function info($pidOrGroupName = null)
{
$list = [];
if($pidOrGroupName == null){
foreach ($this->table as $pid =>$value){
$list[$pid] = $value;
}
}else if(is_numeric($pidOrGroupName)){
$info = $this->table->get($pidOrGroupName);
if($info){
$list[$pidOrGroupName] = $info;
}
}else{
foreach ($this->table as $key => $value){
if($value['group'] == $pidOrGroupName){
$list[$key] = $value;
}
}
}

return $list;
}

function addProcess(AbstractProcess $process)
{
$this->processList[] = $process;;
return $this;
}

function attachToServer(Server $server)
{
/** @var AbstractProcess $process */
foreach ($this->processList as $process)
{
$server->addProcess($process->getProcess());
}
}
}
39 changes: 8 additions & 31 deletions src/ServerManager.php
Original file line number Diff line number Diff line change
Expand Up @@ -9,32 +9,32 @@
namespace EasySwoole\EasySwoole;


use EasySwoole\Component\Process\AbstractProcess;
use EasySwoole\Component\Singleton;
use EasySwoole\EasySwoole\Swoole\EventRegister;
use Swoole\Process;
use Swoole\Redis\Server as RedisServer;
use Swoole\Server;
use Swoole\WebSocket\Server as WebSocketServer;
use Swoole\Http\Server as HttpServer;

class ServerManager
{
use Singleton;
/**
* @var \swoole_server $swooleServer
* @var Server $swooleServer
*/
private $swooleServer;
private $mainServerEventRegister;
private $subServer = [];
private $subServerRegister = [];
private $isStart = false;
private $customProcess = [];

function __construct()
{
$this->mainServerEventRegister = new EventRegister();
}
/**
* @param string $serverName
* @return null|\swoole_server|\swoole_server_port|\swoole_websocket_server|\swoole_http_server
* @return null|Server|Server\Port|WebSocketServer|HttpServer
*/
function getSwooleServer(string $serverName = null)
{
Expand All @@ -52,15 +52,15 @@ function createSwooleServer($port,$type ,$address = '0.0.0.0',array $setting = [
{
switch ($type){
case EASYSWOOLE_SERVER:{
$this->swooleServer = new \swoole_server($address,$port,...$args);
$this->swooleServer = new Server($address,$port,...$args);
break;
}
case EASYSWOOLE_WEB_SERVER:{
$this->swooleServer = new \swoole_http_server($address,$port,...$args);
$this->swooleServer = new HttpServer($address,$port,...$args);
break;
}
case EASYSWOOLE_WEB_SOCKET_SERVER:{
$this->swooleServer = new \swoole_websocket_server($address,$port,...$args);
$this->swooleServer = new WebSocketServer($address,$port,...$args);
break;
}
case EASYSWOOLE_REDIS_SERVER:{
Expand Down Expand Up @@ -97,29 +97,6 @@ public function addServer(string $serverName,int $port,int $type = SWOOLE_TCP,st
return $eventRegister;
}

public function addProcess(AbstractProcess $process, string $processName=null)
{
if ($processName === null) {
$processName = $process->getProcessName();
if ($processName === null) {
$processClass = get_class($process);
$processName = basename(str_replace('\\','/',$processClass));
}
}

if (isset($this->customProcess[$processName])) {
throw new \Exception("Custom process names must be unique :{$processName}");
}

$this->customProcess[$processName] = $process->getProcess();
$this->getSwooleServer()->addProcess($process->getProcess());
}

public function getProcess(string $processName) : ?process
{
return $this->customProcess[$processName];
}

function getMainEventRegister():EventRegister
{
return $this->mainServerEventRegister;
Expand Down

0 comments on commit 8cd55da

Please sign in to comment.